diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index 556d749e23b2..ac047cf17e37 100755 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -55,6 +55,8 @@ public function compileColumnListing($table) */ public function compileCreate(Blueprint $blueprint, Fluent $command) { + $this->processRowid($blueprint); + return sprintf('%s table %s (%s%s%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), @@ -135,6 +137,8 @@ protected function addPrimaryKeys(Blueprint $blueprint) */ public function compileAdd(Blueprint $blueprint, Fluent $command) { + $this->processRowid($blueprint); + $columns = $this->prefixArray('add column', $this->getColumns($blueprint)); return collect($columns)->reject(function ($column) { @@ -907,7 +911,30 @@ protected function modifyDefault(Blueprint $blueprint, Fluent $column) protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { if (in_array($column->type, $this->serials) && $column->autoIncrement) { - return ' primary key autoincrement'; + $modifier = ' primary key'; + + if (! $column->asRowid) { + $modifier .= ' autoincrement'; + } + + return $modifier; } } + + /** + * Modify blueprint's columns marked as rowid. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @return void + */ + protected function processRowid(Blueprint $blueprint) + { + collect($blueprint->getAddedColumns())->filter(function ($column) { + return $column->asRowid === true; + })->each(function ($column) { + $column->autoIncrement(); + $column->nullable(); + unset($column->unsigned); + }); + } } diff --git a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php index 226c58bf2b34..0627b93f1c2d 100755 --- a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php +++ b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php @@ -43,6 +43,30 @@ public function testBasicCreateTable() $this->assertEquals($expected, $statements); } + public function testBasicCreateTableWithRowid() + { + $blueprint = new Blueprint('users'); + $blueprint->create(); + $blueprint->increments('id')->asRowid(); + $blueprint->string('email'); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertSame('create table "users" ("id" integer primary key, "email" varchar not null)', $statements[0]); + + $blueprint = new Blueprint('users'); + $blueprint->increments('id')->asRowid(); + $blueprint->string('email'); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(2, $statements); + $expected = [ + 'alter table "users" add column "id" integer primary key', + 'alter table "users" add column "email" varchar not null', + ]; + $this->assertEquals($expected, $statements); + } + public function testCreateTemporaryTable() { $blueprint = new Blueprint('users'); @@ -56,6 +80,19 @@ public function testCreateTemporaryTable() $this->assertSame('create temporary table "users" ("id" integer not null primary key autoincrement, "email" varchar not null)', $statements[0]); } + public function testCreateTemporaryTableWithRowid() + { + $blueprint = new Blueprint('users'); + $blueprint->create(); + $blueprint->temporary(); + $blueprint->increments('id')->asRowid(); + $blueprint->string('email'); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertSame('create temporary table "users" ("id" integer primary key, "email" varchar not null)', $statements[0]); + } + public function testDropTable() { $blueprint = new Blueprint('users'); @@ -199,6 +236,17 @@ public function testAddingPrimaryKey() $this->assertSame('create table "users" ("foo" varchar not null, primary key ("foo"))', $statements[0]); } + public function testAddingPrimaryKeyAsRowid() + { + $blueprint = new Blueprint('users'); + $blueprint->create(); + $blueprint->string('foo')->primary()->asRowid(); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertSame('create table "users" ("foo" varchar, primary key ("foo"))', $statements[0]); + } + public function testAddingForeignKey() { $blueprint = new Blueprint('users');