Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit c30ec4f

Browse files
committed
Ensure that the thread_id column is non-null and then require it to be non-null.
1 parent aa1ca0a commit c30ec4f

File tree

4 files changed

+151
-3
lines changed

4 files changed

+151
-3
lines changed

synapse/storage/schema/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@
8585

8686

8787
SCHEMA_COMPAT_VERSION = (
88-
# The groups tables are no longer accessible, so synapses with SCHEMA_VERSION < 72
89-
# could break.
90-
72
88+
# The threads_id column must exist for event_push_actions, event_push_summary,
89+
# receipts_linearized, and receipts_graph.
90+
73
9191
)
9292
"""Limit on how far the synapse codebase can be rolled back without breaking db compat
9393
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright 2022 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- Forces the background updates from 06thread_notifications.sql to run in the
17+
-- foreground as code will now require those to be "done".
18+
19+
DELETE FROM background_updates WHERE update_name = 'event_push_backfill_thread_id';
20+
21+
-- Overwrite any null thread_id columns.
22+
UPDATE event_push_actions_staging SET thread_id = 'main' WHERE thread_id IS NULL;
23+
UPDATE event_push_actions SET thread_id = 'main' WHERE thread_id IS NULL;
24+
UPDATE event_push_summary SET thread_id = 'main' WHERE thread_id IS NULL;
25+
26+
-- Do not run the event_push_summary_unique_index job if it is pending; the
27+
-- thread_id field will be made required.
28+
DELETE FROM background_updates WHERE update_name = 'event_push_summary_unique_index';
29+
DROP INDEX IF EXISTS event_push_summary_unique_index;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Copyright 2022 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- The columns can now be made non-nullable.
17+
ALTER TABLE event_push_actions_staging ALTER COLUMN thread_id SET NOT NULL;
18+
ALTER TABLE event_push_actions ALTER COLUMN thread_id SET NOT NULL;
19+
ALTER TABLE event_push_summary ALTER COLUMN thread_id SET NOT NULL;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright 2022 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- SQLite doesn't support modifying columns to an existing table, so it must
17+
-- be recreated.
18+
19+
-- Create the new tables.
20+
CREATE TABLE event_push_actions_staging_new (
21+
event_id TEXT NOT NULL,
22+
user_id TEXT NOT NULL,
23+
actions TEXT NOT NULL,
24+
notif SMALLINT NOT NULL,
25+
highlight SMALLINT NOT NULL,
26+
unread SMALLINT,
27+
thread_id TEXT NOT NULL
28+
);
29+
30+
CREATE TABLE event_push_actions_new (
31+
room_id TEXT NOT NULL,
32+
event_id TEXT NOT NULL,
33+
user_id TEXT NOT NULL,
34+
profile_tag VARCHAR(32),
35+
actions TEXT NOT NULL,
36+
topological_ordering BIGINT,
37+
stream_ordering BIGINT,
38+
notif SMALLINT,
39+
highlight SMALLINT,
40+
unread SMALLINT,
41+
thread_id TEXT NOT NULL,
42+
CONSTRAINT event_id_user_id_profile_tag_uniqueness UNIQUE (room_id, event_id, user_id, profile_tag)
43+
);
44+
45+
CREATE TABLE event_push_summary_new (
46+
user_id TEXT NOT NULL,
47+
room_id TEXT NOT NULL,
48+
notif_count BIGINT NOT NULL,
49+
stream_ordering BIGINT NOT NULL,
50+
unread_count BIGINT,
51+
last_receipt_stream_ordering BIGINT,
52+
thread_id TEXT NOT NULL
53+
);
54+
55+
-- Swap the indexes.
56+
DROP INDEX IF EXISTS event_push_actions_staging_id;
57+
CREATE INDEX event_push_actions_staging_id ON event_push_actions_staging_new(event_id);
58+
59+
DROP INDEX IF EXISTS event_push_actions_room_id_user_id;
60+
DROP INDEX IF EXISTS event_push_actions_rm_tokens;
61+
DROP INDEX IF EXISTS event_push_actions_stream_ordering;
62+
DROP INDEX IF EXISTS event_push_actions_u_highlight;
63+
DROP INDEX IF EXISTS event_push_actions_highlights_index;
64+
CREATE INDEX event_push_actions_room_id_user_id on event_push_actions_new(room_id, user_id);
65+
CREATE INDEX event_push_actions_rm_tokens on event_push_actions_new( user_id, room_id, topological_ordering, stream_ordering );
66+
CREATE INDEX event_push_actions_stream_ordering on event_push_actions_new( stream_ordering, user_id );
67+
CREATE INDEX event_push_actions_u_highlight ON event_push_actions_new (user_id, stream_ordering);
68+
CREATE INDEX event_push_actions_highlights_index ON event_push_actions_new (user_id, room_id, topological_ordering, stream_ordering);
69+
70+
-- Copy the data.
71+
INSERT INTO event_push_actions_staging_new (event_id, user_id, actions, notif, highlight, unread, thread_id)
72+
SELECT event_id, user_id, actions, notif, highlight, unread, thread_id
73+
FROM event_push_actions_staging;
74+
75+
INSERT INTO event_push_actions_new (room_id, event_id, user_id, profile_tag, actions, topological_ordering, stream_ordering, notif, highlight, unread, thread_id)
76+
SELECT room_id, event_id, user_id, profile_tag, actions, topological_ordering, stream_ordering, notif, highlight, unread, thread_id
77+
FROM event_push_actions;
78+
79+
INSERT INTO event_push_summary_new (user_id, room_id, notif_count, stream_ordering, unread_count, last_receipt_stream_ordering, thread_id)
80+
SELECT user_id, room_id, notif_count, stream_ordering, unread_count, last_receipt_stream_ordering, thread_id
81+
FROM event_push_summary;
82+
83+
-- Drop the old tables.
84+
DROP TABLE event_push_actions_staging;
85+
DROP TABLE event_push_actions;
86+
DROP TABLE event_push_summary;
87+
88+
-- Rename the tables.
89+
ALTER TABLE event_push_actions_staging_new RENAME TO event_push_actions_staging;
90+
ALTER TABLE event_push_actions_new RENAME TO event_push_actions;
91+
ALTER TABLE event_push_summary_new RENAME TO event_push_summary;
92+
93+
-- Re-run background updates from 72/02event_push_actions_index.sql and
94+
-- 72/06thread_notifications.sql.
95+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
96+
(7302, 'event_push_summary_unique_index2', '{}')
97+
ON CONFLICT (update_name) DO NOTHING;
98+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
99+
(7302, 'event_push_actions_stream_highlight_index', '{}')
100+
ON CONFLICT (update_name) DO NOTHING;

0 commit comments

Comments
 (0)