From 75c96dde342a25a87418240ae11737336fa46536 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 20 Dec 2024 10:27:00 -0500 Subject: [PATCH] fix(migration)_: fix the pinned_messages migration when a message is deleted Turns out if you had old pinned messages that were still associated with deleted user messages, then the original migration didn't work. This fixes it. If someone already ran the migration successfully because they didn't have deleted pinned messages, then it just won't run again and everything is already fine. If someone was blocked because of the migration, then it will just run fine now. --- ...28521_pinned_messages_add_on_delete_clause.up.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/protocol/migrations/sqlite/1733428521_pinned_messages_add_on_delete_clause.up.sql b/protocol/migrations/sqlite/1733428521_pinned_messages_add_on_delete_clause.up.sql index 83c7de93a1..f876e0b038 100644 --- a/protocol/migrations/sqlite/1733428521_pinned_messages_add_on_delete_clause.up.sql +++ b/protocol/migrations/sqlite/1733428521_pinned_messages_add_on_delete_clause.up.sql @@ -1,3 +1,13 @@ +-- Delete pinned messages that are not associated with any user message +-- This makes sure the migration below works even if a message is deleted +DELETE FROM pin_messages +WHERE NOT EXISTS ( + SELECT 1 + FROM user_messages + WHERE user_messages.id = pin_messages.message_id +); + +-- Add the ON DELETE CASCADE clause to the foreign key constraint CREATE TABLE pin_messages_new ( id VARCHAR PRIMARY KEY NOT NULL, message_id VARCHAR NOT NULL, @@ -10,10 +20,12 @@ CREATE TABLE pin_messages_new ( FOREIGN KEY (message_id) REFERENCES user_messages(id) ON DELETE CASCADE ); +-- Copy the data from the old table to the new table INSERT INTO pin_messages_new (id, message_id, whisper_timestamp, chat_id, local_chat_id, clock_value, pinned, pinned_by) SELECT id, message_id, whisper_timestamp, chat_id, local_chat_id, clock_value, pinned, pinned_by FROM pin_messages; +-- Drop the old table and rename the new table DROP TABLE pin_messages; ALTER TABLE pin_messages_new RENAME TO pin_messages;