Unverified Commit 288264d1 authored by Christopher Davis's avatar Christopher Davis Committed by Luís Cobucci

Add Tests Cases to Demonstrate #2906

Postgres has a `SERIAL` data type to allow shorthand for
`nextval('sequence_name')`, but DDL like this:

    CREATE TABLE example (id SERIAL)

Cannot be generated in the DBAL schema APIs.
parent 53af700a
......@@ -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