Skip to content

Commit

Permalink
Add explicit renameColumn method (4.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Jan 26, 2024
1 parent 63f294e commit 5678b6d
Show file tree
Hide file tree
Showing 23 changed files with 560 additions and 316 deletions.
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

<!-- TODO for PHPUnit 11 -->
<referencedMethod name="PHPUnit\Framework\TestCase::iniSet"/>
<referencedMethod name="Doctrine\DBAL\Schema\TableDiff::getRenamedColumns"/>
</errorLevel>
</DeprecatedMethod>
<DocblockTypeContradiction>
Expand Down
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
23 changes: 18 additions & 5 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,7 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
}
}

$columnSql = [];
$columns = [];
$columns = [];

foreach ($table->getColumns() as $column) {
$columnData = $this->columnToArray($column);
Expand Down Expand Up @@ -846,7 +845,7 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
}
}

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

/**
Expand Down Expand Up @@ -944,7 +943,7 @@ public function getInlineColumnCommentSQL(string $comment): string
* @param mixed[][] $columns
* @param mixed[] $options
*
* @return array<int, string>
* @return list<string>
*/
protected function _getCreateTableSQL(string $name, array $columns, array $options = []): array
{
Expand All @@ -961,7 +960,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) {

Check warning on line 963 in src/Platforms/AbstractPlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/AbstractPlatform.php#L963

Added line #L963 was not covered by tests
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
}
}
Expand Down Expand Up @@ -1290,6 +1289,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) {

Check warning on line 503 in src/Platforms/DB2Platform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/DB2Platform.php#L503

Added line #L503 was not covered by tests
$column['default'] = '1';
}
}
Expand Down

0 comments on commit 5678b6d

Please sign in to comment.