123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Database\Capsule\Manager;
- use Illuminate\Database\Connection;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Schema\ForeignIdColumnDefinition;
- use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use RuntimeException;
- class DatabaseSQLiteSchemaGrammarTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testBasicCreateTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->increments('id');
- $blueprint->string('email');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create table "users" ("id" integer not null primary key autoincrement, "email" varchar not null)', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->increments('id');
- $blueprint->string('email');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $expected = [
- 'alter table "users" add column "id" integer not null primary key autoincrement',
- 'alter table "users" add column "email" varchar not null',
- ];
- $this->assertEquals($expected, $statements);
- }
- public function testCreateTemporaryTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->temporary();
- $blueprint->increments('id');
- $blueprint->string('email');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create temporary table "users" ("id" integer not null primary key autoincrement, "email" varchar not null)', $statements[0]);
- }
- public function testDropTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->drop();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop table "users"', $statements[0]);
- }
- public function testDropTableIfExists()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropIfExists();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop table if exists "users"', $statements[0]);
- }
- public function testDropUnique()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropUnique('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop index "foo"', $statements[0]);
- }
- public function testDropIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropIndex('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('drop index "foo"', $statements[0]);
- }
- public function testDropColumn()
- {
- $db = new Manager;
- $db->addConnection([
- 'driver' => 'sqlite',
- 'database' => ':memory:',
- 'prefix' => 'prefix_',
- ]);
- $schema = $db->getConnection()->getSchemaBuilder();
- $schema->create('users', function (Blueprint $table) {
- $table->string('email');
- $table->string('name');
- });
- $this->assertTrue($schema->hasTable('users'));
- $this->assertTrue($schema->hasColumn('users', 'name'));
- $schema->table('users', function (Blueprint $table) {
- $table->dropColumn('name');
- });
- $this->assertFalse($schema->hasColumn('users', 'name'));
- }
- public function testDropSpatialIndex()
- {
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage('The database driver in use does not support spatial indexes.');
- $blueprint = new Blueprint('geo');
- $blueprint->dropSpatialIndex(['coordinates']);
- $blueprint->toSql($this->getConnection(), $this->getGrammar());
- }
- public function testRenameTable()
- {
- $blueprint = new Blueprint('users');
- $blueprint->rename('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" rename to "foo"', $statements[0]);
- }
- public function testRenameIndex()
- {
- $db = new Manager;
- $db->addConnection([
- 'driver' => 'sqlite',
- 'database' => ':memory:',
- 'prefix' => 'prefix_',
- ]);
- $schema = $db->getConnection()->getSchemaBuilder();
- $schema->create('users', function (Blueprint $table) {
- $table->string('name');
- $table->string('email');
- });
- $schema->table('users', function (Blueprint $table) {
- $table->index(['name', 'email'], 'index1');
- });
- $manager = $db->getConnection()->getDoctrineSchemaManager();
- $details = $manager->listTableDetails('prefix_users');
- $this->assertTrue($details->hasIndex('index1'));
- $this->assertFalse($details->hasIndex('index2'));
- $schema->table('users', function (Blueprint $table) {
- $table->renameIndex('index1', 'index2');
- });
- $details = $manager->listTableDetails('prefix_users');
- $this->assertFalse($details->hasIndex('index1'));
- $this->assertTrue($details->hasIndex('index2'));
- $this->assertEquals(['name', 'email'], $details->getIndex('index2')->getUnquotedColumns());
- }
- public function testAddingPrimaryKey()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->string('foo')->primary();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create table "users" ("foo" varchar not null, primary key ("foo"))', $statements[0]);
- }
- public function testAddingForeignKey()
- {
- $blueprint = new Blueprint('users');
- $blueprint->create();
- $blueprint->string('foo')->primary();
- $blueprint->string('order_id');
- $blueprint->foreign('order_id')->references('id')->on('orders');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create table "users" ("foo" varchar not null, "order_id" varchar not null, foreign key("order_id") references "orders"("id"), primary key ("foo"))', $statements[0]);
- }
- public function testAddingUniqueKey()
- {
- $blueprint = new Blueprint('users');
- $blueprint->unique('foo', 'bar');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create unique index "bar" on "users" ("foo")', $statements[0]);
- }
- public function testAddingIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->index(['foo', 'bar'], 'baz');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "baz" on "users" ("foo", "bar")', $statements[0]);
- }
- public function testAddingSpatialIndex()
- {
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage('The database driver in use does not support spatial indexes.');
- $blueprint = new Blueprint('geo');
- $blueprint->spatialIndex('coordinates');
- $blueprint->toSql($this->getConnection(), $this->getGrammar());
- }
- public function testAddingFluentSpatialIndex()
- {
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage('The database driver in use does not support spatial indexes.');
- $blueprint = new Blueprint('geo');
- $blueprint->point('coordinates')->spatialIndex();
- $blueprint->toSql($this->getConnection(), $this->getGrammar());
- }
- public function testAddingRawIndex()
- {
- $blueprint = new Blueprint('users');
- $blueprint->rawIndex('(function(column))', 'raw_index');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
- }
- public function testAddingIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->increments('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingSmallIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->smallIncrements('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingMediumIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->mediumIncrements('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->id();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->id('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingForeignID()
- {
- $blueprint = new Blueprint('users');
- $foreignId = $blueprint->foreignId('foo');
- $blueprint->foreignId('company_id')->constrained();
- $blueprint->foreignId('laravel_idea_id')->constrained();
- $blueprint->foreignId('team_id')->references('id')->on('teams');
- $blueprint->foreignId('team_column_id')->constrained('teams');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
- $this->assertSame([
- 'alter table "users" add column "foo" integer not null',
- 'alter table "users" add column "company_id" integer not null',
- 'alter table "users" add column "laravel_idea_id" integer not null',
- 'alter table "users" add column "team_id" integer not null',
- 'alter table "users" add column "team_column_id" integer not null',
- ], $statements);
- }
- public function testAddingBigIncrementingID()
- {
- $blueprint = new Blueprint('users');
- $blueprint->bigIncrements('id');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "id" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingString()
- {
- $blueprint = new Blueprint('users');
- $blueprint->string('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->string('foo', 100);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->string('foo', 100)->nullable()->default('bar');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar default \'bar\'', $statements[0]);
- }
- public function testAddingText()
- {
- $blueprint = new Blueprint('users');
- $blueprint->text('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
- }
- public function testAddingBigInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->bigInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->bigInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->integer('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->integer('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingMediumInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->mediumInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->mediumInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingTinyInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->tinyInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->tinyInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingSmallInteger()
- {
- $blueprint = new Blueprint('users');
- $blueprint->smallInteger('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null', $statements[0]);
- $blueprint = new Blueprint('users');
- $blueprint->smallInteger('foo', true);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" integer not null primary key autoincrement', $statements[0]);
- }
- public function testAddingFloat()
- {
- $blueprint = new Blueprint('users');
- $blueprint->float('foo', 5, 2);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" float not null', $statements[0]);
- }
- public function testAddingDouble()
- {
- $blueprint = new Blueprint('users');
- $blueprint->double('foo', 15, 8);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" float not null', $statements[0]);
- }
- public function testAddingDecimal()
- {
- $blueprint = new Blueprint('users');
- $blueprint->decimal('foo', 5, 2);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" numeric not null', $statements[0]);
- }
- public function testAddingBoolean()
- {
- $blueprint = new Blueprint('users');
- $blueprint->boolean('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" tinyint(1) not null', $statements[0]);
- }
- public function testAddingEnum()
- {
- $blueprint = new Blueprint('users');
- $blueprint->enum('role', ['member', 'admin']);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "role" varchar check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
- }
- public function testAddingJson()
- {
- $blueprint = new Blueprint('users');
- $blueprint->json('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
- }
- public function testAddingJsonb()
- {
- $blueprint = new Blueprint('users');
- $blueprint->jsonb('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" text not null', $statements[0]);
- }
- public function testAddingDate()
- {
- $blueprint = new Blueprint('users');
- $blueprint->date('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
- }
- public function testAddingYear()
- {
- $blueprint = new Blueprint('users');
- $blueprint->year('birth_year');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
- }
- public function testAddingDateTime()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTime('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingDateTimeWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTime('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingDateTimeTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTimeTz('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingDateTimeTzWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dateTimeTz('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingTime()
- {
- $blueprint = new Blueprint('users');
- $blueprint->time('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
- }
- public function testAddingTimeWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->time('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
- }
- public function testAddingTimeTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timeTz('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
- }
- public function testAddingTimeTzWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timeTz('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" time not null', $statements[0]);
- }
- public function testAddingTimestamp()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamp('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingTimestampWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamp('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingTimestampTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampTz('created_at');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingTimestampTzWithPrecision()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampTz('created_at', 1);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "created_at" datetime not null', $statements[0]);
- }
- public function testAddingTimestamps()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestamps();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $this->assertEquals([
- 'alter table "users" add column "created_at" datetime',
- 'alter table "users" add column "updated_at" datetime',
- ], $statements);
- }
- public function testAddingTimestampsTz()
- {
- $blueprint = new Blueprint('users');
- $blueprint->timestampsTz();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $this->assertEquals([
- 'alter table "users" add column "created_at" datetime',
- 'alter table "users" add column "updated_at" datetime',
- ], $statements);
- }
- public function testAddingRememberToken()
- {
- $blueprint = new Blueprint('users');
- $blueprint->rememberToken();
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "remember_token" varchar', $statements[0]);
- }
- public function testAddingBinary()
- {
- $blueprint = new Blueprint('users');
- $blueprint->binary('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" blob not null', $statements[0]);
- }
- public function testAddingUuid()
- {
- $blueprint = new Blueprint('users');
- $blueprint->uuid('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
- }
- public function testAddingForeignUuid()
- {
- $blueprint = new Blueprint('users');
- $foreignUuid = $blueprint->foreignUuid('foo');
- $blueprint->foreignUuid('company_id')->constrained();
- $blueprint->foreignUuid('laravel_idea_id')->constrained();
- $blueprint->foreignUuid('team_id')->references('id')->on('teams');
- $blueprint->foreignUuid('team_column_id')->constrained('teams');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
- $this->assertSame([
- 'alter table "users" add column "foo" varchar not null',
- 'alter table "users" add column "company_id" varchar not null',
- 'alter table "users" add column "laravel_idea_id" varchar not null',
- 'alter table "users" add column "team_id" varchar not null',
- 'alter table "users" add column "team_column_id" varchar not null',
- ], $statements);
- }
- public function testAddingIpAddress()
- {
- $blueprint = new Blueprint('users');
- $blueprint->ipAddress('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
- }
- public function testAddingMacAddress()
- {
- $blueprint = new Blueprint('users');
- $blueprint->macAddress('foo');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "users" add column "foo" varchar not null', $statements[0]);
- }
- public function testAddingGeometry()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->geometry('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geometry not null', $statements[0]);
- }
- public function testAddingPoint()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->point('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" point not null', $statements[0]);
- }
- public function testAddingLineString()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->linestring('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" linestring not null', $statements[0]);
- }
- public function testAddingPolygon()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->polygon('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" polygon not null', $statements[0]);
- }
- public function testAddingGeometryCollection()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->geometrycollection('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" geometrycollection not null', $statements[0]);
- }
- public function testAddingMultiPoint()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->multipoint('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" multipoint not null', $statements[0]);
- }
- public function testAddingMultiLineString()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->multilinestring('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" multilinestring not null', $statements[0]);
- }
- public function testAddingMultiPolygon()
- {
- $blueprint = new Blueprint('geo');
- $blueprint->multipolygon('coordinates');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('alter table "geo" add column "coordinates" multipolygon not null', $statements[0]);
- }
- public function testAddingGeneratedColumn()
- {
- $blueprint = new Blueprint('products');
- $blueprint->create();
- $blueprint->integer('price');
- $blueprint->integer('discounted_virtual')->virtualAs('"price" - 5');
- $blueprint->integer('discounted_stored')->storedAs('"price" - 5');
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(1, $statements);
- $this->assertSame('create table "products" ("price" integer not null, "discounted_virtual" integer as ("price" - 5), "discounted_stored" integer as ("price" - 5) stored)', $statements[0]);
- $blueprint = new Blueprint('products');
- $blueprint->integer('price');
- $blueprint->integer('discounted_virtual')->virtualAs('"price" - 5')->nullable(false);
- $blueprint->integer('discounted_stored')->storedAs('"price" - 5')->nullable(false);
- $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
- $this->assertCount(2, $statements);
- $expected = [
- 'alter table "products" add column "price" integer not null',
- 'alter table "products" add column "discounted_virtual" integer as ("price" - 5) not null',
- ];
- $this->assertSame($expected, $statements);
- }
- public function testGrammarsAreMacroable()
- {
- // compileReplace macro.
- $this->getGrammar()::macro('compileReplace', function () {
- return true;
- });
- $c = $this->getGrammar()::compileReplace();
- $this->assertTrue($c);
- }
- protected function getConnection()
- {
- return m::mock(Connection::class);
- }
- public function getGrammar()
- {
- return new SQLiteGrammar;
- }
- }
|