Skip to content

Commit

Permalink
Add explicit renameColumn method (#6280)
Browse files Browse the repository at this point in the history
Merges the changes of #6080 into 4.0.x
  • Loading branch information
Tofandel authored Aug 2, 2024
1 parent b1340a6 commit 0f0398a
Show file tree
Hide file tree
Showing 22 changed files with 558 additions and 321 deletions.
45 changes: 14 additions & 31 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\DBAL\Platforms\Keywords\MySQLKeywords;
use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\MySQLSchemaManager;
use Doctrine\DBAL\Schema\TableDiff;
Expand Down Expand Up @@ -335,7 +334,6 @@ private function buildTableOptions(array $options): string
*/
public function getAlterTableSQL(TableDiff $diff): array
{
$columnSql = [];
$queryParts = [];

foreach ($diff->getAddedColumns() as $column) {
Expand All @@ -353,7 +351,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$queryParts[] = 'DROP ' . $column->getQuotedName($this);
}

foreach ($diff->getModifiedColumns() as $columnDiff) {
foreach ($diff->getChangedColumns() as $columnDiff) {
$newColumn = $columnDiff->getNewColumn();

$newColumnProperties = array_merge($newColumn->toArray(), [
Expand All @@ -366,17 +364,6 @@ public function getAlterTableSQL(TableDiff $diff): array
. $this->getColumnDeclarationSQL($newColumn->getQuotedName($this), $newColumnProperties);
}

foreach ($diff->getRenamedColumns() as $oldColumnName => $column) {
$oldColumnName = new Identifier($oldColumnName);

$columnProperties = array_merge($column->toArray(), [
'comment' => $column->getComment(),
]);

$queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' '
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnProperties);
}

$addedIndexes = $this->indexAssetsByLowerCaseName($diff->getAddedIndexes());
$modifiedIndexes = $this->indexAssetsByLowerCaseName($diff->getModifiedIndexes());
$diffModified = false;
Expand Down Expand Up @@ -405,35 +392,31 @@ public function getAlterTableSQL(TableDiff $diff): array
if ($diffModified) {
$diff = new TableDiff(
$diff->getOldTable(),
$diff->getAddedColumns(),
$diff->getModifiedColumns(),
$diff->getDroppedColumns(),
$diff->getRenamedColumns(),
array_values($addedIndexes),
array_values($modifiedIndexes),
$diff->getDroppedIndexes(),
$diff->getRenamedIndexes(),
$diff->getAddedForeignKeys(),
$diff->getModifiedForeignKeys(),
$diff->getDroppedForeignKeys(),
addedColumns: $diff->getAddedColumns(),
changedColumns: $diff->getChangedColumns(),
droppedColumns: $diff->getDroppedColumns(),
addedIndexes: array_values($addedIndexes),
modifiedIndexes: array_values($modifiedIndexes),
droppedIndexes: $diff->getDroppedIndexes(),
renamedIndexes: $diff->getRenamedIndexes(),
addedForeignKeys: $diff->getAddedForeignKeys(),
modifiedForeignKeys: $diff->getModifiedForeignKeys(),
droppedForeignKeys: $diff->getDroppedForeignKeys(),
);
}

$sql = [];
$tableSql = [];

if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $diff->getOldTable()->getQuotedName($this) . ' '
$tableSql[] = 'ALTER TABLE ' . $diff->getOldTable()->getQuotedName($this) . ' '
. implode(', ', $queryParts);
}

$sql = array_merge(
return array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$tableSql,
$this->getPostAlterTableIndexForeignKeySQL($diff),
);

return array_merge($sql, $tableSql, $columnSql);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
}

if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index => $definition) {
foreach ($options['indexes'] as $definition) {
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
}
}
Expand Down Expand Up @@ -1297,6 +1297,20 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
];
}

/**
* Returns the SQL for renaming a column
*
* @param string $tableName The table to rename the column on.
* @param string $oldColumnName The name of the column we want to rename.
* @param string $newColumnName The name we should rename it to.
*
* @return list<string> The sequence of SQL statements for renaming the given column.
*/
protected function getRenameColumnSQL(string $tableName, string $oldColumnName, string $newColumnName): array
{
return [sprintf('ALTER TABLE %s RENAME COLUMN %s TO %s', $tableName, $oldColumnName, $newColumnName)];
}

