summary refs log tree commit diff
diff options
context:
space:
mode:
authorH. Shay <hillerys@element.io>2023-06-28 11:09:19 -0700
committerH. Shay <hillerys@element.io>2023-06-28 11:09:19 -0700
commitfc8a2ff49c06527ac4063ac065ea229dd3fc6066 (patch)
tree8c76a8e09faac6a4eefe54235b3d229c5f0fac85
parentFix sqlite `user_filters` upgrade (#15817) (diff)
downloadsynapse-fc8a2ff49c06527ac4063ac065ea229dd3fc6066.tar.xz
add check constraint to `current_state_delta_stream`
-rw-r--r--synapse/storage/schema/main/delta/78/05_add_constraint_current_state_delta_stream.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/synapse/storage/schema/main/delta/78/05_add_constraint_current_state_delta_stream.py b/synapse/storage/schema/main/delta/78/05_add_constraint_current_state_delta_stream.py
new file mode 100644

index 0000000000..2d0aafce1e --- /dev/null +++ b/synapse/storage/schema/main/delta/78/05_add_constraint_current_state_delta_stream.py
@@ -0,0 +1,59 @@ +# Copyright 2023 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. + +from synapse.storage.database import LoggingTransaction +from synapse.storage.engines import BaseDatabaseEngine, Sqlite3Engine + + +def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None: + if isinstance(database_engine, Sqlite3Engine): + create_sql = """ + CREATE TABLE temp_current_state_delta_stream ( + stream_id bigint NOT NULL, + room_id text NOT NULL, + type text NOT NULL, + state_key text NOT NULL, + event_id text, + prev_event_id text, + instance_name text + CHECK (event_id != prev_event_id) + ) + """ + cur.execute(create_sql) + + copy_sql = """ + INSERT INTO temp_current_state_delta_stream SELECT * FROM current_state_delta_stream + """ + cur.execute(copy_sql) + + drop_sql = """ + DROP TABLE current_state_delta_stream + """ + cur.execute(drop_sql) + + alter_sql = """ + ALTER TABLE temp_current_state_delta_stream RENAME to current_state_delta_stream + """ + cur.execute(alter_sql) + + idx_sql = """ + CREATE INDEX current_state_delta_stream_idx ON current_state_delta_stream(stream_id) + """ + cur.execute(idx_sql) + else: + constraint_sql = """ + ALTER TABLE current_state_delta_stream ADD CONSTRAINT prev_event_id_and_event_id_not_equal + CHECK (prev_event_id != event_id) + """ + cur.execute(constraint_sql)