Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Adds "asRowid" modifier, making a column an alias of "rowid" #35814

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
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
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