Support renaming database table columns via PHP API
authorMatthias Schmidt <gravatronics@live.com>
Thu, 26 Nov 2020 17:41:04 +0000 (18:41 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 26 Nov 2020 17:41:04 +0000 (18:41 +0100)
Close #3731

wcfsetup/install/files/lib/system/database/editor/MySQLDatabaseEditor.class.php
wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php
wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php
wcfsetup/install/files/lib/system/database/table/column/IDatabaseTableColumn.class.php

index bcdff00f29fb1922f1f95959a8b68270e3bad113..bfa81d7e401cc1fca7ce0380c94d5c3215b85e38 100644 (file)
@@ -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':
index 454897b40d2d80a92d426d944f6529a6e1d3392f..f245f8d7862bb254d1a1b624d2721a4fdae671cc 100644 (file)
@@ -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)
index d712eeeabdf9714604ab4e35e2bbd497ce164f4c..c8e8aaa6b7028ff91990f9a63131658cb0b6afd9 100644 (file)
@@ -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.
         * 
index d9f164d7432858c359d0134d87847bbdc70d7541..d2422dd4751d390b68fb306ec7b9a83f5bdfdf4e 100644 (file)
@@ -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.
         *