Unverified Commit 51bf0ae6 authored by Luís Cobucci's avatar Luís Cobucci Committed by GitHub

Merge pull request #2907 from chrisguitarguy/pg_serial_defaults

Don't Set a Default Value on PostgreSQL SERIAL Fields

Fixes: https://github.com/doctrine/dbal/issues/2906
parents e828bca2 15c2aee0
......@@ -26,7 +26,9 @@ use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Types\BigIntType;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\IntegerType;
/**
* PostgreSqlPlatform.
......@@ -1186,4 +1188,22 @@ class PostgreSqlPlatform extends AbstractPlatform
return parent::quoteStringLiteral($str);
}
/**
* {@inheritdoc}
*/
public function getDefaultValueDeclarationSQL($field)
{
if ($this->isSerialField($field)) {
return '';
}
return parent::getDefaultValueDeclarationSQL($field);
}
private function isSerialField(array $field) : bool
{
return $field['autoincrement'] ?? false === true && isset($field['type'])
&& ($field['type'] instanceof IntegerType || $field['type'] instanceof BigIntType);
}
}
......@@ -419,6 +419,50 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertEquals(-1.1, $columns['col_decimal']->getDefault());
self::assertEquals('(-1)', $columns['col_string']->getDefault());
}
public static function serialTypes() : array
{
return [
['integer'],
['bigint'],
];
}
/**
* @dataProvider serialTypes
* @group 2906
*/
public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(string $type) : void
{
$tableName = "test_serial_type_$type";
$table = new Schema\Table($tableName);
$table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false]);
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns($tableName);
self::assertNull($columns['id']->getDefault());
}
/**
* @dataProvider serialTypes
* @group 2906
*/
public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenWhenDefaultIsSet(string $type) : void
{
$tableName = "test_serial_type_with_default_$type";
$table = new Schema\Table($tableName);
$table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false, 'default' => 1]);
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns($tableName);
self::assertNull($columns['id']->getDefault());
}
}
class MoneyType extends Type
......
......@@ -144,6 +144,63 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
self::assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table));
}
public static function serialTypes() : array
{
return [
['integer', 'SERIAL'],
['bigint', 'BIGSERIAL'],
];
}
/**
* @dataProvider serialTypes
* @group 2906
*/
public function testGenerateTableWithAutoincrementDoesNotSetDefault(string $type, string $definition) : void
{
$table = new \Doctrine\DBAL\Schema\Table('autoinc_table_notnull');
$column = $table->addColumn('id', $type);
$column->setAutoIncrement(true);
$column->setNotNull(false);
$sql = $this->_platform->getCreateTableSQL($table);
self::assertEquals(["CREATE TABLE autoinc_table_notnull (id $definition)"], $sql);
}
/**
* @dataProvider serialTypes
* @group 2906
*/
public function testCreateTableWithAutoincrementAndNotNullAddsConstraint(string $type, string $definition) : void
{
$table = new \Doctrine\DBAL\Schema\Table('autoinc_table_notnull_enabled');
$column = $table->addColumn('id', $type);
$column->setAutoIncrement(true);
$column->setNotNull(true);
$sql = $this->_platform->getCreateTableSQL($table);
self::assertEquals(["CREATE TABLE autoinc_table_notnull_enabled (id $definition NOT NULL)"], $sql);
}
/**
* @dataProvider serialTypes
* @group 2906
*/
public function testGetDefaultValueDeclarationSQLIgnoresTheDefaultKeyWhenTheFieldIsSerial(string $type) : void
{
$sql = $this->_platform->getDefaultValueDeclarationSQL(
[
'autoincrement' => true,
'type' => Type::getType($type),
'default' => 1,
]
);
self::assertSame('', $sql);
}
public function testGeneratesTypeDeclarationForIntegers()
{
self::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