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

Commit 9ef7077

Browse files
committed
Update constraints and indexes now that thread ID is used.
1 parent 625f43a commit 9ef7077

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
-- Drop constraint on (room_id, receipt_type, user_id).
17+
18+
-- Rebuild the unique constraint with the thread_id.
19+
ALTER TABLE receipts_linearized
20+
DROP CONSTRAINT receipts_linearized_uniqueness;
21+
22+
ALTER TABLE receipts_graph
23+
DROP CONSTRAINT receipts_graph_uniqueness;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
-- Drop constraint on (room_id, receipt_type, user_id).
17+
--
18+
-- SQLite doesn't support modifying constraints to an existing table, so it must
19+
-- be recreated.
20+
21+
-- Create the new tables.
22+
CREATE TABLE receipts_linearized_new (
23+
stream_id BIGINT NOT NULL,
24+
room_id TEXT NOT NULL,
25+
receipt_type TEXT NOT NULL,
26+
user_id TEXT NOT NULL,
27+
event_id TEXT NOT NULL,
28+
thread_id TEXT,
29+
data TEXT NOT NULL,
30+
CONSTRAINT receipts_linearized_uniqueness_thread UNIQUE (room_id, receipt_type, user_id, thread_id)
31+
);
32+
33+
CREATE TABLE receipts_graph_new (
34+
room_id TEXT NOT NULL,
35+
receipt_type TEXT NOT NULL,
36+
user_id TEXT NOT NULL,
37+
event_ids TEXT NOT NULL,
38+
thread_id TEXT,
39+
data TEXT NOT NULL,
40+
CONSTRAINT receipts_graph_uniqueness_thread UNIQUE (room_id, receipt_type, user_id, thread_id)
41+
);
42+
43+
-- Drop the old indexes.
44+
DROP INDEX IF EXISTS receipts_linearized_id;
45+
DROP INDEX IF EXISTS receipts_linearized_room_stream;
46+
DROP INDEX IF EXISTS receipts_linearized_user;
47+
48+
-- Copy the data.
49+
INSERT INTO receipts_linearized_new (stream_id, room_id, receipt_type, user_id, event_id, data)
50+
SELECT stream_id, room_id, receipt_type, user_id, event_id, data
51+
FROM receipts_linearized;
52+
INSERT INTO receipts_graph_new (room_id, receipt_type, user_id, event_ids, data)
53+
SELECT room_id, receipt_type, user_id, event_ids, data
54+
FROM receipts_graph;
55+
56+
-- Drop the old tables.
57+
DROP TABLE receipts_linearized;
58+
DROP TABLE receipts_graph;
59+
60+
-- Rename the tables.
61+
ALTER TABLE receipts_linearized_new RENAME TO receipts_linearized;
62+
ALTER TABLE receipts_graph_new RENAME TO receipts_graph;
63+
64+
-- Create the indices.
65+
CREATE INDEX receipts_linearized_id ON receipts_linearized( stream_id );
66+
CREATE INDEX receipts_linearized_room_stream ON receipts_linearized( room_id, stream_id );
67+
CREATE INDEX receipts_linearized_user ON receipts_linearized( user_id );
68+
69+
-- Re-run background updates from 72/08thread_receipts.sql.
70+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
71+
(7211, 'receipts_linearized_unique_index', '{}')
72+
ON CONFLICT (update_name) DO NOTHING;
73+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
74+
(7211, 'receipts_graph_unique_index', '{}')
75+
ON CONFLICT (update_name) DO NOTHING;

0 commit comments

Comments
 (0)