From 34119d3925874e906a10cebf606826e0e281570e Mon Sep 17 00:00:00 2001 From: beberlei Date: Sun, 6 Dec 2009 13:00:49 +0000 Subject: [PATCH] [2.0] DDC-169 - Fix order that column and index/fk changes are applied in alter table. --- .../DBAL/Platforms/AbstractPlatform.php | 22 ++++++++++++------- lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php | 3 ++- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 3 ++- .../DBAL/Platforms/OraclePlatform.php | 4 +++- .../DBAL/Platforms/PostgreSqlPlatform.php | 4 +++- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 0c96fcd46..bc4809ba1 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -802,29 +802,35 @@ abstract class AbstractPlatform */ protected function _getAlterTableIndexForeignKeySql(TableDiff $diff) { + if ($diff->newName !== false) { + $tableName = $diff->newName; + } else { + $tableName = $diff->name; + } + $sql = array(); if ($this->supportsForeignKeyConstraints()) { foreach ($diff->addedForeignKeys AS $foreignKey) { - $sql[] = $this->getCreateForeignKeySql($foreignKey, $diff->name); + $sql[] = $this->getCreateForeignKeySql($foreignKey, $tableName); } foreach ($diff->removedForeignKeys AS $foreignKey) { - $sql[] = $this->getDropForeignKeySql($foreignKey, $diff->name); + $sql[] = $this->getDropForeignKeySql($foreignKey, $tableName); } foreach ($diff->changedForeignKeys AS $foreignKey) { - $sql[] = $this->getDropForeignKeySql($foreignKey, $diff->name); - $sql[] = $this->getCreateForeignKeySql($foreignKey, $diff->name); + $sql[] = $this->getDropForeignKeySql($foreignKey, $tableName); + $sql[] = $this->getCreateForeignKeySql($foreignKey, $tableName); } } foreach ($diff->addedIndexes AS $index) { - $sql[] = $this->getCreateIndexSql($index, $diff->name); + $sql[] = $this->getCreateIndexSql($index, $tableName); } foreach ($diff->removedIndexes AS $index) { - $sql[] = $this->getDropIndexSql($index, $diff->name); + $sql[] = $this->getDropIndexSql($index, $tableName); } foreach ($diff->changedIndexes AS $index) { - $sql[] = $this->getDropIndexSql($index, $diff->name); - $sql[] = $this->getCreateIndexSql($index, $diff->name); + $sql[] = $this->getDropIndexSql($index, $tableName); + $sql[] = $this->getCreateIndexSql($index, $tableName); } return $sql; diff --git a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php index 8885314b8..470d1c11b 100644 --- a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php @@ -125,10 +125,11 @@ class MsSqlPlatform extends AbstractPlatform . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - $sql = $this->_getAlterTableIndexForeignKeySql($diff); + $sql = array(); if (count($queryParts) > 0) { $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts); } + $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySql($diff)); return $sql; } diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 07152edd8..01caf2c8b 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -620,10 +620,11 @@ class MySqlPlatform extends AbstractPlatform . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); } - $sql = $this->_getAlterTableIndexForeignKeySql($diff); + $sql = array(); if (count($queryParts) > 0) { $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts); } + $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySql($diff)); return $sql; } diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index 5c3b7d1df..8c9a39e1f 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -477,7 +477,7 @@ END;'; */ public function getAlterTableSql(TableDiff $diff) { - $sql = $this->_getAlterTableIndexForeignKeySql($diff); + $sql = array(); $fields = array(); foreach ($diff->addedColumns AS $column) { @@ -512,6 +512,8 @@ END;'; $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; } + $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySql($diff)); + return $sql; } diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index f79adc0aa..369f9166d 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -493,7 +493,7 @@ class PostgreSqlPlatform extends AbstractPlatform */ public function getAlterTableSql(TableDiff $diff) { - $sql = $this->_getAlterTableIndexForeignKeySql($diff); + $sql = array(); foreach ($diff->addedColumns as $column) { $query = 'ADD ' . $this->getColumnDeclarationSql($column->getName(), $column->toArray()); @@ -534,6 +534,8 @@ class PostgreSqlPlatform extends AbstractPlatform $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; } + $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySql($diff)); + return $sql; }