Commit 009c5b3a authored by Steve Müller's avatar Steve Müller

fix explicitly quoted table identifiers in ALTER TABLE statements

parent a43ccc5a
...@@ -2016,7 +2016,7 @@ abstract class AbstractPlatform ...@@ -2016,7 +2016,7 @@ abstract class AbstractPlatform
*/ */
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{ {
$tableName = $diff->getName()->getQuotedName($this); $tableName = $diff->getName($this)->getQuotedName($this);
$sql = array(); $sql = array();
if ($this->supportsForeignKeyConstraints()) { if ($this->supportsForeignKeyConstraints()) {
...@@ -2047,7 +2047,7 @@ abstract class AbstractPlatform ...@@ -2047,7 +2047,7 @@ abstract class AbstractPlatform
{ {
$tableName = (false !== $diff->newName) $tableName = (false !== $diff->newName)
? $diff->getNewName()->getQuotedName($this) ? $diff->getNewName()->getQuotedName($this)
: $diff->getName()->getQuotedName($this); : $diff->getName($this)->getQuotedName($this);
$sql = array(); $sql = array();
......
...@@ -519,12 +519,16 @@ class DB2Platform extends AbstractPlatform ...@@ -519,12 +519,16 @@ class DB2Platform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) { if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(" ", $queryParts); $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(" ", $queryParts);
} }
// Some table alteration operations require a table reorganization. // Some table alteration operations require a table reorganization.
if ( ! empty($diff->removedColumns) || ! empty($diff->changedColumns)) { if ( ! empty($diff->removedColumns) || ! empty($diff->changedColumns)) {
$sql[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $diff->getName()->getQuotedName($this) . "')"; $sql[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $diff->getName($this)->getQuotedName($this) . "')";
}
if ($diff->newName !== false) {
$sql[] = 'RENAME TABLE ' . $diff->getName($this)->getQuotedName($this) . ' TO ' . $diff->getNewName()->getQuotedName($this);
} }
$sql = array_merge( $sql = array_merge(
...@@ -532,10 +536,6 @@ class DB2Platform extends AbstractPlatform ...@@ -532,10 +536,6 @@ class DB2Platform extends AbstractPlatform
$sql, $sql,
$this->getPostAlterTableIndexForeignKeySQL($diff) $this->getPostAlterTableIndexForeignKeySQL($diff)
); );
if ($diff->newName !== false) {
$sql[] = 'RENAME TABLE ' . $diff->getName()->getQuotedName($this) . ' TO ' . $diff->getNewName()->getQuotedName($this);
}
} }
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
...@@ -547,7 +547,7 @@ class DB2Platform extends AbstractPlatform ...@@ -547,7 +547,7 @@ class DB2Platform extends AbstractPlatform
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{ {
$sql = array(); $sql = array();
$table = $diff->getName()->getQuotedName($this); $table = $diff->getName($this)->getQuotedName($this);
foreach ($diff->removedIndexes as $remKey => $remIndex) { foreach ($diff->removedIndexes as $remKey => $remIndex) {
foreach ($diff->addedIndexes as $addKey => $addIndex) { foreach ($diff->addedIndexes as $addKey => $addIndex) {
......
...@@ -549,7 +549,7 @@ class DrizzlePlatform extends AbstractPlatform ...@@ -549,7 +549,7 @@ class DrizzlePlatform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) { if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(", ", $queryParts); $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(", ", $queryParts);
} }
$sql = array_merge( $sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPreAlterTableIndexForeignKeySQL($diff),
......
...@@ -593,7 +593,7 @@ class MySqlPlatform extends AbstractPlatform ...@@ -593,7 +593,7 @@ class MySqlPlatform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) { if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . implode(", ", $queryParts); $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(", ", $queryParts);
} }
$sql = array_merge( $sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPreAlterTableIndexForeignKeySQL($diff),
...@@ -611,7 +611,7 @@ class MySqlPlatform extends AbstractPlatform ...@@ -611,7 +611,7 @@ class MySqlPlatform extends AbstractPlatform
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
{ {
$sql = array(); $sql = array();
$table = $diff->getName()->getQuotedName($this); $table = $diff->getName($this)->getQuotedName($this);
foreach ($diff->removedIndexes as $remKey => $remIndex) { foreach ($diff->removedIndexes as $remKey => $remIndex) {
// Dropping primary keys requires to unset autoincrement attribute on the particular column first. // Dropping primary keys requires to unset autoincrement attribute on the particular column first.
......
...@@ -702,7 +702,7 @@ LEFT JOIN user_cons_columns r_cols ...@@ -702,7 +702,7 @@ LEFT JOIN user_cons_columns r_cols
} }
if (count($fields)) { if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')'; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')';
} }
$fields = array(); $fields = array();
...@@ -749,7 +749,7 @@ LEFT JOIN user_cons_columns r_cols ...@@ -749,7 +749,7 @@ LEFT JOIN user_cons_columns r_cols
} }
if (count($fields)) { if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')'; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')';
} }
foreach ($diff->renamedColumns as $oldColumnName => $column) { foreach ($diff->renamedColumns as $oldColumnName => $column) {
...@@ -759,7 +759,7 @@ LEFT JOIN user_cons_columns r_cols ...@@ -759,7 +759,7 @@ LEFT JOIN user_cons_columns r_cols
$oldColumnName = new Identifier($oldColumnName); $oldColumnName = new Identifier($oldColumnName);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .' TO ' . $column->getQuotedName($this); ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .' TO ' . $column->getQuotedName($this);
} }
...@@ -773,17 +773,23 @@ LEFT JOIN user_cons_columns r_cols ...@@ -773,17 +773,23 @@ LEFT JOIN user_cons_columns r_cols
} }
if (count($fields)) { if (count($fields)) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' DROP (' . implode(', ', $fields).')'; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' DROP (' . implode(', ', $fields).')';
} }
$tableSql = array(); $tableSql = array();
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) { if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this); $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL); $sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$this->getPostAlterTableIndexForeignKeySQL($diff)
);
} }
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
......
...@@ -453,7 +453,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -453,7 +453,7 @@ class PostgreSqlPlatform extends AbstractPlatform
} }
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
$comment = $this->getColumnComment($column); $comment = $this->getColumnComment($column);
...@@ -468,7 +468,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -468,7 +468,7 @@ class PostgreSqlPlatform extends AbstractPlatform
} }
$query = 'DROP ' . $column->getQuotedName($this); $query = 'DROP ' . $column->getQuotedName($this);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} }
foreach ($diff->changedColumns as $columnDiff) { foreach ($diff->changedColumns as $columnDiff) {
...@@ -489,7 +489,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -489,7 +489,7 @@ class PostgreSqlPlatform extends AbstractPlatform
// here was a server version check before, but DBAL API does not support this anymore. // here was a server version check before, but DBAL API does not support this anymore.
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this); $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} }
if ($columnDiff->hasChanged('default') || $columnDiff->hasChanged('type')) { if ($columnDiff->hasChanged('default') || $columnDiff->hasChanged('type')) {
...@@ -497,12 +497,12 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -497,12 +497,12 @@ class PostgreSqlPlatform extends AbstractPlatform
? ' DROP DEFAULT' ? ' DROP DEFAULT'
: ' SET' . $this->getDefaultValueDeclarationSQL($column->toArray()); : ' SET' . $this->getDefaultValueDeclarationSQL($column->toArray());
$query = 'ALTER ' . $oldColumnName . $defaultClause; $query = 'ALTER ' . $oldColumnName . $defaultClause;
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} }
if ($columnDiff->hasChanged('notnull')) { if ($columnDiff->hasChanged('notnull')) {
$query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL'; $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} }
if ($columnDiff->hasChanged('autoincrement')) { if ($columnDiff->hasChanged('autoincrement')) {
...@@ -511,13 +511,13 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -511,13 +511,13 @@ class PostgreSqlPlatform extends AbstractPlatform
$seqName = $this->getIdentitySequenceName($diff->name, $oldColumnName); $seqName = $this->getIdentitySequenceName($diff->name, $oldColumnName);
$sql[] = "CREATE SEQUENCE " . $seqName; $sql[] = "CREATE SEQUENCE " . $seqName;
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->getName()->getQuotedName($this) . "))"; $sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->getName($this)->getQuotedName($this) . "))";
$query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')"; $query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
$sql[] = "ALTER TABLE " . $diff->getName()->getQuotedName($this) . " " . $query; $sql[] = "ALTER TABLE " . $diff->getName($this)->getQuotedName($this) . " " . $query;
} else { } else {
// Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have // Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
$query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT"; $query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
$sql[] = "ALTER TABLE " . $diff->getName()->getQuotedName($this) . " " . $query; $sql[] = "ALTER TABLE " . $diff->getName($this)->getQuotedName($this) . " " . $query;
} }
} }
...@@ -531,7 +531,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -531,7 +531,7 @@ class PostgreSqlPlatform extends AbstractPlatform
if ($columnDiff->hasChanged('length')) { if ($columnDiff->hasChanged('length')) {
$query = 'ALTER ' . $oldColumnName . ' TYPE ' . $column->getType()->getSqlDeclaration($column->toArray(), $this); $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $column->getType()->getSqlDeclaration($column->toArray(), $this);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} }
} }
...@@ -542,18 +542,24 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -542,18 +542,24 @@ class PostgreSqlPlatform extends AbstractPlatform
$oldColumnName = new Identifier($oldColumnName); $oldColumnName = new Identifier($oldColumnName);
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this); ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);
} }
$tableSql = array(); $tableSql = array();
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) { if ($diff->newName !== false) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this); $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
} }
$sql = array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $sql, $this->getPostAlterTableIndexForeignKeySQL($diff), $commentsSQL); $sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$this->getPostAlterTableIndexForeignKeySQL($diff)
);
} }
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
......
...@@ -185,21 +185,27 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -185,21 +185,27 @@ class SQLAnywherePlatform extends AbstractPlatform
continue; continue;
} }
$sql[] = $this->getAlterTableClause($diff->getName()) . ' ' . $sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' .
$this->getAlterTableRenameColumnClause($oldColumnName, $column); $this->getAlterTableRenameColumnClause($oldColumnName, $column);
} }
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
if ( ! empty($alterClauses)) { if ( ! empty($alterClauses)) {
$sql[] = $this->getAlterTableClause($diff->getName()) . ' ' . implode(", ", $alterClauses); $sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' . implode(", ", $alterClauses);
} }
$sql = array_merge($sql, $commentsSQL);
if ($diff->newName !== false) { if ($diff->newName !== false) {
$sql[] = $this->getAlterTableClause($diff->getName()) . ' ' . $sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' .
$this->getAlterTableRenameTableClause($diff->getNewName()); $this->getAlterTableRenameTableClause($diff->getNewName());
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL); $sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$this->getPostAlterTableIndexForeignKeySQL($diff)
);
} }
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
......
...@@ -501,7 +501,8 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -501,7 +501,8 @@ class SQLServerPlatform extends AbstractPlatform
$oldColumnName = new Identifier($oldColumnName); $oldColumnName = new Identifier($oldColumnName);
$sql[] = "sp_RENAME '" . $diff->name . "." . $oldColumnName->getQuotedName($this) . $sql[] = "sp_RENAME '" .
$diff->getName($this)->getQuotedName($this) . "." . $oldColumnName->getQuotedName($this) .
"', '" . $column->getQuotedName($this) . "', 'COLUMN'"; "', '" . $column->getQuotedName($this) . "', 'COLUMN'";
// Recreate default constraint with new column name if necessary (for future reference). // Recreate default constraint with new column name if necessary (for future reference).
...@@ -521,13 +522,13 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -521,13 +522,13 @@ class SQLServerPlatform extends AbstractPlatform
} }
foreach ($queryParts as $query) { foreach ($queryParts as $query) {
$sql[] = 'ALTER TABLE ' . $diff->getName()->getQuotedName($this) . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . $query;
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSql); $sql = array_merge($sql, $commentsSql);
if ($diff->newName !== false) { if ($diff->newName !== false) {
$sql[] = "sp_RENAME '" . $diff->getName()->getQuotedName($this) . "', '" . $diff->getNewName()->getQuotedName($this) . "'"; $sql[] = "sp_RENAME '" . $diff->getName($this)->getQuotedName($this) . "', '" . $diff->getNewName()->getName() . "'";
/** /**
* Rename table's default constraints names * Rename table's default constraints names
...@@ -543,10 +544,16 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -543,10 +544,16 @@ class SQLServerPlatform extends AbstractPlatform
"'" . $this->generateIdentifierName($diff->newName) . "') + ''', ''OBJECT'';' " . "'" . $this->generateIdentifierName($diff->newName) . "') + ''', ''OBJECT'';' " .
"FROM sys.default_constraints dc " . "FROM sys.default_constraints dc " .
"JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " . "JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " .
"WHERE tbl.name = '" . $diff->getNewName()->getQuotedName($this) . "';" . "WHERE tbl.name = '" . $diff->getNewName()->getName() . "';" .
"EXEC sp_executesql @sql"; "EXEC sp_executesql @sql";
} }
$sql = array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$this->getPostAlterTableIndexForeignKeySQL($diff)
);
return array_merge($sql, $tableSql, $columnSql); return array_merge($sql, $tableSql, $columnSql);
} }
......
...@@ -645,13 +645,13 @@ class SqlitePlatform extends AbstractPlatform ...@@ -645,13 +645,13 @@ class SqlitePlatform extends AbstractPlatform
} }
$sql = array(); $sql = array();
$tableName = $diff->newName ?: $diff->name; $tableName = $diff->newName ? $diff->getNewName(): $diff->getName($this);
foreach ($this->getIndexesInAlteredTable($diff) as $index) { foreach ($this->getIndexesInAlteredTable($diff) as $index) {
if ($index->isPrimary()) { if ($index->isPrimary()) {
continue; continue;
} }
$sql[] = $this->getCreateIndexSQL($index, $tableName); $sql[] = $this->getCreateIndexSQL($index, $tableName->getQuotedName($this));
} }
return $sql; return $sql;
...@@ -836,7 +836,7 @@ class SqlitePlatform extends AbstractPlatform ...@@ -836,7 +836,7 @@ class SqlitePlatform extends AbstractPlatform
if ( ! $this->onSchemaAlterTable($diff, $tableSql)) { if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
$dataTable = new Table('__temp__'.$table->getName()); $dataTable = new Table('__temp__'.$table->getName());
$newTable = new Table($table->getName(), $columns, $this->getPrimaryIndexInAlteredTable($diff), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions()); $newTable = new Table($table->getQuotedName($this), $columns, $this->getPrimaryIndexInAlteredTable($diff), $this->getForeignKeysInAlteredTable($diff), 0, $table->getOptions());
$newTable->addOption('alter', true); $newTable->addOption('alter', true);
$sql = $this->getPreAlterTableIndexForeignKeySQL($diff); $sql = $this->getPreAlterTableIndexForeignKeySQL($diff);
...@@ -849,7 +849,7 @@ class SqlitePlatform extends AbstractPlatform ...@@ -849,7 +849,7 @@ class SqlitePlatform extends AbstractPlatform
$sql[] = $this->getDropTableSQL($dataTable); $sql[] = $this->getDropTableSQL($dataTable);
if ($diff->newName && $diff->newName != $diff->name) { if ($diff->newName && $diff->newName != $diff->name) {
$renamedTable = new Identifier($diff->newName); $renamedTable = $diff->getNewName();
$sql[] = 'ALTER TABLE '.$newTable->getQuotedName($this).' RENAME TO '.$renamedTable->getQuotedName($this); $sql[] = 'ALTER TABLE '.$newTable->getQuotedName($this).' RENAME TO '.$renamedTable->getQuotedName($this);
} }
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/** /**
* Table Diff. * Table Diff.
* *
...@@ -147,11 +149,15 @@ class TableDiff ...@@ -147,11 +149,15 @@ class TableDiff
} }
/** /**
* @param AbstractPlatform $platform The platform to use for retrieving this table diff's name.
*
* @return \Doctrine\DBAL\Schema\Identifier * @return \Doctrine\DBAL\Schema\Identifier
*/ */
public function getName() public function getName(AbstractPlatform $platform)
{ {
return new Identifier($this->name); return new Identifier(
$this->fromTable instanceof Table ? $this->fromTable->getQuotedName($platform) : $this->name
);
} }
/** /**
......
...@@ -614,4 +614,19 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase ...@@ -614,4 +614,19 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
"ALTER TABLE foo CHANGE bar baz INT DEFAULT 666 NOT NULL COMMENT 'rename test'", "ALTER TABLE foo CHANGE bar baz INT DEFAULT 666 NOT NULL COMMENT 'rename test'",
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'ALTER TABLE `foo` DROP FOREIGN KEY fk1',
'ALTER TABLE `foo` DROP FOREIGN KEY fk2',
'ALTER TABLE `foo` RENAME TO `table`, ADD bloo INT NOT NULL, DROP baz, CHANGE bar bar INT DEFAULT NULL, ' .
'CHANGE id war INT NOT NULL',
'ALTER TABLE `table` ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
'ALTER TABLE `table` ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
} }
...@@ -6,6 +6,7 @@ use Doctrine\Common\EventManager; ...@@ -6,6 +6,7 @@ use Doctrine\Common\EventManager;
use Doctrine\DBAL\Events; use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
...@@ -985,4 +986,46 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -985,4 +986,46 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
* @return array * @return array
*/ */
abstract public function getAlterTableRenameColumnSQL(); abstract public function getAlterTableRenameColumnSQL();
/**
* @group DBAL-1016
*/
public function testQuotesTableIdentifiersInAlterTableSQL()
{
$table = new Table('"foo"');
$table->addColumn('id', 'integer');
$table->addColumn('fk', 'integer');
$table->addColumn('fk2', 'integer');
$table->addColumn('fk3', 'integer');
$table->addColumn('bar', 'integer');
$table->addColumn('baz', 'integer');
$table->addForeignKeyConstraint('fk_table', array('fk'), array('id'), array(), 'fk1');
$table->addForeignKeyConstraint('fk_table', array('fk2'), array('id'), array(), 'fk2');
$tableDiff = new TableDiff('"foo"');
$tableDiff->fromTable = $table;
$tableDiff->newName = 'table';
$tableDiff->addedColumns['bloo'] = new Column('bloo', Type::getType('integer'));
$tableDiff->changedColumns['bar'] = new ColumnDiff(
'bar',
new Column('bar', Type::getType('integer'), array('notnull' => false)),
array('notnull'),
$table->getColumn('bar')
);
$tableDiff->renamedColumns['id'] = new Column('war', Type::getType('integer'));
$tableDiff->removedColumns['baz'] = new Column('baz', Type::getType('integer'));
$tableDiff->addedForeignKeys[] = new ForeignKeyConstraint(array('fk3'), 'fk_table', array('id'), 'fk_add');
$tableDiff->changedForeignKeys[] = new ForeignKeyConstraint(array('fk2'), 'fk_table2', array('id'), 'fk2');
$tableDiff->removedForeignKeys[] = new ForeignKeyConstraint(array('fk'), 'fk_table', array('id'), 'fk1');
$this->assertSame(
$this->getQuotesTableIdentifiersInAlterTableSQL(),
$this->_platform->getAlterTableSQL($tableDiff)
);
}
/**
* @return array
*/
abstract protected function getQuotesTableIdentifiersInAlterTableSQL();
} }
...@@ -682,4 +682,24 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -682,4 +682,24 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
'ALTER TABLE foo RENAME COLUMN bar TO baz', 'ALTER TABLE foo RENAME COLUMN bar TO baz',
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'ALTER TABLE "foo" DROP CONSTRAINT fk1',
'ALTER TABLE "foo" DROP CONSTRAINT fk2',
'ALTER TABLE "foo" ADD bloo INT NOT NULL',
'ALTER TABLE "foo" DROP baz',
'ALTER TABLE "foo" ALTER bar DROP NOT NULL',
'ALTER TABLE "foo" RENAME COLUMN id TO war',
'ALTER TABLE "foo" RENAME TO "table"',
'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id) NOT DEFERRABLE ' .
'INITIALLY IMMEDIATE',
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id) NOT DEFERRABLE ' .
'INITIALLY IMMEDIATE',
);
}
} }
...@@ -1137,4 +1137,26 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas ...@@ -1137,4 +1137,26 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
'ALTER TABLE foo ADD CONSTRAINT DF_8C736521_78240498 DEFAULT 666 FOR baz', 'ALTER TABLE foo ADD CONSTRAINT DF_8C736521_78240498 DEFAULT 666 FOR baz',
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'ALTER TABLE [foo] DROP CONSTRAINT fk1',
'ALTER TABLE [foo] DROP CONSTRAINT fk2',
"sp_RENAME '[foo].id', 'war', 'COLUMN'",
'ALTER TABLE [foo] ADD bloo INT NOT NULL',
'ALTER TABLE [foo] DROP COLUMN baz',
'ALTER TABLE [foo] ALTER COLUMN bar INT',
"sp_RENAME '[foo]', 'table'",
"DECLARE @sql NVARCHAR(MAX) = N''; " .
"SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' + REPLACE(dc.name, '8C736521', 'F6298F46') + ''', " .
"''OBJECT'';' FROM sys.default_constraints dc JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " .
"WHERE tbl.name = 'table';EXEC sp_executesql @sql",
'ALTER TABLE [table] ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
'ALTER TABLE [table] ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
} }
...@@ -481,4 +481,21 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -481,4 +481,21 @@ class DB2PlatformTest extends AbstractPlatformTestCase
'ALTER TABLE foo RENAME COLUMN bar TO baz', 'ALTER TABLE foo RENAME COLUMN bar TO baz',
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'ALTER TABLE "foo" DROP FOREIGN KEY fk1',
'ALTER TABLE "foo" DROP FOREIGN KEY fk2',
'ALTER TABLE "foo" ADD COLUMN bloo INTEGER NOT NULL WITH DEFAULT DROP COLUMN baz ' .
'ALTER bar bar INTEGER DEFAULT NULL RENAME COLUMN id TO war',
'CALL SYSPROC.ADMIN_CMD (\'REORG TABLE "foo"\')',
'RENAME TABLE "foo" TO "table"',
'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
} }
...@@ -549,4 +549,22 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -549,4 +549,22 @@ class OraclePlatformTest extends AbstractPlatformTestCase
), ),
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'ALTER TABLE "foo" DROP CONSTRAINT fk1',
'ALTER TABLE "foo" DROP CONSTRAINT fk2',
'ALTER TABLE "foo" ADD (bloo NUMBER(10) NOT NULL)',
'ALTER TABLE "foo" MODIFY (bar NUMBER(10) DEFAULT NULL NULL)',
'ALTER TABLE "foo" RENAME COLUMN id TO war',
'ALTER TABLE "foo" DROP (baz)',
'ALTER TABLE "foo" RENAME TO "table"',
'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
} }
...@@ -873,4 +873,20 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase ...@@ -873,4 +873,20 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
'ALTER TABLE foo RENAME bar TO baz', 'ALTER TABLE foo RENAME bar TO baz',
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'ALTER TABLE "foo" DROP FOREIGN KEY fk1',
'ALTER TABLE "foo" DROP FOREIGN KEY fk2',
'ALTER TABLE "foo" RENAME id TO war',
'ALTER TABLE "foo" ADD bloo INT NOT NULL, DROP baz, ALTER bar INT DEFAULT NULL',
'ALTER TABLE "foo" RENAME "table"',
'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
);
}
} }
...@@ -498,8 +498,8 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -498,8 +498,8 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'CREATE TABLE "table" (id INTEGER NOT NULL, PRIMARY KEY(id))', 'CREATE TABLE "table" (id INTEGER NOT NULL, PRIMARY KEY(id))',
'INSERT INTO "table" (id) SELECT id FROM __temp__table', 'INSERT INTO "table" (id) SELECT id FROM __temp__table',
'DROP TABLE __temp__table', 'DROP TABLE __temp__table',
'CREATE INDEX "select" ON table (id)', 'CREATE INDEX "select" ON "table" (id)',
'CREATE INDEX "bar" ON table (id)', 'CREATE INDEX "bar" ON "table" (id)',
); );
} }
...@@ -574,4 +574,26 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -574,4 +574,26 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'DROP TABLE __temp__foo', 'DROP TABLE __temp__foo',
); );
} }
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL()
{
return array(
'DROP INDEX IDX_8C736521A81E660E',
'DROP INDEX IDX_8C736521FDC58D6C',
'CREATE TEMPORARY TABLE __temp__foo AS SELECT fk, fk2, id, fk3, bar FROM "foo"',
'DROP TABLE "foo"',
'CREATE TABLE "foo" (fk2 INTEGER NOT NULL, fk3 INTEGER NOT NULL, fk INTEGER NOT NULL, war INTEGER NOT NULL, ' .
'bar INTEGER DEFAULT NULL, bloo INTEGER NOT NULL, ' .
'CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id) NOT DEFERRABLE INITIALLY IMMEDIATE, ' .
'CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id) NOT DEFERRABLE INITIALLY IMMEDIATE)',
'INSERT INTO "foo" (fk, fk2, war, fk3, bar) SELECT fk, fk2, id, fk3, bar FROM __temp__foo',
'DROP TABLE __temp__foo',
'ALTER TABLE "foo" RENAME TO "table"',
'CREATE INDEX IDX_8C736521A81E660E ON "table" (fk)',
'CREATE INDEX IDX_8C736521FDC58D6C ON "table" (fk2)',
);
}
} }
...@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Schema; ...@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Schema;
use Doctrine\DBAL\Schema\Identifier; use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class TableDiffTest extends \PHPUnit_Framework_TestCase class TableDiffTest extends \PHPUnit_Framework_TestCase
{ {
...@@ -14,7 +15,28 @@ class TableDiffTest extends \PHPUnit_Framework_TestCase ...@@ -14,7 +15,28 @@ class TableDiffTest extends \PHPUnit_Framework_TestCase
{ {
$tableDiff = new TableDiff('foo'); $tableDiff = new TableDiff('foo');
$this->assertEquals(new Identifier('foo'), $tableDiff->getName()); $this->assertEquals(new Identifier('foo'), $tableDiff->getName(new MockPlatform()));
}
/**
* @group DBAL-1016
*/
public function testPrefersNameFromTableObject()
{
$platformMock = new MockPlatform();
$tableMock = $this->getMockBuilder('Doctrine\DBAL\Schema\Table')
->disableOriginalConstructor()
->getMock();
$tableDiff = new TableDiff('foo');
$tableDiff->fromTable = $tableMock;
$tableMock->expects($this->once())
->method('getQuotedName')
->with($platformMock)
->will($this->returnValue('foo'));
$this->assertEquals(new Identifier('foo'), $tableDiff->getName($platformMock));
} }
/** /**
......
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