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.