/**
* Gets declaration of a number of columns in bulk.
*
Expand Down
68 changes: 39 additions & 29 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\SQL\Builder\DefaultSelectSQLBuilder;
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\Types;

use function array_merge;
Expand Down Expand Up @@ -262,7 +263,6 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
public function getAlterTableSQL(TableDiff $diff): array
{
$sql = [];
$columnSql = [];
$commentsSQL = [];

$tableNameSQL = $diff->getOldTable()->getQuotedName($this);
Expand Down Expand Up @@ -296,11 +296,13 @@ public function getAlterTableSQL(TableDiff $diff): array
);
}

$needsReorg = false;
foreach ($diff->getDroppedColumns() as $column) {
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
$needsReorg = true;
}

foreach ($diff->getModifiedColumns() as $columnDiff) {
foreach ($diff->getChangedColumns() as $columnDiff) {
if ($columnDiff->hasCommentChanged()) {
$newColumn = $columnDiff->getNewColumn();
$commentsSQL[] = $this->getCommentOnColumnSQL(
Expand All @@ -315,33 +317,25 @@ public function getAlterTableSQL(TableDiff $diff): array
$columnDiff,
$sql,
$queryParts,
$needsReorg,
);
}

foreach ($diff->getRenamedColumns() as $oldColumnName => $column) {
$oldColumnName = new Identifier($oldColumnName);

$queryParts[] = 'RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .
' TO ' . $column->getQuotedName($this);
}

if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' ' . implode(' ', $queryParts);
}

// Some table alteration operations require a table reorganization.
if (count($diff->getDroppedColumns()) > 0 || count($diff->getModifiedColumns()) > 0) {
if ($needsReorg) {
$sql[] = "CALL SYSPROC.ADMIN_CMD ('REORG TABLE " . $tableNameSQL . "')";
}

$sql = array_merge(
return array_merge(
$this->getPreAlterTableIndexForeignKeySQL($diff),
$sql,
$commentsSQL,
$this->getPostAlterTableIndexForeignKeySQL($diff),
);

return array_merge($sql, $columnSql);
}

public function getRenameTableSQL(string $oldName, string $newName): string
Expand All @@ -362,10 +356,11 @@ private function gatherAlterColumnSQL(
ColumnDiff $columnDiff,
array &$sql,
array &$queryParts,
bool &$needsReorg,
): void {
$alterColumnClauses = $this->getAlterColumnClausesSQL($columnDiff);
$alterColumnClauses = $this->getAlterColumnClausesSQL($columnDiff, $needsReorg);

if (empty($alterColumnClauses)) {
if (count($alterColumnClauses) < 1) {
return;
}

Expand All @@ -389,41 +384,56 @@ private function gatherAlterColumnSQL(
*
* @return string[]
*/
private function getAlterColumnClausesSQL(ColumnDiff $columnDiff): array
private function getAlterColumnClausesSQL(ColumnDiff $columnDiff, bool &$needsReorg): array
{
$newColumn = $columnDiff->getNewColumn()->toArray();
$newColumn = $columnDiff->getNewColumn();
$columnArray = $newColumn->toArray();

$newName = $columnDiff->getNewColumn()->getQuotedName($this);
$oldName = $columnDiff->getOldColumn()->getQuotedName($this);

$alterClause = 'ALTER COLUMN ' . $columnDiff->getNewColumn()->getQuotedName($this);
$alterClause = 'ALTER COLUMN ' . $newName;

if ($newColumn['columnDefinition'] !== null) {
return [$alterClause . ' ' . $newColumn['columnDefinition']];
if ($newColumn->getColumnDefinition() !== null) {
$needsReorg = true;

return [$alterClause . ' ' . $newColumn->getColumnDefinition()];
}

$clauses = [];

if ($columnDiff->hasNameChanged()) {
$clauses[] = 'RENAME COLUMN ' . $oldName . ' TO ' . $newName;
}

if (
$columnDiff->hasTypeChanged() ||
$columnDiff->hasLengthChanged() ||
$columnDiff->hasPrecisionChanged() ||
$columnDiff->hasScaleChanged() ||
$columnDiff->hasFixedChanged()
) {
$clauses[] = $alterClause . ' SET DATA TYPE ' . $newColumn['type']->getSQLDeclaration($newColumn, $this);
$needsReorg = true;
$clauses[] = $alterClause . ' SET DATA TYPE ' . $newColumn->getType()
->getSQLDeclaration($columnArray, $this);
}

if ($columnDiff->hasNotNullChanged()) {
$clauses[] = $newColumn['notnull'] ? $alterClause . ' SET NOT NULL' : $alterClause . ' DROP NOT NULL';
$needsReorg = true;
$clauses[] = $newColumn->getNotnull() ? $alterClause . ' SET NOT NULL' : $alterClause . ' DROP NOT NULL';
}

if ($columnDiff->hasDefaultChanged()) {
if (isset($newColumn['default'])) {
$defaultClause = $this->getDefaultValueDeclarationSQL($newColumn);
if ($newColumn->getDefault() !== null) {
$defaultClause = $this->getDefaultValueDeclarationSQL($columnArray);

if ($defaultClause !== '') {
$clauses[] = $alterClause . ' SET' . $defaultClause;
$needsReorg = true;
$clauses[] = $alterClause . ' SET' . $defaultClause;
}
} else {
$clauses[] = $alterClause . ' DROP DEFAULT';
$needsReorg = true;
$clauses[] = $alterClause . ' DROP DEFAULT';
}
}

Expand Down Expand Up @@ -485,12 +495,12 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
*/
public function getDefaultValueDeclarationSQL(array $column): string
{
if (! empty($column['autoincrement'])) {
if (isset($column['autoincrement']) && $column['autoincrement'] === true) {
return '';
}

if (! empty($column['version'])) {
if ((string) $column['type'] !== 'DateTime') {
if (isset($column['version']) && $column['version'] === true) {
if ($column['type'] instanceof DateTimeType) {
$column['default'] = '1';
}
}
Expand Down
Loading

0 comments on commit 0f0398a

Please sign in to comment.