Fix changing column from int to bigint with autoincrement#2916
Fix changing column from int to bigint with autoincrement#2916lcobucci merged 1 commit intodoctrine:masterfrom icewind1991:postgres-int-to-bigint
Conversation
lcobucci
left a comment
There was a problem hiding this comment.
Please rebase your branch to sync to the latest changes of master
|
|
||
| if ($columnDiff->hasChanged('default') || $columnDiff->hasChanged('type')) { | ||
| if ($columnDiff->fromColumn) { | ||
| $oldTypeIsNumeric = $columnDiff->fromColumn->getType()->getName() === Type::INTEGER || $columnDiff->fromColumn->getType()->getName() === Type::BIGINT; |
There was a problem hiding this comment.
Please don't rely on Type::getName(), use instance of instead.
| if ($columnDiff->hasChanged('default') || $columnDiff->hasChanged('type')) { | ||
| if ($columnDiff->fromColumn) { | ||
| $oldTypeIsNumeric = $columnDiff->fromColumn->getType()->getName() === Type::INTEGER || $columnDiff->fromColumn->getType()->getName() === Type::BIGINT; | ||
| $newTypeIsNumeric = $column->getType()->getName() === Type::INTEGER || $column->getType()->getName() === Type::BIGINT; |
There was a problem hiding this comment.
Please don't rely on Type::getName(), use instance of instead.
| self::assertEquals('(-1)', $columns['col_string']->getDefault()); | ||
| } | ||
|
|
||
| public function testAlterTableAutoIncrementBigInt() |
There was a problem hiding this comment.
Please add @group 2916 to a docblock here
|
|
||
| public function testAlterTableAutoIncrementBigInt() | ||
| { | ||
| $tableFrom = new \Doctrine\DBAL\Schema\Table('autoinc_table_bitgint'); |
| $tableFrom = $this->_sm->listTableDetails('autoinc_table_bitgint'); | ||
| self::assertTrue($tableFrom->getColumn('id')->getAutoincrement()); | ||
|
|
||
| $tableTo = new \Doctrine\DBAL\Schema\Table('autoinc_table_bitgint'); |
| $column = $tableTo->addColumn('id', 'bigint'); | ||
| $column->setAutoincrement(true); | ||
|
|
||
| $c = new \Doctrine\DBAL\Schema\Comparator(); |
| $c = new \Doctrine\DBAL\Schema\Comparator(); | ||
| $diff = $c->diffTable($tableFrom, $tableTo); | ||
| self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $diff, "There should be a difference and not false being returned from the table comparison"); | ||
| self::assertEquals(array("ALTER TABLE autoinc_table_bitgint ALTER id TYPE BIGINT"), $this->_conn->getDatabasePlatform()->getAlterTableSQL($diff)); |
There was a problem hiding this comment.
assertSame() is more appropriated here
There was a problem hiding this comment.
Please also use short-array syntax
|
@lcobucci all done |
There was a problem hiding this comment.
Nice job, some more nitpicks 😜
Just to understand, is this needed only for PGSQL 10+ compatibility? I mean, I didn't see any issue reporting this before.
This question is just to categorise this PR correctly.
I've already checked, this operation breaks badly on PGSQL 9.x too
|
|
||
| $c = new Comparator(); | ||
| $diff = $c->diffTable($tableFrom, $tableTo); | ||
| self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $diff, "There should be a difference and not false being returned from the table comparison"); |
There was a problem hiding this comment.
Please use TableDiff::class instead
| } | ||
|
|
||
| if ($columnDiff->hasChanged('default') || $columnDiff->hasChanged('type')) { | ||
| if ($columnDiff->fromColumn) { |
There was a problem hiding this comment.
What do you think about extracting this if block to a private method? I think that something like this would improve the readability a bit:
private function hasDefaultTypeChanged(ColumnDiff $columnDiff, Column $newColumn) : bool
{
$defaultTypeChanged = $columnDiff->hasChanged('type');
if ( ! $columnDiff->fromColumn || ! $defaultTypeChanged) {
return $defaultTypeChanged;
}
$oldTypeIsNumeric = $this->isNumericType($columnDiff->fromColumn->getType());
$newTypeIsNumeric = $this->isNumericType($newColumn->getType());
return ! ($oldTypeIsNumeric && $newTypeIsNumeric && $newColumn->getDefault() === null)
}We might improve things there (specially naming wise), but I think it makes things easier and more understandable 😄
|
One last request, could add some info on the commit msg on why this is needed? BTW thanks for ensuring that the commit history is organised =) |
|
Ahhh I forgot one more thing, can you please add a test for the reverse operation too? bigint -> int |
|
done |
- SERIAL and BIGSERIAL are not true types and can't be used in ALTER TABLE expressions - When changing between int and bigint auto increment fields the default should no be dropped to preserve the link between the column and the sequence.
|
@icewind1991 🚢 and backported to |
|
Any chance of getting things backported to 2.5.x? We're stuck with supporting php 5.6/7.0 for the foreseeable future |
Currently it tries to alter the column type to
BIGSERIALwhich is not a valid type to be used in anALTER TABLEquery.Also it currently drops the default which breaks the auto increment.
cc @nickvergessen