forked-synapse/synapse/storage/schema/main/delta/71/01rebuild_event_edges.sql.sqlite
Richard van der Hoff 75fb10ee45
Clean up schema for event_edges (#12893)
* Remove redundant references to `event_edges.room_id`

We don't need to care about the room_id here, because we are already checking
the event id.

* Clean up the event_edges table

We make a number of changes to `event_edges`:

 * We give the `room_id` and `is_state` columns defaults (null and false
   respectively) so that we can stop populating them.
 * We drop any rows that have `is_state` set true - they should no longer
   exist.
 * We drop any rows that do not exist in `events` - these should not exist
   either.
 * We drop the old unique constraint on all the colums, which wasn't much use.
 * We create a new unique index on `(event_id, prev_event_id)`.
 * We add a foreign key constraint to `events`.

These happen rather differently depending on whether we are on Postgres or
SQLite. For SQLite, we just rebuild the whole table, copying only the rows we
want to keep. For Postgres, we try to do things in the background as much as
possible.

* Stop populating `event_edges.room_id` and `is_state`

We can just rely on the defaults.
2022-06-15 12:29:42 +01:00

48 lines
1.8 KiB
Plaintext

/* Copyright 2022 The Matrix.org Foundation C.I.C
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-- We're going to stop populating event_edges.room_id and event_edges.is_state,
-- which means we now need to give them defaults.
--
-- We also take the opportunity to:
-- - drop any rows with is_state=True (these were populated a long time ago, but
-- are no longer used.)
-- - drop any rows which do not correspond to entries in `events`
-- - tighten the unique index so that it applies just to (event_id, prev_event_id)
-- - drop the "ev_edges_id" index, which is redundant to the above.
-- - add a foreign key constraint from event_id to `events`
CREATE TABLE new_event_edges (
event_id TEXT NOT NULL,
prev_event_id TEXT NOT NULL,
room_id TEXT NULL,
is_state BOOL NOT NULL DEFAULT 0,
FOREIGN KEY(event_id) REFERENCES events(event_id)
);
INSERT INTO new_event_edges
SELECT ee.event_id, ee.prev_event_id, ee.room_id, ee.is_state
FROM event_edges ee JOIN events ev USING (event_id)
WHERE NOT ee.is_state;
DROP TABLE event_edges;
ALTER TABLE new_event_edges RENAME TO event_edges;
CREATE UNIQUE INDEX event_edges_event_id_prev_event_id_idx
ON event_edges (event_id, prev_event_id);
CREATE INDEX ev_edges_prev_id ON event_edges (prev_event_id);