Skip to content

[11.x] Add support for non-primary auto-increment column #50155

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ protected function compileCreateTable($blueprint, $command, $connection)
{
$tableStructure = $this->getColumns($blueprint);

if ($primaryKey = $this->getCommandByName($blueprint, 'primary')) {
$tableStructure[] = sprintf(
'primary key %s(%s)',
$primaryKey->algorithm ? 'using '.$primaryKey->algorithm : '',
$this->columnize($primaryKey->columns)
);
foreach (['index', 'fulltext', 'spatialIndex', 'primary', 'unique'] as $index) {
foreach ($this->getCommandsByName($blueprint, $index) as $command) {
$method = 'compile'.ucfirst($index);

$tableStructure[] = $this->$method($blueprint, $command);

$primaryKey->shouldBeSkipped = true;
$command->shouldBeSkipped = true;
}
}

return sprintf('%s table %s (%s)',
Expand Down Expand Up @@ -383,11 +383,9 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
*/
public function compilePrimary(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter table %s add primary key %s(%s)',
$this->wrapTable($blueprint),
$command->algorithm ? 'using '.$command->algorithm : '',
$this->columnize($command->columns)
);
$command->index(null);

return $this->compileKey($blueprint, $command, 'primary key');
}

/**
Expand Down Expand Up @@ -448,11 +446,11 @@ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
*/
protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
{
return sprintf('alter table %s add %s %s%s(%s)',
$this->wrapTable($blueprint),
return sprintf('%s%s%s %s(%s)',
$blueprint->creating() ? '' : 'alter table '.$this->wrapTable($blueprint).' add ',
$type,
$this->wrap($command->index),
$command->algorithm ? ' using '.$command->algorithm : '',
$command->index ? ' '.$this->wrap($command->index) : '',
$command->algorithm ? 'using '.$command->algorithm : '',
$this->columnize($command->columns)
);
}
Expand Down
53 changes: 47 additions & 6 deletions tests/Database/DatabaseMariaDbSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public function testAddingUniqueKey()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add unique `bar`(`foo`)', $statements[0]);
$this->assertSame('alter table `users` add unique `bar` (`foo`)', $statements[0]);
}

public function testAddingIndex()
Expand All @@ -375,7 +375,7 @@ public function testAddingIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add index `baz`(`foo`, `bar`)', $statements[0]);
$this->assertSame('alter table `users` add index `baz` (`foo`, `bar`)', $statements[0]);
}

public function testAddingIndexWithAlgorithm()
Expand All @@ -395,7 +395,7 @@ public function testAddingFulltextIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add fulltext `users_body_fulltext`(`body`)', $statements[0]);
$this->assertSame('alter table `users` add fulltext `users_body_fulltext` (`body`)', $statements[0]);
}

public function testAddingSpatialIndex()
Expand All @@ -405,7 +405,7 @@ public function testAddingSpatialIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[0]);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex` (`coordinates`)', $statements[0]);
}

public function testAddingFluentSpatialIndex()
Expand All @@ -415,7 +415,7 @@ public function testAddingFluentSpatialIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(2, $statements);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[1]);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex` (`coordinates`)', $statements[1]);
}

public function testAddingRawIndex()
Expand All @@ -425,7 +425,48 @@ public function testAddingRawIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add index `raw_index`((function(column)))', $statements[0]);
$this->assertSame('alter table `users` add index `raw_index` ((function(column)))', $statements[0]);
}

public function testAddingIndexesOnCreate()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->integer('col1');
$blueprint->integer('col2');
$blueprint->integer('col3');
$blueprint->string('col4');
$blueprint->geometry('col5');
$blueprint->primary('col1');
$blueprint->unique('col2');
$blueprint->index('col3');
$blueprint->fullText('col4');
$blueprint->spatialIndex('col5');

$conn = $this->getConnection();
$conn->shouldReceive('getConfig')->andReturn(null);

$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertEquals(['create table `users` (`col1` int not null, `col2` int not null, `col3` int not null, `col4` varchar(255) not null, `col5` geometry not null, index `users_col3_index` (`col3`), fulltext `users_col4_fulltext` (`col4`), spatial index `users_col5_spatialindex` (`col5`), primary key (`col1`), unique `users_col2_unique` (`col2`))'], $statements);
}

public function testFluentAddingIndexesOnCreate()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->integer('col1')->primary();
$blueprint->integer('col2')->unique();
$blueprint->integer('col3')->index();
$blueprint->string('col4')->fulltext();
$blueprint->geometry('col5')->spatialIndex();

$conn = $this->getConnection();
$conn->shouldReceive('getConfig')->andReturn(null);

$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertEquals(['create table `users` (`col1` int not null, `col2` int not null, `col3` int not null, `col4` varchar(255) not null, `col5` geometry not null, index `users_col3_index` (`col3`), fulltext `users_col4_fulltext` (`col4`), spatial index `users_col5_spatialindex` (`col5`), primary key (`col1`), unique `users_col2_unique` (`col2`))'], $statements);
}

public function testAddingForeignKey()
Expand Down
53 changes: 47 additions & 6 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public function testAddingUniqueKey()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add unique `bar`(`foo`)', $statements[0]);
$this->assertSame('alter table `users` add unique `bar` (`foo`)', $statements[0]);
}

public function testAddingIndex()
Expand All @@ -375,7 +375,7 @@ public function testAddingIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add index `baz`(`foo`, `bar`)', $statements[0]);
$this->assertSame('alter table `users` add index `baz` (`foo`, `bar`)', $statements[0]);
}

