Remove extraneous foreign keys on `wcf1_like.reactionTypeID`
authorAlexander Ebert <ebert@woltlab.com>
Sun, 19 Nov 2023 15:29:23 +0000 (16:29 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 19 Nov 2023 15:29:23 +0000 (16:29 +0100)
For some reason there are sometimes multiple or incorrectly named foreign keys on that column. This causes an issue when trying to remove the “unnamed” foreign key.

See https://www.woltlab.com/community/thread/302748-update-5-5-auf-6-0-x/

com.woltlab.wcf/package.xml
wcfsetup/install/files/acp/database/update_com.woltlab.wcf_6.0_reaction_fk_step1.php [deleted file]
wcfsetup/install/files/acp/update_com.woltlab.wcf_6.0_reaction_fk_step1.php [new file with mode: 0644]

index 81e9edd1374c10f6a07f19265d49680ed1675aa5..46ae275bc2cf7ca9708d71d10aa8618b8c2529dc 100644 (file)
@@ -99,7 +99,7 @@ tar cvf com.woltlab.wcf/files_pre_check.tar -C wcfsetup/install/files/ \
                <instruction type="script" run="standalone">acp/update_com.woltlab.wcf_6.0_landingPage.php</instruction>
 
                <!-- Migrate the database as early as possible. -->
-               <instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_6.0_reaction_fk_step1.php</instruction>
+               <instruction type="script" run="standalone">acp/update_com.woltlab.wcf_6.0_reaction_fk_step1.php</instruction>
                <instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_6.0_reaction_fk_step2.php</instruction>
 
                <instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_6.0.php</instruction>
diff --git a/wcfsetup/install/files/acp/database/update_com.woltlab.wcf_6.0_reaction_fk_step1.php b/wcfsetup/install/files/acp/database/update_com.woltlab.wcf_6.0_reaction_fk_step1.php
deleted file mode 100644 (file)
index 21889c5..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-
-/**
- * Drops the reactionTypeID foreign key to later recreate it with the correct name.
- *
- * @author Tim Duesterhus
- * @copyright 2001-2022 WoltLab GmbH
- * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- */
-
-use wcf\system\database\table\index\DatabaseTableForeignKey;
-use wcf\system\database\table\PartialDatabaseTable;
-
-return [
-    PartialDatabaseTable::create('wcf1_like')
-        ->foreignKeys([
-            DatabaseTableForeignKey::create()
-                ->columns(['reactionTypeID'])
-                ->referencedTable('wcf1_reaction_type')
-                ->referencedColumns(['reactionTypeID'])
-                ->onDelete('CASCADE')
-                ->drop(),
-        ]),
-];
diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_6.0_reaction_fk_step1.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_6.0_reaction_fk_step1.php
new file mode 100644 (file)
index 0000000..d521442
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace wcf\acp;
+
+/**
+ * Remove extraneous foreign keys on `wcf1_like.reactionTypeID` that use generic
+ * `*_ibfk` names
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ */
+
+use wcf\system\WCF;
+
+$databaseEditor = WCF::getDB()->getEditor();
+$tableName = 'wcf' . WCF_N . '_like';
+foreach ($databaseEditor->getForeignKeys($tableName) as $foreignKey => $columnData) {
+    if ($columnData['columns'] !== ['reactionTypeID']) {
+        continue;
+    }
+
+    if ($columnData['referencedColumns'] !== ['reactionTypeID']) {
+        continue;
+    }
+
+    if ($columnData['referencedTable'] !== 'wcf' . WCF_N . '_reaction_type') {
+        continue;
+    }
+
+    $databaseEditor->dropForeignKey($tableName, $foreignKey);
+
+    $sql = "DELETE FROM wcf1_package_installation_sql_log
+            WHERE       sqlTable = ?
+                    AND sqlIndex = ?";
+    $statement = WCF::getDB()->prepare($sql);
+    $statement->execute([
+        $tableName,
+        $foreignKey,
+    ]);
+}