Skip to content

Commit

Permalink
fix(migration)_: fix the pinned_messages migration when a message is …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
jrainville committed Jan 6, 2025
1 parent f7d73be commit 75c96dd
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;

0 comments on commit 75c96dd

Please sign in to comment.