From 689cb90b4fd541726612471bf58e5974147b5b67 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 29 Sep 2019 19:07:08 +0200 Subject: [PATCH] Fix default value handling in DB API --- .../DatabaseTableChangeProcessor.class.php | 19 ++++++++++++++++++- .../AbstractDatabaseTableColumn.class.php | 2 +- 2 files changed, 19 insertions(+), 2 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 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() ]; -- 2.20.1