Fix default value handling in DB API
authorMatthias Schmidt <gravatronics@live.com>
Sun, 29 Sep 2019 17:07:08 +0000 (19:07 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 29 Sep 2019 17:07:08 +0000 (19:07 +0200)
wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php
wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php

index 0e07b14c32048cc5c0a283375015089a7c9723ec..3672aba3c4d7c2fd386a45b5d2ddda4daf18a38e 100644 (file)
@@ -401,7 +401,7 @@ class DatabaseTableChangeProcessor {
                                                }
                                                $this->columnsToAdd[$tableName][] = $column;
                                        }
-                                       else if (!empty(array_diff($column->getData(), $existingColumns[$column->getName()]->getData()))) {
+                                       else if ($this->diffColumns($existingColumns[$column->getName()], $column)) {
                                                if (!isset($this->columnsToAlter[$tableName])) {
                                                        $this->columnsToAlter[$tableName] = [];
                                                }
@@ -650,6 +650,23 @@ class DatabaseTableChangeProcessor {
                ]);
        }
        
+       /**
+        * Returns `true` if the two columns differ.
+        * 
+        * @param       IDatabaseTableColumn    $oldColumn
+        * @param       IDatabaseTableColumn    $newColumn
+        * @return      bool
+        */
+       protected function diffColumns(IDatabaseTableColumn $oldColumn, IDatabaseTableColumn $newColumn) {
+               if (!empty(array_diff($oldColumn->getData(), $newColumn->getData()))) {
+                       return true;
+               }
+               
+               // default type has to be checked with a strict check to differentiate between having
+               // no default value (`null`) and having an empty string as default value
+               return $oldColumn->getDefaultValue() !== $newColumn->getDefaultValue();
+       }
+       
        /**
         * Drops the given foreign key.
         * 
index 379b661bad0a88b6a3bca024319a97988d6f7289..1efa602041fd1634c626da7ea98ae4af5c490af0 100644 (file)
@@ -54,7 +54,7 @@ abstract class AbstractDatabaseTableColumn implements IDatabaseTableColumn {
         */
        public function getData() {
                $data = [
-                       'default' => $this->defaultValue,
+                       'default' => $this->getDefaultValue() !== null ? "'" . str_replace(["'", '\\'], ["''", '\\\\'], $this->getDefaultValue()) . "'" : null,
                        'notNull' => $this->isNotNull() ? 1 : 0,
                        'type' => $this->getType()
                ];