Commit 08afb482 authored by Steve Müller's avatar Steve Müller

fix SQLite integer type primary autoincrement columns

parent 439f6743
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\ForeignKeyConstraint;
...@@ -213,6 +214,11 @@ class SqlitePlatform extends AbstractPlatform ...@@ -213,6 +214,11 @@ class SqlitePlatform extends AbstractPlatform
*/ */
public function getBigIntTypeDeclarationSQL(array $field) public function getBigIntTypeDeclarationSQL(array $field)
{ {
// SQLite autoincrement is implicit for INTEGER PKs, but not for BIGINT fields.
if ( ! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
}
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
} }
...@@ -221,6 +227,11 @@ class SqlitePlatform extends AbstractPlatform ...@@ -221,6 +227,11 @@ class SqlitePlatform extends AbstractPlatform
*/ */
public function getTinyIntTypeDeclarationSql(array $field) public function getTinyIntTypeDeclarationSql(array $field)
{ {
// SQLite autoincrement is implicit for INTEGER PKs, but not for TINYINT fields.
if ( ! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
}
return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
} }
...@@ -229,6 +240,11 @@ class SqlitePlatform extends AbstractPlatform ...@@ -229,6 +240,11 @@ class SqlitePlatform extends AbstractPlatform
*/ */
public function getSmallIntTypeDeclarationSQL(array $field) public function getSmallIntTypeDeclarationSQL(array $field)
{ {
// SQLite autoincrement is implicit for INTEGER PKs, but not for SMALLINT fields.
if ( ! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
}
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
} }
...@@ -237,6 +253,11 @@ class SqlitePlatform extends AbstractPlatform ...@@ -237,6 +253,11 @@ class SqlitePlatform extends AbstractPlatform
*/ */
public function getMediumIntTypeDeclarationSql(array $field) public function getMediumIntTypeDeclarationSql(array $field)
{ {
// SQLite autoincrement is implicit for INTEGER PKs, but not for MEDIUMINT fields.
if ( ! empty($field['autoincrement'])) {
return $this->getIntegerTypeDeclarationSQL($field);
}
return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
} }
...@@ -845,6 +866,29 @@ class SqlitePlatform extends AbstractPlatform ...@@ -845,6 +866,29 @@ class SqlitePlatform extends AbstractPlatform
*/ */
private function getSimpleAlterTableSQL(TableDiff $diff) private function getSimpleAlterTableSQL(TableDiff $diff)
{ {
// Suppress changes on integer type autoincrement columns.
foreach ($diff->changedColumns as $oldColumnName => $columnDiff) {
if ( ! $columnDiff->fromColumn instanceof Column ||
! $columnDiff->column instanceof Column ||
! $columnDiff->column->getAutoincrement() ||
! (string) $columnDiff->column->getType() === 'Integer'
) {
continue;
}
if ( ! $columnDiff->hasChanged('type') && $columnDiff->hasChanged('unsigned')) {
unset($diff->changedColumns[$oldColumnName]);
continue;
}
$fromColumnType = (string) $columnDiff->fromColumn->getType();
if ($fromColumnType === 'SmallInt' || $fromColumnType === 'BigInt') {
unset($diff->changedColumns[$oldColumnName]);
}
}
if ( ! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes) if ( ! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes)
|| ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes) || ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes)
|| ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes) || ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes)
......
...@@ -131,4 +131,44 @@ EOS ...@@ -131,4 +131,44 @@ EOS
$this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.'); $this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
$this->assertEquals(array('other_id', 'id'), array_map('strtolower', $tableIndexes['primary']->getColumns())); $this->assertEquals(array('other_id', 'id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
} }
/**
* @dataProvider getDiffListIntegerAutoincrementTableColumnsData
* @group DBAL-924
*/
public function testDiffListIntegerAutoincrementTableColumns($integerType, $unsigned, $expectedComparatorDiff)
{
$tableName = 'test_int_autoincrement_table';
$offlineTable = new \Doctrine\DBAL\Schema\Table($tableName);
$offlineTable->addColumn('id', $integerType, array('autoincrement' => true, 'unsigned' => $unsigned));
$offlineTable->setPrimaryKey(array('id'));
$this->_sm->dropAndCreateTable($offlineTable);
$onlineTable = $this->_sm->listTableDetails($tableName);
$comparator = new Schema\Comparator();
$diff = $comparator->diffTable($offlineTable, $onlineTable);
if ($expectedComparatorDiff) {
$this->assertEmpty($this->_sm->getDatabasePlatform()->getAlterTableSQL($diff));
} else {
$this->assertFalse($diff);
}
}
/**
* @return array
*/
public function getDiffListIntegerAutoincrementTableColumnsData()
{
return array(
array('smallint', false, true),
array('smallint', true, true),
array('integer', false, false),
array('integer', true, true),
array('bigint', false, true),
array('bigint', true, true),
);
}
} }
...@@ -73,6 +73,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -73,6 +73,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
/** /**
* @group DBAL-752 * @group DBAL-752
* @group DBAL-924
*/ */
public function testGeneratesTypeDeclarationForTinyIntegers() public function testGeneratesTypeDeclarationForTinyIntegers()
{ {
...@@ -81,11 +82,11 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -81,11 +82,11 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
$this->_platform->getTinyIntTypeDeclarationSQL(array()) $this->_platform->getTinyIntTypeDeclarationSQL(array())
); );
$this->assertEquals( $this->assertEquals(
'TINYINT', 'INTEGER',
$this->_platform->getTinyIntTypeDeclarationSQL(array('autoincrement' => true)) $this->_platform->getTinyIntTypeDeclarationSQL(array('autoincrement' => true))
); );
$this->assertEquals( $this->assertEquals(
'TINYINT', 'INTEGER',
$this->_platform->getTinyIntTypeDeclarationSQL( $this->_platform->getTinyIntTypeDeclarationSQL(
array('autoincrement' => true, 'primary' => true)) array('autoincrement' => true, 'primary' => true))
); );
...@@ -101,6 +102,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -101,6 +102,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
/** /**
* @group DBAL-752 * @group DBAL-752
* @group DBAL-924
*/ */
public function testGeneratesTypeDeclarationForSmallIntegers() public function testGeneratesTypeDeclarationForSmallIntegers()
{ {
...@@ -109,11 +111,15 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -109,11 +111,15 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
$this->_platform->getSmallIntTypeDeclarationSQL(array()) $this->_platform->getSmallIntTypeDeclarationSQL(array())
); );
$this->assertEquals( $this->assertEquals(
'SMALLINT', 'INTEGER',
$this->_platform->getSmallIntTypeDeclarationSQL(array('autoincrement' => true)) $this->_platform->getSmallIntTypeDeclarationSQL(array('autoincrement' => true))
); );
$this->assertEquals( $this->assertEquals(
'SMALLINT', 'INTEGER',
$this->_platform->getTinyIntTypeDeclarationSQL(array('autoincrement' => true, 'unsigned' => true))
);
$this->assertEquals(
'INTEGER',
$this->_platform->getSmallIntTypeDeclarationSQL( $this->_platform->getSmallIntTypeDeclarationSQL(
array('autoincrement' => true, 'primary' => true)) array('autoincrement' => true, 'primary' => true))
); );
...@@ -129,6 +135,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -129,6 +135,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
/** /**
* @group DBAL-752 * @group DBAL-752
* @group DBAL-924
*/ */
public function testGeneratesTypeDeclarationForMediumIntegers() public function testGeneratesTypeDeclarationForMediumIntegers()
{ {
...@@ -137,11 +144,15 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -137,11 +144,15 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
$this->_platform->getMediumIntTypeDeclarationSQL(array()) $this->_platform->getMediumIntTypeDeclarationSQL(array())
); );
$this->assertEquals( $this->assertEquals(
'MEDIUMINT', 'INTEGER',
$this->_platform->getMediumIntTypeDeclarationSQL(array('autoincrement' => true)) $this->_platform->getMediumIntTypeDeclarationSQL(array('autoincrement' => true))
); );
$this->assertEquals( $this->assertEquals(
'MEDIUMINT', 'INTEGER',
$this->_platform->getMediumIntTypeDeclarationSQL(array('autoincrement' => true, 'unsigned' => true))
);
$this->assertEquals(
'INTEGER',
$this->_platform->getMediumIntTypeDeclarationSQL( $this->_platform->getMediumIntTypeDeclarationSQL(
array('autoincrement' => true, 'primary' => true)) array('autoincrement' => true, 'primary' => true))
); );
...@@ -165,6 +176,10 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -165,6 +176,10 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'INTEGER', 'INTEGER',
$this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)) $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true))
); );
$this->assertEquals(
'INTEGER',
$this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true, 'unsigned' => true))
);
$this->assertEquals( $this->assertEquals(
'INTEGER', 'INTEGER',
$this->_platform->getIntegerTypeDeclarationSQL( $this->_platform->getIntegerTypeDeclarationSQL(
...@@ -182,6 +197,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -182,6 +197,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
/** /**
* @group DBAL-752 * @group DBAL-752
* @group DBAL-924
*/ */
public function testGeneratesTypeDeclarationForBigIntegers() public function testGeneratesTypeDeclarationForBigIntegers()
{ {
...@@ -190,11 +206,15 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -190,11 +206,15 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
$this->_platform->getBigIntTypeDeclarationSQL(array()) $this->_platform->getBigIntTypeDeclarationSQL(array())
); );
$this->assertEquals( $this->assertEquals(
'BIGINT', 'INTEGER',
$this->_platform->getBigIntTypeDeclarationSQL(array('autoincrement' => true)) $this->_platform->getBigIntTypeDeclarationSQL(array('autoincrement' => true))
); );
$this->assertEquals( $this->assertEquals(
'BIGINT', 'INTEGER',
$this->_platform->getBigIntTypeDeclarationSQL(array('autoincrement' => true, 'unsigned' => true))
);
$this->assertEquals(
'INTEGER',
$this->_platform->getBigIntTypeDeclarationSQL( $this->_platform->getBigIntTypeDeclarationSQL(
array('autoincrement' => true, 'primary' => true)) array('autoincrement' => true, 'primary' => true))
); );
......
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