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

Commit b6e4cf2

Browse files
committed
Update constraints and indexes now that thread ID is used.
1 parent e201626 commit b6e4cf2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-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,76 @@
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+
event_stream_ordering BIGINT,
30+
data TEXT NOT NULL,
31+
CONSTRAINT receipts_linearized_uniqueness_thread UNIQUE (room_id, receipt_type, user_id, thread_id)
32+
);
33+
34+
CREATE TABLE receipts_graph_new (
35+
room_id TEXT NOT NULL,
36+
receipt_type TEXT NOT NULL,
37+
user_id TEXT NOT NULL,
38+
event_ids TEXT NOT NULL,
39+
thread_id TEXT,
40+
data TEXT NOT NULL,
41+
CONSTRAINT receipts_graph_uniqueness_thread UNIQUE (room_id, receipt_type, user_id, thread_id)
42+
);
43+
44+
-- Drop the old indexes.
45+
DROP INDEX IF EXISTS receipts_linearized_id;
46+
DROP INDEX IF EXISTS receipts_linearized_room_stream;
47+
DROP INDEX IF EXISTS receipts_linearized_user;
48+
49+
-- Copy the data.
50+
INSERT INTO receipts_linearized_new (stream_id, room_id, receipt_type, user_id, event_id, data)
51+
SELECT stream_id, room_id, receipt_type, user_id, event_id, data
52+
FROM receipts_linearized;
53+
INSERT INTO receipts_graph_new (room_id, receipt_type, user_id, event_ids, data)
54+
SELECT room_id, receipt_type, user_id, event_ids, data
55+
FROM receipts_graph;
56+
57+
-- Drop the old tables.
58+
DROP TABLE receipts_linearized;
59+
DROP TABLE receipts_graph;
60+
61+
-- Rename the tables.
62+
ALTER TABLE receipts_linearized_new RENAME TO receipts_linearized;
63+
ALTER TABLE receipts_graph_new RENAME TO receipts_graph;
64+
65+
-- Create the indices.
66+
CREATE INDEX receipts_linearized_id ON receipts_linearized( stream_id );
67+
CREATE INDEX receipts_linearized_room_stream ON receipts_linearized( room_id, stream_id );
68+
CREATE INDEX receipts_linearized_user ON receipts_linearized( user_id );
69+
70+
-- Re-run background updates from 72/08thread_receipts.sql.
71+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
72+
(7303, 'receipts_linearized_unique_index', '{}')
73+
ON CONFLICT (update_name) DO NOTHING;
74+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
75+
(7303, 'receipts_graph_unique_index', '{}')
76+
ON CONFLICT (update_name) DO NOTHING;

0 commit comments

Comments
 (0)