From: Matthias Schmidt Date: Thu, 26 Nov 2020 17:41:04 +0000 (+0100) Subject: Support renaming database table columns via PHP API X-Git-Tag: 5.4.0_Alpha_1~556^2~2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=1a024c6552f62eeac40adf564886a6478665c92c;p=GitHub%2FWoltLab%2FWCF.git Support renaming database table columns via PHP API Close #3731 --- diff --git a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php index bcdff00f29..bfa81d7e40 100644 --- a/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php +++ b/wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php @@ -271,7 +271,19 @@ class MySQLDatabaseEditor extends DatabaseEditor { break; case 'alter': - $queries .= "CHANGE COLUMN `{$columnName}` {$this->buildColumnDefinition($data['oldColumnName'], $data['data'])},"; + $newColumnName = $columnName; + if (isset($data['oldColumnName'])) { + /** + * @deprecated 5.4 `oldColumnName` was an incorrect name for the index + * that is kept for backwards compatibility for now + */ + $newColumnName = $data['oldColumnName']; + } + else if (isset($data['newColumnName'])) { + $newColumnName = $data['newColumnName']; + } + + $queries .= "CHANGE COLUMN `{$columnName}` {$this->buildColumnDefinition($newColumnName, $data['data'])},"; break; case 'drop': 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 454897b40d..f245f8d786 100644 --- a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php @@ -343,7 +343,7 @@ class DatabaseTableChangeProcessor { $columnData[$alteredColumn->getName()] = [ 'action' => 'alter', 'data' => $alteredColumn->getData(), - 'oldColumnName' => $alteredColumn->getName() + 'newColumnName' => $alteredColumn->getNewName() ?? $alteredColumn->getName() ]; } @@ -750,6 +750,10 @@ class DatabaseTableChangeProcessor { } } + if ($newColumn->getNewName()) { + return true; + } + // default type has to be checked explicitly for `null` to properly detect changing // from no default value (`null`) and to an empty string as default value (and vice // versa) diff --git a/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php b/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php index d712eeeabd..c8e8aaa6b7 100644 --- a/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php +++ b/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php @@ -26,6 +26,12 @@ abstract class AbstractDatabaseTableColumn implements IDatabaseTableColumn { */ protected $name; + /** + * new name of the database table column + * @var ?string + */ + protected $newName; + /** * is `true` if the values of the column may not be `null` * @var bool @@ -95,6 +101,14 @@ abstract class AbstractDatabaseTableColumn implements IDatabaseTableColumn { return $this->defaultValue; } + /** + * @inheritDoc + * @since 5.4 + */ + public function getNewName(): ?string { + return $this->newName; + } + /** * @inheritDoc */ @@ -146,6 +160,20 @@ abstract class AbstractDatabaseTableColumn implements IDatabaseTableColumn { return $this; } + /** + * @inheritDoc + * @since 5.4 + */ + public function renameTo(string $newName): self { + if ($newName === $this->getName()) { + throw new \InvalidArgumentException("'{$newName}' is the current name of the column."); + } + + $this->newName = $newName; + + return $this; + } + /** * Checks if the given default value is valid. * diff --git a/wcfsetup/install/files/lib/system/database/table/column/IDatabaseTableColumn.class.php b/wcfsetup/install/files/lib/system/database/table/column/IDatabaseTableColumn.class.php index d9f164d743..d2422dd475 100644 --- a/wcfsetup/install/files/lib/system/database/table/column/IDatabaseTableColumn.class.php +++ b/wcfsetup/install/files/lib/system/database/table/column/IDatabaseTableColumn.class.php @@ -47,6 +47,13 @@ interface IDatabaseTableColumn { */ public function getName(); + /** + * Returns the new name of the column or `null` if the column's name is unchanged. + * + * @since 5.4 + */ + public function getNewName(): ?string; + /** * Returns the type of the column. * @@ -77,6 +84,13 @@ interface IDatabaseTableColumn { */ public function notNull($notNull = true); + /** + * Sets the new name of the column and returns the column. + * + * @since 5.4 + */ + public function renameTo(string $newName): self; + /** * Returns `true` if the column will be dropped. *