From: Matthias Schmidt Date: Sun, 29 Sep 2019 17:07:08 +0000 (+0200) Subject: Fix default value handling in DB API X-Git-Tag: 5.2.0_Beta_2~16 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=689cb90b4fd541726612471bf58e5974147b5b67;p=GitHub%2FWoltLab%2FWCF.git Fix default value handling in DB API --- 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 0e07b14c32..3672aba3c4 100644 --- a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php @@ -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. * 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 379b661bad..1efa602041 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 @@ -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() ];