From 7a70d4780dd7c0833cd2a4bf8e277cf35e28e022 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 22 Aug 2023 17:52:31 +0100 Subject: [PATCH] Migration to clean up bogus rooms These rooms have invalid state snapshots, leading to correctness problems. The existence of such snapshots leaves us open to performance problems due to gappy state updates. We have prevented invalid state snapshots from being formed (#255, #266) in the future. The remaining hole is to discard existing invalid snapshots. Closes #256. --- .../20230822174049_bogus_snapshot_cleanup.sql | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 state/migrations/20230822174049_bogus_snapshot_cleanup.sql diff --git a/state/migrations/20230822174049_bogus_snapshot_cleanup.sql b/state/migrations/20230822174049_bogus_snapshot_cleanup.sql new file mode 100644 index 00000000..063bc1ae --- /dev/null +++ b/state/migrations/20230822174049_bogus_snapshot_cleanup.sql @@ -0,0 +1,32 @@ +-- +goose Up +-- This migration runs a one-time cleanup job to delete rooms with dodgy state snapshots. +-- A room is dodgy if its current snapshot lacks a create event. +BEGIN; + +CREATE TEMPORARY TABLE bogus_rooms AS ( + SELECT room_id, current_snapshot_id + FROM syncv3_rooms + WHERE room_id NOT IN ( + SELECT room_id + FROM syncv3_events + WHERE event_type = 'm.room.create' + AND state_key = '' + ) +); + +DELETE +FROM syncv3_events +WHERE room_id IN (SELECT room_id FROM bogus_rooms); + +DELETE +FROM syncv3_snapshots +WHERE room_id IN (SELECT room_id FROM bogus_rooms); + +DELETE +FROM syncv3_rooms +WHERE room_id IN (SELECT room_id FROM bogus_rooms); + +COMMIT; + +-- +goose Down +-- downgrading is a no-op.