Skip to content

Commit d2406e3

Browse files
committed
Merge branch '4.1.x' into 5.0.x
* 4.1.x: Fix tests (#6481) Add explicit renameColumn method (#6280) Make AbstractSchemaManager covariant to its template argument
2 parents 842f2b9 + 3248855 commit d2406e3

25 files changed

+591
-329
lines changed

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Doctrine\DBAL\Platforms\Keywords\MySQLKeywords;
1111
use Doctrine\DBAL\Schema\AbstractAsset;
1212
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
13-
use Doctrine\DBAL\Schema\Identifier;
1413
use Doctrine\DBAL\Schema\Index;
1514
use Doctrine\DBAL\Schema\MySQLSchemaManager;
1615
use Doctrine\DBAL\Schema\TableDiff;
@@ -328,7 +327,6 @@ private function buildTableOptions(array $options): string
328327
*/
329328
public function getAlterTableSQL(TableDiff $diff): array
330329
{
331-
$columnSql = [];
332330
$queryParts = [];
333331

334332
foreach ($diff->getAddedColumns() as $column) {
@@ -346,7 +344,7 @@ public function getAlterTableSQL(TableDiff $diff): array
346344
$queryParts[] = 'DROP ' . $column->getQuotedName($this);
347345
}
348346

349-
foreach ($diff->getModifiedColumns() as $columnDiff) {
347+
foreach ($diff->getChangedColumns() as $columnDiff) {
350348
$newColumn = $columnDiff->getNewColumn();
351349

352350
$newColumnProperties = array_merge($newColumn->toArray(), [
@@ -359,17 +357,6 @@ public function getAlterTableSQL(TableDiff $diff): array
359357
. $this->getColumnDeclarationSQL($newColumn->getQuotedName($this), $newColumnProperties);
360358
}
361359

362-
foreach ($diff->getRenamedColumns() as $oldColumnName => $column) {
363-
$oldColumnName = new Identifier($oldColumnName);
364-
365-
$columnProperties = array_merge($column->toArray(), [
366-
'comment' => $column->getComment(),
367-
]);
368-
369-
$queryParts[] = 'CHANGE ' . $oldColumnName->getQuotedName($this) . ' '
370-
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnProperties);
371-
}
372-
373360
$addedIndexes = $this->indexAssetsByLowerCaseName($diff->getAddedIndexes());
374361
$modifiedIndexes = $this->indexAssetsByLowerCaseName($diff->getModifiedIndexes());
375362
$diffModified = false;
@@ -398,35 +385,31 @@ public function getAlterTableSQL(TableDiff $diff): array
398385
if ($diffModified) {
399386
$diff = new TableDiff(
400387
$diff->getOldTable(),
401-
$diff->getAddedColumns(),
402-
$diff->getModifiedColumns(),
403-
$diff->getDroppedColumns(),
404-
$diff->getRenamedColumns(),
405-
array_values($addedIndexes),
406-
array_values($modifiedIndexes),
407-
$diff->getDroppedIndexes(),
408-
$diff->getRenamedIndexes(),
409-
$diff->getAddedForeignKeys(),
410-
$diff->getModifiedForeignKeys(),
411-
$diff->getDroppedForeignKeys(),
388+
addedColumns: $diff->getAddedColumns(),
389+
changedColumns: $diff->getChangedColumns(),
390+
droppedColumns: $diff->getDroppedColumns(),
391+
addedIndexes: array_values($addedIndexes),
392+
modifiedIndexes: array_values($modifiedIndexes),
393+
droppedIndexes: $diff->getDroppedIndexes(),
394+
renamedIndexes: $diff->getRenamedIndexes(),
395+
addedForeignKeys: $diff->getAddedForeignKeys(),
396+
modifiedForeignKeys: $diff->getModifiedForeignKeys(),
397+
droppedForeignKeys: $diff->getDroppedForeignKeys(),
412398
);
413399
}
414400

415-
$sql = [];
416401
$tableSql = [];
417402

418403
if (count($queryParts) > 0) {
419-
$sql[] = 'ALTER TABLE ' . $diff->getOldTable()->getQuotedName($this) . ' '
404+
$tableSql[] = 'ALTER TABLE ' . $diff->getOldTable()->getQuotedName($this) . ' '
420405
. implode(', ', $queryParts);
421406
}
422407

423-
$sql = array_merge(
408+
return array_merge(
424409
$this->getPreAlterTableIndexForeignKeySQL($diff),
425-
$sql,
410+
$tableSql,
426411
$this->getPostAlterTableIndexForeignKeySQL($diff),
427412
);
428-
429-
return array_merge($sql, $tableSql, $columnSql);
430413
}
431414

432415
/**

src/Platforms/AbstractPlatform.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
968968
}
969969

970970
if (isset($options['indexes']) && ! empty($options['indexes'])) {
971-
foreach ($options['indexes'] as $index => $definition) {
971+
foreach ($options['indexes'] as $definition) {
972972
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
973973
}
974974
}
@@ -1297,6 +1297,20 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
12971297
];
12981298
}
12991299

1300+
/**
1301+
* Returns the SQL for renaming a column
1302+
*
1303+
* @param string $tableName The table to rename the column on.
1304+
* @param string $oldColumnName The name of the column we want to rename.
1305+
* @param string $newColumnName The name we should rename it to.
1306+
*
1307+
* @return list<string> The sequence of SQL statements for renaming the given column.
1308+
*/
1309+
protected function getRenameColumnSQL(string $tableName, string $oldColumnName, string $newColumnName): array
1310+
{
1311+
return [sprintf('ALTER TABLE %s RENAME COLUMN %s TO %s', $tableName, $oldColumnName, $newColumnName)];
1312+
}
1313+
13001314
/**
13011315
* Gets declaration of a number of columns in bulk.
13021316
*

src/Platforms/DB2Platform.php

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\DBAL\SQL\Builder\DefaultSelectSQLBuilder;
1717
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
1818
use Doctrine\DBAL\TransactionIsolationLevel;
19+
use Doctrine\DBAL\Types\DateTimeType;
1920
use Doctrine\DBAL\Types\Types;
2021

2122
use function array_merge;
@@ -262,7 +263,6 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
262263
public function getAlterTableSQL(TableDiff $diff): array
263264
{
264265
$sql = [];
265-
$columnSql = [];
266266
$commentsSQL = [];
267267

268268
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);
@@ -296,11 +296,13 @@ public function getAlterTableSQL(TableDiff $diff): array
296296
);
297297
}
298298

299+
$needsReorg = false;
299300
foreach ($diff->getDroppedColumns() as $column) {
300301
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
302+
$needsReorg = true;
301303
}
302304

303-
foreach ($diff->getModifiedColumns() as $columnDiff) {
305+
foreach ($diff->getChangedColumns() as $columnDiff) {
304306
if ($columnDiff->hasCommentChanged()) {
305307
$newColumn = $columnDiff->getNewColumn();
306308
$commentsSQL[] = $this->getCommentOnColumnSQL(
@@ -315,33 +317,25 @@ public function getAlterTableSQL(TableDiff $diff): array
315317
$columnDiff,
316318
$sql,
317319
$queryParts,
320+
$needsReorg,
318321
);
319322
}
320323

321-
foreach ($diff->getRenamedColumns() as $oldColumnName => $column) {
322-
$oldColumnName = new Identifier($oldColumnName);
323-
324-
$queryParts[] = 'RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .
325-
' TO ' . $column->getQuotedName($this);
326-
}
327-
328324
if (count($queryParts) > 0) {
329325
$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' ' . implode(' ', $queryParts);
330326
}
331327

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

337-
$sql = array_merge(
333+
return array_merge(
338334
$this->getPreAlterTableIndexForeignKeySQL($diff),
339335
$sql,
340336
$commentsSQL,
341337
$this->getPostAlterTableIndexForeignKeySQL($diff),
342338
);
343-
344-
return array_merge($sql, $columnSql);
345339
}
346340

347341
public function getRenameTableSQL(string $oldName, string $newName): string
@@ -362,10 +356,11 @@ private function gatherAlterColumnSQL(
362356
ColumnDiff $columnDiff,
363357
array &$sql,
364358
array &$queryParts,
359+
bool &$needsReorg,
365360
): void {
366-
$alterColumnClauses = $this->getAlterColumnClausesSQL($columnDiff);
361+
$alterColumnClauses = $this->getAlterColumnClausesSQL($columnDiff, $needsReorg);
367362

368-
if (empty($alterColumnClauses)) {
363+
if (count($alterColumnClauses) < 1) {
369364
return;
370365
}
371366

@@ -389,41 +384,56 @@ private function gatherAlterColumnSQL(
389384
*
390385
* @return string[]
391386
*/
392-
private function getAlterColumnClausesSQL(ColumnDiff $columnDiff): array
387+
private function getAlterColumnClausesSQL(ColumnDiff $columnDiff, bool &$needsReorg): array
393388
{
394-
$newColumn = $columnDiff->getNewColumn()->toArray();
389+
$newColumn = $columnDiff->getNewColumn();
390+
$columnArray = $newColumn->toArray();
391+
392+
$newName = $columnDiff->getNewColumn()->getQuotedName($this);
393+
$oldName = $columnDiff->getOldColumn()->getQuotedName($this);
395394

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

398-
if ($newColumn['columnDefinition'] !== null) {
399-
return [$alterClause . ' ' . $newColumn['columnDefinition']];
397+
if ($newColumn->getColumnDefinition() !== null) {
398+
$needsReorg = true;
399+
400+
return [$alterClause . ' ' . $newColumn->getColumnDefinition()];
400401
}
401402

402403
$clauses = [];
403404

405+
if ($columnDiff->hasNameChanged()) {
406+
$clauses[] = 'RENAME COLUMN ' . $oldName . ' TO ' . $newName;
407+
}
408+
404409
if (
405410
$columnDiff->hasTypeChanged() ||
406411
$columnDiff->hasLengthChanged() ||
407412
$columnDiff->hasPrecisionChanged() ||
408413
$columnDiff->hasScaleChanged() ||
409414
$columnDiff->hasFixedChanged()
410415
) {
411-
$clauses[] = $alterClause . ' SET DATA TYPE ' . $newColumn['type']->getSQLDeclaration($newColumn, $this);
416+
$needsReorg = true;
417+
$clauses[] = $alterClause . ' SET DATA TYPE ' . $newColumn->getType()
418+
->getSQLDeclaration($columnArray, $this);
412419
}
413420

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

418426
if ($columnDiff->hasDefaultChanged()) {
419-
if (isset($newColumn['default'])) {
420-
$defaultClause = $this->getDefaultValueDeclarationSQL($newColumn);
427+
if ($newColumn->getDefault() !== null) {
428+
$defaultClause = $this->getDefaultValueDeclarationSQL($columnArray);
421429

422430
if ($defaultClause !== '') {
423-
$clauses[] = $alterClause . ' SET' . $defaultClause;
431+
$needsReorg = true;
432+
$clauses[] = $alterClause . ' SET' . $defaultClause;
424433
}
425434
} else {
426-
$clauses[] = $alterClause . ' DROP DEFAULT';
435+
$needsReorg = true;
436+
$clauses[] = $alterClause . ' DROP DEFAULT';
427437
}
428438
}
429439

@@ -485,12 +495,12 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
485495
*/
486496
public function getDefaultValueDeclarationSQL(array $column): string
487497
{
488-
if (! empty($column['autoincrement'])) {
498+
if (isset($column['autoincrement']) && $column['autoincrement'] === true) {
489499
return '';
490500
}
491501

492-
if (! empty($column['version'])) {
493-
if ((string) $column['type'] !== 'DateTime') {
502+
if (isset($column['version']) && $column['version'] === true) {
503+
if ($column['type'] instanceof DateTimeType) {
494504
$column['default'] = '1';
495505
}
496506
}

0 commit comments

Comments
 (0)