Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix schema delta error in 1.85 (#15739)
Browse files Browse the repository at this point in the history
Some users seem to have multiple rows per user / room with a null thread
ID, which we need to handle.
  • Loading branch information
erikjohnston committed Jun 7, 2023
1 parent 7acf7f2 commit f7c6553
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/15739.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in schema delta that broke upgrades for some deployments. Introduced in v1.85.0.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ UPDATE event_push_actions_staging SET thread_id = 'main' WHERE thread_id IS NULL
UPDATE event_push_actions SET thread_id = 'main' WHERE thread_id IS NULL;

-- Empirically we can end up with entries in the push summary table with both a
-- `NULL` and `main` thread ID, which causes the update below to fail. We fudge
-- `NULL` and `main` thread ID, which causes the insert below to fail. We fudge
-- this by deleting any `NULL` rows that have a corresponding `main`.
DELETE FROM event_push_summary AS a WHERE thread_id IS NULL AND EXISTS (
SELECT 1 FROM event_push_summary AS b
WHERE b.thread_id = 'main' AND a.user_id = b.user_id AND a.room_id = b.room_id
);
UPDATE event_push_summary SET thread_id = 'main' WHERE thread_id IS NULL;
-- Copy the NULL threads to have a 'main' thread ID.
--
-- Note: Some people seem to have duplicate rows with a `NULL` thread ID, in
-- which case we just fudge it with using MAX of the values. The counts *may* be
-- wrong for such rooms, but a) its an edge case, and b) they'll be fixed when
-- the user reads the room.
INSERT INTO event_push_summary (user_id, room_id, notif_count, stream_ordering, unread_count, last_receipt_stream_ordering, thread_id)
SELECT user_id, room_id, MAX(notif_count), MAX(stream_ordering), MAX(unread_count), MAX(last_receipt_stream_ordering), 'main'
FROM event_push_summary
WHERE thread_id IS NULL
GROUP BY user_id, room_id, thread_id;

DELETE FROM event_push_summary AS a WHERE thread_id IS NULL;

-- Drop the background updates to calculate the indexes used to find null thread_ids.
DELETE FROM background_updates WHERE update_name = 'event_push_actions_thread_id_null';
Expand Down

0 comments on commit f7c6553

Please sign in to comment.