From 91db9ff82b1cab8c121f5ec2daa6de164a578ef3 Mon Sep 17 00:00:00 2001 From: Devin Zuczek Date: Sun, 27 Sep 2026 21:27:33 -0400 Subject: [PATCH] [rooms] fix room instance index --- .../0022_room_instance_unique_indexes.sql | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 apps/rooms/migrations/0022_room_instance_unique_indexes.sql diff --git a/apps/rooms/migrations/0022_room_instance_unique_indexes.sql b/apps/rooms/migrations/0022_room_instance_unique_indexes.sql new file mode 100644 index 0000000..9cf1b46 --- /dev/null +++ b/apps/rooms/migrations/0022_room_instance_unique_indexes.sql @@ -0,0 +1,26 @@ +-- Restore the two UNIQUE indexes on `room_instance`. +-- +-- Generated from packages/domain/src/room-instance-db.ts (ROOM_INSTANCE_SCHEMA_DDL) — +-- keep in sync. The DDL and migration 0004 have always declared them, but the deployed +-- table carried only the non-unique `idx_room_instance_room_id`: the two `CREATE UNIQUE +-- INDEX` statements never took, so nothing has enforced uniqueness on `id` or +-- `photon_room_id` in production. Two rows sharing one `photon_room_id` were found — +-- the signature of a replayed INSERT (same blob written twice), which the index would +-- have refused. Without the `id` index the `MAX(id) + 1` allocation in +-- createRoomInstance can also hand two concurrent matchmakes the same id, which the +-- index would have failed loudly instead of silently minting two instances. +-- +-- A UNIQUE index refuses to build over duplicates, so they're removed first. Instances +-- are live sessions, not records: of each duplicate set the earliest row (lowest rowid) +-- is kept and the rest dropped. A replayed insert's twins are identical, so nothing is +-- lost; presence rows point at instances by `id`, which the survivor still carries. +-- Dedupe by `id` first so a row that is a duplicate on both counts is only counted once. + +DELETE FROM room_instance + WHERE rowid NOT IN (SELECT MIN(rowid) FROM room_instance GROUP BY id); + +DELETE FROM room_instance + WHERE rowid NOT IN (SELECT MIN(rowid) FROM room_instance GROUP BY photon_room_id); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_room_instance_id ON room_instance (id); +CREATE UNIQUE INDEX IF NOT EXISTS idx_room_instance_photon_room_id ON room_instance (photon_room_id);