public function testAddingIndexWithAlgorithm()
Expand All @@ -395,7 +395,7 @@ public function testAddingFulltextIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add fulltext `users_body_fulltext`(`body`)', $statements[0]);
$this->assertSame('alter table `users` add fulltext `users_body_fulltext` (`body`)', $statements[0]);
}

public function testAddingSpatialIndex()
Expand All @@ -405,7 +405,7 @@ public function testAddingSpatialIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[0]);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex` (`coordinates`)', $statements[0]);
}

public function testAddingFluentSpatialIndex()
Expand All @@ -415,7 +415,7 @@ public function testAddingFluentSpatialIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(2, $statements);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[1]);
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex` (`coordinates`)', $statements[1]);
}

public function testAddingRawIndex()
Expand All @@ -425,7 +425,48 @@ public function testAddingRawIndex()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add index `raw_index`((function(column)))', $statements[0]);
$this->assertSame('alter table `users` add index `raw_index` ((function(column)))', $statements[0]);
}

public function testAddingIndexesOnCreate()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->integer('col1');
$blueprint->integer('col2');
$blueprint->integer('col3');
$blueprint->string('col4');
$blueprint->geometry('col5');
$blueprint->primary('col1');
$blueprint->unique('col2');
$blueprint->index('col3');
$blueprint->fullText('col4');
$blueprint->spatialIndex('col5');

$conn = $this->getConnection();
$conn->shouldReceive('getConfig')->andReturn(null);

$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertEquals(['create table `users` (`col1` int not null, `col2` int not null, `col3` int not null, `col4` varchar(255) not null, `col5` geometry not null, index `users_col3_index` (`col3`), fulltext `users_col4_fulltext` (`col4`), spatial index `users_col5_spatialindex` (`col5`), primary key (`col1`), unique `users_col2_unique` (`col2`))'], $statements);
}

public function testFluentAddingIndexesOnCreate()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->integer('col1')->primary();
$blueprint->integer('col2')->unique();
$blueprint->integer('col3')->index();
$blueprint->string('col4')->fulltext();
$blueprint->geometry('col5')->spatialIndex();

$conn = $this->getConnection();
$conn->shouldReceive('getConfig')->andReturn(null);

$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertEquals(['create table `users` (`col1` int not null, `col2` int not null, `col3` int not null, `col4` varchar(255) not null, `col5` geometry not null, index `users_col3_index` (`col3`), fulltext `users_col4_fulltext` (`col4`), spatial index `users_col5_spatialindex` (`col5`), primary key (`col1`), unique `users_col2_unique` (`col2`))'], $statements);
}

public function testAddingForeignKey()
Expand Down
12 changes: 6 additions & 6 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public function testDefaultUsingIdMorph()

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` bigint unsigned not null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
'alter table `comments` add index `comments_commentable_type_commentable_id_index` (`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

Expand All @@ -294,7 +294,7 @@ public function testDefaultUsingNullableIdMorph()

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` bigint unsigned null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
'alter table `comments` add index `comments_commentable_type_commentable_id_index` (`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

Expand All @@ -312,7 +312,7 @@ public function testDefaultUsingUuidMorph()

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` char(36) not null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
'alter table `comments` add index `comments_commentable_type_commentable_id_index` (`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

Expand All @@ -330,7 +330,7 @@ public function testDefaultUsingNullableUuidMorph()

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` char(36) null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
'alter table `comments` add index `comments_commentable_type_commentable_id_index` (`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

Expand All @@ -348,7 +348,7 @@ public function testDefaultUsingUlidMorph()

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` char(26) not null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
'alter table `comments` add index `comments_commentable_type_commentable_id_index` (`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

Expand All @@ -366,7 +366,7 @@ public function testDefaultUsingNullableUlidMorph()

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` char(26) null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
'alter table `comments` add index `comments_commentable_type_commentable_id_index` (`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public function testAddUniqueIndexWithoutNameWorks()

$expected = [
'alter table `users` modify `name` varchar(255) null',
'alter table `users` add unique `users_name_unique`(`name`)',
'alter table `users` add unique `users_name_unique` (`name`)',
];

$this->assertEquals($expected, $queries);
Expand Down Expand Up @@ -470,7 +470,7 @@ public function testAddUniqueIndexWithNameWorks()

$expected = [
'alter table `users` modify `name` varchar(255) null',
'alter table `users` add unique `index1`(`name`)',
'alter table `users` add unique `index1` (`name`)',
];

$this->assertEquals($expected, $queries);
Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ public function testCompoundPrimaryWithAutoIncrement()
$this->assertTrue(Schema::hasIndex('test', ['id', 'uuid'], 'primary'));
}

public function testNonPrimaryAutoIncrementColumn()
{
if ($this->driver === 'sqlite') {
$this->markTestSkipped('non-primary auto-increment column is not supported on SQLite.');
}

Schema::create('test', function (Blueprint $table) {
$table->uuid()->primary();
$table->id()->unique();
});

$this->assertTrue(collect(Schema::getColumns('test'))->firstWhere('name', 'id')['auto_increment']);
$this->assertTrue(Schema::hasIndex('test', ['uuid'], 'primary'));
$this->assertTrue(Schema::hasIndex('test', ['id'], 'unique'));
$this->assertTrue(Schema::hasIndex('test', 'test_id_unique', 'unique'));
}

public function testModifyingAutoIncrementColumn()
{
if ($this->driver === 'sqlsrv') {
Expand Down