From 1a029332791f3e3cdca054d8f139857b5ef78791 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Mon, 5 Jul 2021 11:15:20 +0200 Subject: [PATCH] Make renaming columns with PHP DB API idempotent (#4367) An error message should only be shown if the neither a column with the old name, nor with the name name exists so that after the rename, a second rename is a no-op. Close #4362 See #3765 --- .../table/DatabaseTableChangeProcessor.class.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php index 01f535b164..6ee6fa6085 100644 --- a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php @@ -417,10 +417,14 @@ class DatabaseTableChangeProcessor $this->deleteColumnLog($tableName, $column); } } elseif (!isset($existingColumns[$column->getName()])) { - if (!isset($this->columnsToAdd[$tableName])) { - $this->columnsToAdd[$tableName] = []; + // It was already checked in `validate()` that for renames, the column either + // exists with the old or new name. + if (!$column->getNewName()) { + if (!isset($this->columnsToAdd[$tableName])) { + $this->columnsToAdd[$tableName] = []; + } + $this->columnsToAdd[$tableName][] = $column; } - $this->columnsToAdd[$tableName][] = $column; } elseif ($this->diffColumns($existingColumns[$column->getName()], $column)) { if (!isset($this->columnsToAlter[$tableName])) { $this->columnsToAlter[$tableName] = []; @@ -1182,7 +1186,9 @@ class DatabaseTableChangeProcessor 'type' => 'foreignColumnChange', ]; } - } elseif ($column->getNewName()) { + } elseif ($column->getNewName() && !isset($existingColumns[$column->getNewName()])) { + // Only show error message for a column rename if no column with the + // old or new name exists. $errors[] = [ 'columnName' => $column->getName(), 'tableName' => $table->getName(), -- 2.20.1