Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migration)_: fix the pinned_messages migration when a message is deleted #6231

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading