Idempotent modification of the database
authorAlexander Ebert <ebert@woltlab.com>
Mon, 6 Jan 2020 15:29:32 +0000 (16:29 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 6 Jan 2020 15:29:32 +0000 (16:29 +0100)
Fixes #3125

com.woltlab.wcf/files_pre.tar
com.woltlab.wcf/update_5.2.sql
wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php

index 5a42367369b780ab8b6e02c2c754bacd99c6d319..4d8e91d117a10bcb5585328b63907786ea89462f 100644 (file)
Binary files a/com.woltlab.wcf/files_pre.tar and b/com.woltlab.wcf/files_pre.tar differ
index 5fbc40d4f136a3ee28aafc802c7fe2955e56a1e6..896f750add91499d151335d65c2d0543daee6e5f 100644 (file)
@@ -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
index 9ef5c88efabf2dcaef23155296855d696008aef1..4c4338571f5bbdaf3803ddf816abd30bbeaae969 100644 (file)
@@ -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();
 }