From dd6abf751ff78231175d302737bc7859bf693dab Mon Sep 17 00:00:00 2001 From: beberlei Date: Sun, 6 Dec 2009 09:47:54 +0000 Subject: [PATCH] [2.0] DDC-169 - AbstractPlatform::getAlterTableSql() now accepts only a TableDiff instance as argument across all platforms. --- .../DBAL/Platforms/AbstractPlatform.php | 11 +-- lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php | 90 +++++------------- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 92 ++++--------------- .../DBAL/Platforms/OraclePlatform.php | 75 ++++++--------- .../DBAL/Platforms/PostgreSqlPlatform.php | 91 +++++++----------- lib/Doctrine/DBAL/Schema/Column.php | 49 +++------- lib/Doctrine/DBAL/Schema/ColumnDiff.php | 5 +- lib/Doctrine/DBAL/Schema/Comparator.php | 3 +- .../Platforms/AbstractPlatformTestCase.php | 15 +-- .../Doctrine/Tests/DBAL/Schema/ColumnTest.php | 54 ++++++++--- 10 files changed, 172 insertions(+), 313 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 8ea03fc58..0c1504316 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -26,7 +26,8 @@ use Doctrine\DBAL\DBALException, Doctrine\DBAL\Types, Doctrine\DBAL\Schema\Table, Doctrine\DBAL\Schema\Index, - Doctrine\DBAL\Schema\ForeignKeyConstraint; + Doctrine\DBAL\Schema\ForeignKeyConstraint, + Doctrine\DBAL\Schema\TableDiff; /** * Base class for all DatabasePlatforms. The DatabasePlatforms are the central @@ -785,14 +786,10 @@ abstract class AbstractPlatform * * The method returns an array of sql statements, since some platforms need several statements. * - * @param string $name name of the table that is intended to be changed. - * @param array $changes associative array that contains the details of each type * - * @param boolean $check indicates whether the function should just check if the DBMS driver - * can perform the requested table alterations if the value is true or - * actually perform them otherwise. + * @param TableDiff $diff * @return array */ - public function getAlterTableSql($name, array $changes, $check = false) + public function getAlterTableSql(TableDiff $diff) { throw DBALException::notSupported(__METHOD__); } diff --git a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php index 6ea3d9d4d..2b8a4f7fb 100644 --- a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php @@ -21,6 +21,8 @@ namespace Doctrine\DBAL\Platforms; +use \Doctrine\DBAL\Schema\TableDiff; + use Doctrine\Common\DoctrineException; /** @@ -93,91 +95,41 @@ class MsSqlPlatform extends AbstractPlatform * * The method returns an array of sql statements, since some platforms need several statements. * - * @param string $name name of the table that is intended to be changed. - * @param array $changes associative array that contains the details of each type * - * @param boolean $check indicates whether the function should just check if the DBMS driver - * can perform the requested table alterations if the value is true or - * actually perform them otherwise. + * @param TableDiff $diff * @return array */ - public function getAlterTableSql($name, array $changes, $check = false) + public function getAlterTableSql(TableDiff $diff) { - foreach ($changes as $changeName => $change) { - switch ($changeName) { - case 'add': - case 'remove': - case 'change': - case 'rename': - case 'name': - break; - default: - throw \Doctrine\Common\DoctrineException::alterTableChangeNotSupported($changeName); - } + $queryParts = array(); + if ($diff->newName !== false) { + $queryParts[] = 'RENAME TO ' . $diff->newName; } - $query = ''; - if ( ! empty($changes['name'])) { - $change_name = $changes['name']; - $query .= 'RENAME TO ' . $change_name; + foreach ($diff->addedColumns AS $fieldName => $column) { + $queryParts[] = 'ADD ' . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - if ( ! empty($changes['add']) && is_array($changes['add'])) { - foreach ($changes['add'] as $fieldName => $field) { - if ($query) { - $query .= ', '; - } - $query .= 'ADD ' . $this->getColumnDeclarationSql($fieldName, $field); - } + foreach ($diff->removedColumns AS $column) { + $queryParts[] = 'DROP ' . $column->getName(); } - if ( ! empty($changes['remove']) && is_array($changes['remove'])) { - foreach ($changes['remove'] as $fieldName => $field) { - if ($query) { - $query .= ', '; - } - $query .= 'DROP COLUMN ' . $fieldName; - } + foreach ($diff->changedColumns AS $columnDiff) { + /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ + $column = $columnDiff->column; + $queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' ' + . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - $rename = array(); - if ( ! empty($changes['rename']) && is_array($changes['rename'])) { - foreach ($changes['rename'] as $fieldName => $field) { - $rename[$field['name']] = $fieldName; - } + foreach ($diff->renamedColumns AS $oldColumnName => $column) { + $queryParts[] = 'CHANGE ' . $oldColumnName . ' ' + . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - if ( ! empty($changes['change']) && is_array($changes['change'])) { - foreach ($changes['change'] as $fieldName => $field) { - if ($query) { - $query.= ', '; - } - if (isset($rename[$fieldName])) { - $oldFieldName = $rename[$fieldName]; - unset($rename[$fieldName]); - } else { - $oldFieldName = $fieldName; - } - $query .= 'CHANGE ' . $oldFieldName . ' ' - . $this->getColumnDeclarationSql($fieldName, $field['definition']); - } - } - - if ( ! empty($rename) && is_array($rename)) { - foreach ($rename as $renameName => $renamedField) { - if ($query) { - $query.= ', '; - } - $field = $changes['rename'][$renamedField]; - $query .= 'CHANGE ' . $renamedField . ' ' - . $this->getColumnDeclarationSql($field['name'], $field['definition']); - } - } - - if ( ! $query) { + if (count($queryParts) == 0) { return false; } - return array('ALTER TABLE ' . $name . ' ' . $query); + return array('ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts)); } /** diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 977afdbd3..bebea7d83 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -21,7 +21,8 @@ namespace Doctrine\DBAL\Platforms; -use Doctrine\Common\DoctrineException; +use Doctrine\Common\DoctrineException, + Doctrine\DBAL\Schema\TableDiff; /** * The MySqlPlatform provides the behavior, features and SQL dialect of the @@ -592,91 +593,38 @@ class MySqlPlatform extends AbstractPlatform * @return boolean * @override */ - public function getAlterTableSql($name, array $changes, $check = false) + public function getAlterTableSql(TableDiff $diff) { - if ( ! $name) { - throw DoctrineException::missingTableName(); - } - - foreach ($changes as $changeName => $change) { - switch ($changeName) { - case 'add': - case 'remove': - case 'change': - case 'rename': - case 'name': - break; - default: - throw \Doctrine\Common\DoctrineException::alterTableChangeNotSupported($changeName); - } + $queryParts = array(); + if ($diff->newName !== false) { + $queryParts[] = 'RENAME TO ' . $diff->newName; } - if ($check) { - return true; + foreach ($diff->addedColumns AS $fieldName => $column) { + $queryParts[] = 'ADD ' . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - $query = ''; - if ( ! empty($changes['name'])) { - $query .= 'RENAME TO ' . $changes['name']; + foreach ($diff->removedColumns AS $column) { + $queryParts[] = 'DROP ' . $column->getName(); } - if ( ! empty($changes['add']) && is_array($changes['add'])) { - foreach ($changes['add'] as $fieldName => $field) { - if ($query) { - $query.= ', '; - } - $query.= 'ADD ' . $this->getColumnDeclarationSql($fieldName, $field); - } + foreach ($diff->changedColumns AS $columnDiff) { + /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ + $column = $columnDiff->column; + $queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' ' + . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - if ( ! empty($changes['remove']) && is_array($changes['remove'])) { - foreach ($changes['remove'] as $fieldName => $field) { - if ($query) { - $query .= ', '; - } - $query .= 'DROP ' . $fieldName; - } + foreach ($diff->renamedColumns AS $oldColumnName => $column) { + $queryParts[] = 'CHANGE ' . $oldColumnName . ' ' + . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - $rename = array(); - if ( ! empty($changes['rename']) && is_array($changes['rename'])) { - foreach ($changes['rename'] as $fieldName => $field) { - $rename[$field['name']] = $fieldName; - } - } - - if ( ! empty($changes['change']) && is_array($changes['change'])) { - foreach ($changes['change'] as $fieldName => $field) { - if ($query) { - $query.= ', '; - } - if (isset($rename[$fieldName])) { - $oldFieldName = $rename[$fieldName]; - unset($rename[$fieldName]); - } else { - $oldFieldName = $fieldName; - } - $query .= 'CHANGE ' . $oldFieldName . ' ' - . $this->getColumnDeclarationSql($fieldName, $field['definition']); - } - } - - if ( ! empty($rename) && is_array($rename)) { - foreach ($rename as $renameName => $renamedField) { - if ($query) { - $query.= ', '; - } - $field = $changes['rename'][$renamedField]; - $query .= 'CHANGE ' . $renamedField . ' ' - . $this->getColumnDeclarationSql($field['name'], $field['definition']); - } - } - - if ( ! $query) { + if (count($queryParts) == 0) { return false; } - return array('ALTER TABLE ' . $name . ' ' . $query); + return array('ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts)); } /** diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index a590b36d8..e89af5141 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -21,6 +21,8 @@ namespace Doctrine\DBAL\Platforms; +use Doctrine\DBAL\Schema\TableDiff; + /** * OraclePlatform. * @@ -466,69 +468,48 @@ END;'; * * The method returns an array of sql statements, since some platforms need several statements. * - * @param string $name name of the table that is intended to be changed. + * @param string $diff->name name of the table that is intended to be changed. * @param array $changes associative array that contains the details of each type * * @param boolean $check indicates whether the function should just check if the DBMS driver * can perform the requested table alterations if the value is true or * actually perform them otherwise. * @return array */ - public function getAlterTableSql($name, array $changes, $check = false) + public function getAlterTableSql(TableDiff $diff) { - if ( ! $name) { - throw DoctrineException::missingTableName(); + $sql = array(); + + $fields = array(); + foreach ($diff->addedColumns AS $column) { + $fields[] = $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - foreach ($changes as $changeName => $change) { - switch ($changeName) { - case 'add': - case 'remove': - case 'change': - case 'name': - case 'rename': - break; - default: - throw \Doctrine\Common\DoctrineException::alterTableChangeNotSupported($changeName); - } + if (count($fields)) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')'; } - if ($check) { - return false; + $fields = array(); + foreach ($diff->changedColumns AS $columnDiff) { + $column = $columnDiff->column; + $fields[] = $column->getName(). ' ' . $this->getColumnDeclarationSql('', $column->toArray()); + } + if (count($fields)) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')'; } - if ( ! empty($changes['add']) && is_array($changes['add'])) { - $fields = array(); - foreach ($changes['add'] as $fieldName => $field) { - $fields[] = $this->getColumnDeclarationSql($fieldName, $field); - } - $sql[] = 'ALTER TABLE ' . $name . ' ADD (' . implode(', ', $fields) . ')'; + foreach ($diff->renamedColumns AS $oldColumnName => $column) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getName(); } - if ( ! empty($changes['change']) && is_array($changes['change'])) { - $fields = array(); - foreach ($changes['change'] as $fieldName => $field) { - $fields[] = $fieldName. ' ' . $this->getColumnDeclarationSql('', $field['definition']); - } - $sql[] = 'ALTER TABLE ' . $name . ' MODIFY (' . implode(', ', $fields) . ')'; + $fields = array(); + foreach ($diff->removedColumns AS $column) { + $fields[] = $column->getName(); + } + if (count($fields)) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' DROP COLUMN ' . implode(', ', $fields); } - if ( ! empty($changes['rename']) && is_array($changes['rename'])) { - foreach ($changes['rename'] as $fieldName => $field) { - $sql[] = 'ALTER TABLE ' . $name . ' RENAME COLUMN ' . $fieldName - . ' TO ' . $field['name']; - } - } - - if ( ! empty($changes['remove']) && is_array($changes['remove'])) { - $fields = array(); - foreach ($changes['remove'] as $fieldName => $field) { - $fields[] = $fieldName; - } - $sql[] = 'ALTER TABLE ' . $name . ' DROP COLUMN ' . implode(', ', $fields); - } - - if ( ! empty($changes['name'])) { - $changeName = $changes['name']; - $sql[] = 'ALTER TABLE ' . $name . ' RENAME TO ' . $changeName; + if ($diff->newName !== false) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; } return $sql; diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 04ec30aec..ad8d99f30 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -21,6 +21,8 @@ namespace Doctrine\DBAL\Platforms; +use Doctrine\DBAL\Schema\TableDiff; + /** * PostgreSqlPlatform. * @@ -489,70 +491,47 @@ class PostgreSqlPlatform extends AbstractPlatform * @return array * @override */ - public function getAlterTableSql($name, array $changes, $check = false) + public function getAlterTableSql(TableDiff $diff) { - foreach ($changes as $changeName => $change) { - switch ($changeName) { - case 'add': - case 'remove': - case 'change': - case 'name': - case 'rename': - break; - default: - throw DoctrineException::alterTableChangeNotSupported($changeName); - } - } - - if ($check) { - return true; - } - $sql = array(); - if (isset($changes['add']) && is_array($changes['add'])) { - foreach ($changes['add'] as $fieldName => $field) { - $query = 'ADD ' . $this->getColumnDeclarationSql($fieldName, $field); - $sql[] = 'ALTER TABLE ' . $name . ' ' . $query; + foreach ($diff->addedColumns as $column) { + $query = 'ADD ' . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); + $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; + } + + foreach ($diff->removedColumns as $column) { + $query = 'DROP ' . $column->getName(); + $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; + } + + foreach ($diff->changedColumns AS $columnDiff) { + $oldColumnName = $columnDiff->oldColumnName; + $column = $columnDiff->column; + + if ($columnDiff->hasChanged('type')) { + $type = $column->getType(); + + // here was a server version check before, but DBAL API does not support this anymore. + $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this); + $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; + } + if ($columnDiff->hasChanged('default')) { + $query = 'ALTER ' . $oldColumnName . ' SET DEFAULT ' . $column->getDefault(); + $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; + } + if ($columnDiff->hasChanged('notnull')) { + $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL'; + $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; } } - if (isset($changes['remove']) && is_array($changes['remove'])) { - foreach ($changes['remove'] as $fieldName => $field) { - $query = 'DROP ' . $fieldName; - $sql[] = 'ALTER TABLE ' . $name . ' ' . $query; - } + foreach ($diff->renamedColumns as $oldColumnName => $column) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getName(); } - if (isset($changes['change']) && is_array($changes['change'])) { - foreach ($changes['change'] as $fieldName => $field) { - if (isset($field['definition']['type']) && $field['definition']['type'] instanceof \Doctrine\DBAL\Types\Type) { - $type = $field['definition']['type']; - - // here was a server version check before, but DBAL API does not support this anymore. - $query = 'ALTER ' . $fieldName . ' TYPE ' . $type->getSqlDeclaration($field['definition'], $this); - $sql[] = 'ALTER TABLE ' . $name . ' ' . $query; - } - if (isset($field['definition']['default'])) { - $query = 'ALTER ' . $fieldName . ' SET DEFAULT ' . $field['definition']['default']; - $sql[] = 'ALTER TABLE ' . $name . ' ' . $query; - } - if (isset($field['definition']['notnull']) && is_bool($field['definition']['notnull'])) { - $query = 'ALTER ' . $fieldName . ' ' . ($field['definition']['notnull'] ? 'SET' : 'DROP') . ' NOT NULL'; - $sql[] = 'ALTER TABLE ' . $name . ' ' . $query; - } - - } - } - - if (isset($changes['rename']) && is_array($changes['rename'])) { - foreach ($changes['rename'] as $fieldName => $field) { - $sql[] = 'ALTER TABLE ' . $name . ' RENAME COLUMN ' . $fieldName . ' TO ' . $field['name']; - } - } - - if (isset($changes['name'])) { - $sql[] = 'ALTER TABLE ' . $name . ' RENAME TO ' . $changes['name']; + if ($diff->newName !== false) { + $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; } return $sql; diff --git a/lib/Doctrine/DBAL/Schema/Column.php b/lib/Doctrine/DBAL/Schema/Column.php index c2730d7c8..23d44e9ba 100644 --- a/lib/Doctrine/DBAL/Schema/Column.php +++ b/lib/Doctrine/DBAL/Schema/Column.php @@ -33,7 +33,7 @@ use Doctrine\DBAL\Schema\Visitor\Visitor; * @version $Revision$ * @author Benjamin Eberlei */ -class Column extends AbstractAsset implements \ArrayAccess +class Column extends AbstractAsset { /** * @var \Doctrine\DBAL\Types\Type @@ -290,41 +290,20 @@ class Column extends AbstractAsset implements \ArrayAccess } /** - * @param string $option - * @return mixed + * @return array */ - public function offsetExists($option) + public function toArray() { - return (\property_exists($this, "_".$option) || $this->hasPlatformOption($option)); - } - - /** - * @param string $option - * @return mixed - */ - public function offsetGet($option) - { - $optionAccessor = "_".$option; - if(\property_exists($this, $optionAccessor)) { - return $this->$optionAccessor; - } else if($this->hasPlatformOption($option)) { - return $this->getPlatformOption($option); - } else { - return false; - } - } - - public function offsetSet($offset, $value) - { - throw new \BadMethodCallException( - "Setting column property ".$this->_name."::".$offset." through the ArrayAccess interface is not allowed." - ); - } - - public function offsetUnset($offset) - { - throw new \BadMethodCallException( - "Unsetting column property ".$this->_name."::".$offset." through the ArrayAccess interface is not allowed." - ); + return array_merge(array( + 'name' => $this->_name, + 'type' => $this->_type, + 'default' => $this->_default, + 'notnull' => $this->_notnull, + 'length' => $this->_length, + 'precision' => $this->_precision, + 'scale' => $this->_scale, + 'fixed' => $this->_fixed, + 'unsigned' => $this->_unsigned, + ), $this->_platformOptions); } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Schema/ColumnDiff.php b/lib/Doctrine/DBAL/Schema/ColumnDiff.php index 29caab370..4cf896901 100644 --- a/lib/Doctrine/DBAL/Schema/ColumnDiff.php +++ b/lib/Doctrine/DBAL/Schema/ColumnDiff.php @@ -32,6 +32,8 @@ namespace Doctrine\DBAL\Schema; */ class ColumnDiff { + public $oldColumnName; + /** * @var Column */ @@ -42,8 +44,9 @@ class ColumnDiff */ public $changedProperties = array(); - public function __construct(Column $column, array $changedProperties = array()) + public function __construct($oldColumnName, Column $column, array $changedProperties = array()) { + $this->oldColumnName = $oldColumnName; $this->column = $column; $this->changedProperties = $changedProperties; } diff --git a/lib/Doctrine/DBAL/Schema/Comparator.php b/lib/Doctrine/DBAL/Schema/Comparator.php index 88206371c..da5eaa9bb 100644 --- a/lib/Doctrine/DBAL/Schema/Comparator.php +++ b/lib/Doctrine/DBAL/Schema/Comparator.php @@ -161,12 +161,11 @@ class Comparator $changes++; } } - /* See if there are any changed fieldDefinitioninitions */ foreach ( $table1Columns as $columnName => $column ) { if ( $table2->hasColumn($columnName) ) { $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) ); if (count($changedProperties) ) { - $columnDiff = new ColumnDiff($table2->getColumn($columnName), $changedProperties); + $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); $tableDifferences->changedColumns[$column->getName()] = $columnDiff; $changes++; } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php index 7e3e671cc..f7776ae85 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php @@ -95,18 +95,13 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase { $expectedSql = $this->getGenerateAlterTableSql(); - $changes = array( - 'name' => 'userlist', - 'add' => array( - 'quota' => array( - 'type' => \Doctrine\DBAL\Types\Type::getType('integer'), - 'notnull' => false, - ) - )); + $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable'); + $tableDiff->newName = 'userlist'; + $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false)); - $sql = $this->_platform->getAlterTableSql('mytable', $changes); + $sql = $this->_platform->getAlterTableSql($tableDiff); - $this->assertEquals(count($sql), count($expectedSql), "Expecting the same number of sql queries for alter table failed."); + $this->assertEquals(count($expectedSql), count($sql), "Expecting the same number of sql queries for alter table failed."); for ($i = 0; $i < count($expectedSql); $i++) { $this->assertEquals($expectedSql[$i], $sql[$i], $i."th query of alter table does not match."); } diff --git a/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php b/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php index fe748dc43..931dace4d 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php @@ -13,22 +13,10 @@ class ColumnTest extends \PHPUnit_Framework_TestCase { public function testGet() { - $options = array( - 'length' => 200, - 'precision' => 5, - 'scale' => 2, - 'unsigned' => true, - 'notnull' => false, - 'fixed' => true, - 'default' => 'baz', - 'platformOptions' => array('foo' => 'bar'), - ); - - $string = Type::getType('string'); - $column = new Column("foo", $string, $options); + $column = $this->createColumn(); $this->assertEquals("foo", $column->getName()); - $this->assertSame($string, $column->getType()); + $this->assertSame(Type::getType('string'), $column->getType()); $this->assertEquals(200, $column->getLength()); $this->assertEquals(5, $column->getPrecision()); @@ -43,4 +31,42 @@ class ColumnTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar', $column->getPlatformOption('foo')); $this->assertFalse($column->hasPlatformOption('bar')); } + + public function testToArray() + { + $expected = array( + 'name' => 'foo', + 'type' => Type::getType('string'), + 'default' => 'baz', + 'notnull' => false, + 'length' => 200, + 'precision' => 5, + 'scale' => 2, + 'fixed' => true, + 'unsigned' => true, + 'foo' => 'bar', + ); + + $this->assertEquals($expected, $this->createColumn()->toArray()); + } + + /** + * @return Column + */ + public function createColumn() + { + $options = array( + 'length' => 200, + 'precision' => 5, + 'scale' => 2, + 'unsigned' => true, + 'notnull' => false, + 'fixed' => true, + 'default' => 'baz', + 'platformOptions' => array('foo' => 'bar'), + ); + + $string = Type::getType('string'); + return new Column("foo", $string, $options); + } } \ No newline at end of file