Commit e7b76919 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #467 from deeky666/DBAL-472

[DBAL-472] Fix altering column from notnull to null in Oracle
parents 597fdfad 0782e925
...@@ -687,7 +687,7 @@ LEFT JOIN user_cons_columns r_cols ...@@ -687,7 +687,7 @@ LEFT JOIN user_cons_columns r_cols
$columnInfo['notnull'] = false; $columnInfo['notnull'] = false;
} }
$fields[] = $column->getQuotedName($this) . ' ' . $this->getColumnDeclarationSQL('', $columnInfo); $fields[] = $column->getQuotedName($this) . $this->getColumnDeclarationSQL('', $columnInfo);
} }
if ($columnHasChangedComment) { if ($columnHasChangedComment) {
...@@ -737,6 +737,31 @@ LEFT JOIN user_cons_columns r_cols ...@@ -737,6 +737,31 @@ LEFT JOIN user_cons_columns r_cols
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
} }
/**
* {@inheritdoc}
*/
public function getColumnDeclarationSQL($name, array $field)
{
if (isset($field['columnDefinition'])) {
$columnDef = $this->getCustomTypeDeclarationSQL($field);
} else {
$default = $this->getDefaultValueDeclarationSQL($field);
$notnull = empty($field['notnull']) ? ' NULL' : ' NOT NULL';
$unique = (isset($field['unique']) && $field['unique']) ?
' ' . $this->getUniqueFieldDeclarationSQL() : '';
$check = (isset($field['check']) && $field['check']) ?
' ' . $field['check'] : '';
$typeDecl = $field['type']->getSqlDeclaration($field, $this);
$columnDef = $typeDecl . $default . $notnull . $unique . $check;
}
return $name . ' ' . $columnDef;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -57,4 +57,35 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -57,4 +57,35 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType()); $this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType());
$this->assertFalse($table->getColumn('column_binary')->getFixed()); $this->assertFalse($table->getColumn('column_binary')->getFixed());
} }
/**
* @group DBAL-472
*/
public function testAlterTableColumnNotNull()
{
$comparator = new Schema\Comparator();
$tableName = 'list_table_column_notnull';
$table = new Schema\Table($tableName);
$table->addColumn('id', 'integer');
$table->addColumn('foo', 'integer');
$table->setPrimaryKey(array('id'));
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns($tableName);
$this->assertTrue($columns['id']->getNotnull());
$this->assertTrue($columns['foo']->getNotnull());
$diffTable = clone $table;
$diffTable->changeColumn('foo', array('notnull' => false));
$this->_sm->alterTable($comparator->diffTable($table, $diffTable));
$columns = $this->_sm->listTableColumns($tableName);
$this->assertTrue($columns['id']->getNotnull());
$this->assertFalse($columns['foo']->getNotnull());
}
} }
...@@ -63,13 +63,13 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -63,13 +63,13 @@ class OraclePlatformTest extends AbstractPlatformTestCase
public function getGenerateTableSql() public function getGenerateTableSql()
{ {
return 'CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) DEFAULT NULL, PRIMARY KEY(id))'; return 'CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) DEFAULT NULL NULL, PRIMARY KEY(id))';
} }
public function getGenerateTableWithMultiColumnUniqueIndexSql() public function getGenerateTableWithMultiColumnUniqueIndexSql()
{ {
return array( return array(
'CREATE TABLE test (foo VARCHAR2(255) DEFAULT NULL, bar VARCHAR2(255) DEFAULT NULL)', 'CREATE TABLE test (foo VARCHAR2(255) DEFAULT NULL NULL, bar VARCHAR2(255) DEFAULT NULL NULL)',
'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)', 'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)',
); );
} }
...@@ -77,7 +77,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -77,7 +77,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase
public function getGenerateAlterTableSql() public function getGenerateAlterTableSql()
{ {
return array( return array(
'ALTER TABLE mytable ADD (quota NUMBER(10) DEFAULT NULL)', 'ALTER TABLE mytable ADD (quota NUMBER(10) DEFAULT NULL NULL)',
"ALTER TABLE mytable MODIFY (baz VARCHAR2(255) DEFAULT 'def' NOT NULL, bloo NUMBER(1) DEFAULT '0' NOT NULL)", "ALTER TABLE mytable MODIFY (baz VARCHAR2(255) DEFAULT 'def' NOT NULL, bloo NUMBER(1) DEFAULT '0' NOT NULL)",
"ALTER TABLE mytable DROP (foo)", "ALTER TABLE mytable DROP (foo)",
"ALTER TABLE mytable RENAME TO userlist", "ALTER TABLE mytable RENAME TO userlist",
...@@ -326,9 +326,15 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -326,9 +326,15 @@ class OraclePlatformTest extends AbstractPlatformTestCase
), ),
array('type', 'notnull') array('type', 'notnull')
); );
$tableDiff->changedColumns['metar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
'metar', new \Doctrine\DBAL\Schema\Column(
'metar', \Doctrine\DBAL\Types\Type::getType('string'), array('length' => 2000, 'notnull' => false)
),
array('notnull')
);
$expectedSql = array( $expectedSql = array(
"ALTER TABLE mytable MODIFY (foo VARCHAR2(255) DEFAULT 'bla', baz VARCHAR2(255) DEFAULT 'bla' NOT NULL)", "ALTER TABLE mytable MODIFY (foo VARCHAR2(255) DEFAULT 'bla' NULL, baz VARCHAR2(255) DEFAULT 'bla' NOT NULL, metar VARCHAR2(2000) DEFAULT NULL NULL)",
); );
$this->assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff)); $this->assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
} }
......
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