Commit 8a8ee96f authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-37 - Fix PostgreSQL Platform not supporting changes in Autoicnrement...

DBAL-37 - Fix PostgreSQL Platform not supporting changes in Autoicnrement during ALTER TABLE generation
parent 835c9a03
...@@ -370,6 +370,21 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -370,6 +370,21 @@ class PostgreSqlPlatform extends AbstractPlatform
$query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL'; $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
} }
if ($columnDiff->hasChanged('autoincrement')) {
if ($column->getAutoincrement()) {
// add autoincrement
$seqName = $diff->name . '_' . $oldColumnName . '_seq';
$sql[] = "CREATE SEQUENCE " . $seqName;
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->name . "))";
$query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
$sql[] = "ALTER TABLE " . $diff->name . " " . $query;
} else {
// Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
$query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
$sql[] = "ALTER TABLE " . $diff->name . " " . $query;
}
}
} }
foreach ($diff->renamedColumns as $oldColumnName => $column) { foreach ($diff->renamedColumns as $oldColumnName => $column) {
......
...@@ -30,6 +30,74 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -30,6 +30,74 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$table = $this->_conn->getSchemaManager()->listTableDetails('domain_type_test'); $table = $this->_conn->getSchemaManager()->listTableDetails('domain_type_test');
$this->assertType('Doctrine\Tests\DBAL\Functional\Schema\MoneyType', $table->getColumn('value')->getType()); $this->assertType('Doctrine\Tests\DBAL\Functional\Schema\MoneyType', $table->getColumn('value')->getType());
} }
/**
* @group DBAL-37
*/
public function testDetectsAutoIncrement()
{
$autoincTable = new \Doctrine\DBAL\Schema\Table('autoinc_table');
$column = $autoincTable->addColumn('id', 'integer');
$column->setAutoincrement(true);
$this->_sm->createTable($autoincTable);
$autoincTable = $this->_sm->listTableDetails('autoinc_table');
$this->assertTrue($autoincTable->getColumn('id')->getAutoincrement());
}
/**
* @group DBAL-37
*/
public function testAlterTableAutoIncrementAdd()
{
$tableFrom = new \Doctrine\DBAL\Schema\Table('autoinc_table_add');
$column = $tableFrom->addColumn('id', 'integer');
$this->_sm->createTable($tableFrom);
$tableFrom = $this->_sm->listTableDetails('autoinc_table_add');
$this->assertFalse($tableFrom->getColumn('id')->getAutoincrement());
$tableTo = new \Doctrine\DBAL\Schema\Table('autoinc_table_add');
$column = $tableTo->addColumn('id', 'integer');
$column->setAutoincrement(true);
$c = new \Doctrine\DBAL\Schema\Comparator();
$diff = $c->diffTable($tableFrom, $tableTo);
$sql = $this->_conn->getDatabasePlatform()->getAlterTableSQL($diff);
$this->assertEquals(array(
"CREATE SEQUENCE autoinc_table_add_id_seq",
"SELECT setval('autoinc_table_add_id_seq', (SELECT MAX(id) FROM autoinc_table_add))",
"ALTER TABLE autoinc_table_add ALTER id SET DEFAULT nextval('autoinc_table_add_id_seq')",
), $sql);
$this->_sm->alterTable($diff);
$tableFinal = $this->_sm->listTableDetails('autoinc_table_add');
$this->assertTrue($tableFinal->getColumn('id')->getAutoincrement());
}
/**
* @group DBAL-37
*/
public function testAlterTableAutoIncrementDrop()
{
$tableFrom = new \Doctrine\DBAL\Schema\Table('autoinc_table_drop');
$column = $tableFrom->addColumn('id', 'integer');
$column->setAutoincrement(true);
$this->_sm->createTable($tableFrom);
$tableFrom = $this->_sm->listTableDetails('autoinc_table_drop');
$this->assertTrue($tableFrom->getColumn('id')->getAutoincrement());
$tableTo = new \Doctrine\DBAL\Schema\Table('autoinc_table_drop');
$column = $tableTo->addColumn('id', 'integer');
$c = new \Doctrine\DBAL\Schema\Comparator();
$diff = $c->diffTable($tableFrom, $tableTo);
$this->assertType('Doctrine\DBAL\Schema\TableDiff', $diff, "There should be a difference and not false being returned from the table comparison");
$this->assertEquals(array("ALTER TABLE autoinc_table_drop ALTER id DROP DEFAULT"), $this->_conn->getDatabasePlatform()->getAlterTableSQL($diff));
$this->_sm->alterTable($diff);
$tableFinal = $this->_sm->listTableDetails('autoinc_table_drop');
$this->assertFalse($tableFinal->getColumn('id')->getAutoincrement());
}
} }
class MoneyType extends Type class MoneyType extends Type
......
...@@ -96,6 +96,15 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase ...@@ -96,6 +96,15 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar')); $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
} }
public function testGenerateTableWithAutoincrement()
{
$table = new \Doctrine\DBAL\Schema\Table('autoinc_table');
$column = $table->addColumn('id', 'integer');
$column->setAutoincrement(true);
$this->assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table));
}
public function testGeneratesTypeDeclarationForIntegers() public function testGeneratesTypeDeclarationForIntegers()
{ {
$this->assertEquals( $this->assertEquals(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment