From: Alexander Ebert Date: Mon, 6 Jan 2020 15:29:32 +0000 (+0100) Subject: Idempotent modification of the database X-Git-Tag: 5.2.1~9 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c83172a3c8d79aeb0f6483162180289739f78277;p=GitHub%2FWoltLab%2FWCF.git Idempotent modification of the database Fixes #3125 --- diff --git a/com.woltlab.wcf/files_pre.tar b/com.woltlab.wcf/files_pre.tar index 5a42367369..4d8e91d117 100644 Binary files a/com.woltlab.wcf/files_pre.tar and b/com.woltlab.wcf/files_pre.tar differ diff --git a/com.woltlab.wcf/update_5.2.sql b/com.woltlab.wcf/update_5.2.sql index 5fbc40d4f1..896f750add 100644 --- a/com.woltlab.wcf/update_5.2.sql +++ b/com.woltlab.wcf/update_5.2.sql @@ -1,2 +1,2 @@ -INSERT INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfEditorTableBorder', 'rgba(221, 221, 221, 1)'); -INSERT INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfPageThemeColor', ''); -- uses `$wcfHeaderBackground` if left empty +INSERT IGNORE INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfEditorTableBorder', 'rgba(221, 221, 221, 1)'); +INSERT IGNORE INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfPageThemeColor', ''); -- uses `$wcfHeaderBackground` if left empty diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php index 9ef5c88efa..4c4338571f 100644 --- a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php +++ b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php @@ -27,6 +27,12 @@ try { $reactions = ['like', 'thanks', 'haha', 'confused', 'sad']; if (LIKE_ENABLE_DISLIKE) { + // Remove the existing phrase in case a previous upgrade attempt has failed. + $sql = "DELETE FROM wcf".WCF_N."_language_item + WHERE languageItem = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(['wcf.reactionType.title6']); + $reactions[] = 'thumbsDown'; $sql = "SELECT languageCategoryID @@ -50,9 +56,9 @@ try { } } - $sql = "INSERT INTO wcf".WCF_N."_reaction_type - (reactionTypeID, title, showOrder, iconFile) - VALUES (?, ?, ?, ?)"; + $sql = "INSERT IGNORE INTO wcf".WCF_N."_reaction_type + (reactionTypeID, title, showOrder, iconFile) + VALUES (?, ?, ?, ?)"; $statement = WCF::getDB()->prepareStatement($sql); for ($i = 0, $length = count($reactions); $i < $length; $i++) { $reactionTypeID = $i + 1; @@ -90,8 +96,33 @@ try { dislikes = 0"; WCF::getDB()->prepareStatement($sql)->execute(); - $statement = WCF::getDB()->prepareStatement('ALTER TABLE wcf'.WCF_N.'_like ADD FOREIGN KEY (reactionTypeID) REFERENCES wcf'.WCF_N.'_reaction_type (reactionTypeID) ON DELETE CASCADE'); - $statement->execute(); + $dbEditor = WCF::getDB()->getEditor(); + $foreignKeys = $dbEditor->getForeignKeys('wcf'.WCF_N.'_like'); + $expectedKey = 'fe5076ee92a558ce8177e3afbfc3dafc_fk'; + $hasExpectedKey = false; + + // Find the previously added foreign key, in case the upgrade was interrupted before. + foreach ($foreignKeys as $indexName => $definition) { + if ($indexName === $expectedKey) { + $hasExpectedKey = true; + break; + } + + if ($definition['referencedTable'] === 'wcf'.WCF_N.'_reaction_type') { + if (count($definition['columns']) === 1 && $definition['columns'][0] === 'reactionTypeID') { + $dbEditor->dropForeignKey('wcf'.WCF_N.'_like', $indexName); + } + } + } + + if (!$hasExpectedKey) { + $dbEditor->addForeignKey('wcf' . WCF_N . '_like', $expectedKey, [ + 'columns' => 'reactionTypeID', + 'referencedColumns' => 'reactionTypeID', + 'referencedTable' => 'wcf'.WCF_N.'_reaction_type', + 'ON DELETE' => 'CASCADE', + ]); + } WCF::getDB()->commitTransaction(); }