Skip to content

Commit

Permalink
Disable autoincrement for unsupported column type (#48501)
Browse files Browse the repository at this point in the history
ref: https://www.doctrine-project.org/projects/doctrine-dbal/en/3.6/reference/schema-representation.html

Autoincrement is only supported on smallint, integer, and bigint column
types. If a primary index column type is changed to an invalid type,
generated SQL will be missing the primary key declaration syntax.
  • Loading branch information
ikari7789 authored Sep 22, 2023
1 parent 05c7572 commit 165252b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ protected static function getDoctrineColumnChangeOptions(Fluent $fluent)
{
$options = ['type' => static::getDoctrineColumnType($fluent['type'])];

if (! in_array($fluent['type'], ['smallint', 'integer', 'bigint'])) {
$options['autoincrement'] = false;
}

if (in_array($fluent['type'], ['tinyText', 'text', 'mediumText', 'longText'])) {
$options['length'] = static::calculateDoctrineTextLength($fluent['type']);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,31 @@ public function testChangingCharColumnsWork()
$this->assertContains($queries, $expected);
}

public function testChangingPrimaryAutoincrementColumnsToNonAutoincrementColumnsWork()
{
$this->db->connection()->getSchemaBuilder()->create('users', function ($table) {
$table->increments('id');
});

$blueprint = new Blueprint('users', function ($table) {
$table->binary('id')->change();
});

$queries = $blueprint->toSql($this->db->connection(), new SQLiteGrammar);

$expected = [
[
'CREATE TEMPORARY TABLE __temp__users AS SELECT id FROM users',
'DROP TABLE users',
'CREATE TABLE users (id BLOB NOT NULL, PRIMARY KEY(id))',
'INSERT INTO users (id) SELECT id FROM __temp__users',
'DROP TABLE __temp__users',
],
];

$this->assertContains($queries, $expected);
}

public function testChangingDoubleColumnsWork()
{
$this->db->connection()->getSchemaBuilder()->create('products', function ($table) {
Expand Down

0 comments on commit 165252b

Please sign in to comment.