Insert update script to remove duplicate entries
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 25 Jan 2024 11:58:38 +0000 (12:58 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Thu, 25 Jan 2024 11:58:38 +0000 (12:58 +0100)
Insert database unique key

wcfsetup/install/files/acp/database/update_com.woltlab.wcf_6.1.php
wcfsetup/install/files/acp/update_com.woltlab.wcf_6.1_messageEmbeddedObject.php [new file with mode: 0644]
wcfsetup/setup/db/install.sql

index 8927551c8a01e87b50fafaf15f78d75dda0f993f..9d384563e6ff07305b78aff9c115fbb278f93985 100644 (file)
@@ -14,6 +14,7 @@ use wcf\system\database\table\column\NotNullVarchar191DatabaseTableColumn;
 use wcf\system\database\table\DatabaseTable;
 use wcf\system\database\table\index\DatabaseTableForeignKey;
 use wcf\system\database\table\index\DatabaseTableIndex;
+use wcf\system\database\table\PartialDatabaseTable;
 
 return [
     DatabaseTable::create('wcf1_acp_dashboard_box_to_user')
@@ -35,4 +36,10 @@ return [
                 ->referencedColumns(['userID'])
                 ->onDelete('CASCADE'),
         ]),
+    PartialDatabaseTable::create('wcf1_message_embedded_object')
+        ->indices([
+            DatabaseTableIndex::create('id')
+                ->type(DatabaseTableIndex::UNIQUE_TYPE)
+                ->columns(['messageObjectTypeID', 'messageID', 'embeddedObjectTypeID', 'embeddedObjectID']),
+        ])
 ];
diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_6.1_messageEmbeddedObject.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_6.1_messageEmbeddedObject.php
new file mode 100644 (file)
index 0000000..1c4c168
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * Deletes duplicate entries in the message_embedded_object table.
+ *
+ * @author  Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ */
+
+use wcf\system\WCF;
+
+WCF::getDB()->beginTransaction();
+
+$sql = "SELECT   messageObjectTypeID, messageID, embeddedObjectTypeID, embeddedObjectID, COUNT(*) as counter
+        FROM     wcf1_message_embedded_object
+        GROUP BY messageObjectTypeID, messageID, embeddedObjectTypeID, embeddedObjectID
+        HAVING   counter > 1";
+$statement = WCF::getDB()->prepare($sql);
+$statement->execute();
+
+$sql = "DELETE FROM wcf1_message_embedded_object
+        WHERE       messageObjectTypeID = ?
+        AND         messageID = ?
+        AND         embeddedObjectTypeID = ?
+        AND         embeddedObjectID = ?
+        LIMIT       ?";
+$deleteStatement = WCF::getDB()->prepare($sql);
+
+while ($row = $statement->fetchArray()) {
+    $deleteStatement->execute([
+        $row['messageObjectTypeID'],
+        $row['messageID'],
+        $row['embeddedObjectTypeID'],
+        $row['embeddedObjectID'],
+        $row['counter'] - 1
+    ]);
+}
+
+WCF::getDB()->commitTransaction();
index 847dad830817eb48a5fa0ee0c212dbf966df423e..67f7ba4c73aa24b8f3c878aebc2b26326aa93618 100644 (file)
@@ -799,7 +799,8 @@ CREATE TABLE wcf1_message_embedded_object (
        embeddedObjectTypeID INT(10) NOT NULL,
        embeddedObjectID INT(10) NOT NULL,
 
-       KEY (messageObjectTypeID, messageID)
+       KEY (messageObjectTypeID, messageID),
+       UNIQUE KEY id (messageObjectTypeID, messageID, embeddedObjectTypeID, embeddedObjectID)
 );
 
 DROP TABLE IF EXISTS wcf1_moderation_queue;