Unverified Commit fe824157 authored by Luís Cobucci's avatar Luís Cobucci

Merge branch 'pg_serial_defaults' into 2.6

Backporting https://github.com/doctrine/dbal/pull/2907
parents 53af700a 79e6d130
......@@ -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
$this->assertEquals(-1.1, $columns['col_decimal']->getDefault());
$this->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
$this->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()
{
$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