From: Matthias Schmidt Date: Sat, 7 Dec 2019 09:19:56 +0000 (+0100) Subject: Support updating explicitly named database table indices X-Git-Tag: 5.2.0_RC_1~11 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c8280fb8f9842b385c9f5d5cacbfe6ce50129696;p=GitHub%2FWoltLab%2FWCF.git Support updating explicitly named database table indices Close #3116 --- diff --git a/wcfsetup/install/files/lib/system/database/table/DatabaseTable.class.php b/wcfsetup/install/files/lib/system/database/table/DatabaseTable.class.php index 9ef900887d..21b96c55a3 100644 --- a/wcfsetup/install/files/lib/system/database/table/DatabaseTable.class.php +++ b/wcfsetup/install/files/lib/system/database/table/DatabaseTable.class.php @@ -211,7 +211,7 @@ class DatabaseTable { } if ($index->getName() === '') { - $index->name(md5($this->getName() . '_' . $index->getColumns()[0])); + $index->generatedName(md5($this->getName() . '_' . $index->getColumns()[0])); } if (isset($this->foreignKeys[$index->getName()])) { 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 43e4b88010..b0897945ce 100644 --- a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php @@ -282,22 +282,22 @@ class DatabaseTableChangeProcessor { } } - foreach ($this->indicesToAdd as $tableName => $indices) { + foreach ($this->indicesToDrop as $tableName => $indices) { foreach ($indices as $index) { $appliedAnyChange = true; - $this->prepareIndexLog($tableName, $index); - $this->addIndex($tableName, $index); - $this->finalizeIndexLog($tableName, $index); + $this->dropIndex($tableName, $index); + $this->deleteIndexLog($tableName, $index); } } - foreach ($this->indicesToDrop as $tableName => $indices) { + foreach ($this->indicesToAdd as $tableName => $indices) { foreach ($indices as $index) { $appliedAnyChange = true; - $this->dropIndex($tableName, $index); - $this->deleteIndexLog($tableName, $index); + $this->prepareIndexLog($tableName, $index); + $this->addIndex($tableName, $index); + $this->finalizeIndexLog($tableName, $index); } } @@ -459,7 +459,7 @@ class DatabaseTableChangeProcessor { foreach ($table->getIndices() as $index) { $matchingExistingIndex = null; foreach ($existingIndices as $existingIndex) { - if (empty(array_diff($index->getData(), $existingIndex->getData()))) { + if (!$this->diffIndices($existingIndex, $index)) { $matchingExistingIndex = $existingIndex; break; } @@ -479,7 +479,23 @@ class DatabaseTableChangeProcessor { $this->deleteIndexLog($tableName, $index); } } - else if ($matchingExistingIndex === null) { + else if ($matchingExistingIndex !== null) { + // updating index type and index columns is supported with an + // explicit index name is given (automatically generated index + // names are not deterministic) + if (!$index->hasGeneratedName() && !empty(array_diff($matchingExistingIndex->getData(), $index->getData()))) { + if (!isset($this->indicesToDrop[$tableName])) { + $this->indicesToDrop[$tableName] = []; + } + $this->indicesToDrop[$tableName][] = $matchingExistingIndex; + + if (!isset($this->indicesToAdd[$tableName])) { + $this->indicesToAdd[$tableName] = []; + } + $this->indicesToAdd[$tableName][] = $index; + } + } + else { if (!isset($this->indicesToAdd[$tableName])) { $this->indicesToAdd[$tableName] = []; } @@ -678,6 +694,21 @@ class DatabaseTableChangeProcessor { return $oldColumn->getDefaultValue() != $newColumn->getDefaultValue(); } + /** + * Returns `true` if the two indices differ. + * + * @param DatabaseTableIndex $oldIndex + * @param DatabaseTableIndex $newIndex + * @return bool + */ + protected function diffIndices(DatabaseTableIndex $oldIndex, DatabaseTableIndex $newIndex) { + if ($newIndex->hasGeneratedName()) { + return !empty(array_diff($oldIndex->getData(), $newIndex->getData())); + } + + return $oldIndex->getName() !== $newIndex->getName(); + } + /** * Drops the given foreign key. * diff --git a/wcfsetup/install/files/lib/system/database/table/index/DatabaseTableIndex.class.php b/wcfsetup/install/files/lib/system/database/table/index/DatabaseTableIndex.class.php index 747001eb5c..746d7e8c20 100644 --- a/wcfsetup/install/files/lib/system/database/table/index/DatabaseTableIndex.class.php +++ b/wcfsetup/install/files/lib/system/database/table/index/DatabaseTableIndex.class.php @@ -20,6 +20,12 @@ class DatabaseTableIndex { */ protected $columns; + /** + * is `true` if index name has been automatically generated + * @var bool + */ + protected $generatedName = false; + /** * name of index * @var string @@ -57,6 +63,19 @@ class DatabaseTableIndex { return $this; } + + /** + * Sets the automatically generated name of the index. + * + * @param string $name index name + * @return $this this index + */ + public function generatedName($name) { + $this->name($name); + $this->generatedName = true; + + return $this; + } /** * Returns the index columns. @@ -97,6 +116,15 @@ class DatabaseTableIndex { return $this->type; } + /** + * Returns `true` if the name of the index has been automatically generated. + * + * @return bool + */ + public function hasGeneratedName() { + return $this->generatedName; + } + /** * Sets the name of the index. *