Skip to content

Commit

Permalink
[8.x] Adds "asRowid" modifier, making a column an alias of "rowid"
Browse files Browse the repository at this point in the history
  • Loading branch information
Max13 committed Jan 7, 2021
1 parent 930e7f6 commit ed30505
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
}
48 changes: 48 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit ed30505

Please sign in to comment.