1
+ /*
2
+ * Copyright 2020 The Matrix.org Foundation C.I.C.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ -- Dropping last_used column from access_tokens table.
18
+
19
+ CREATE TABLE access_tokens2 (
20
+ id BIGINT PRIMARY KEY,
21
+ user_id TEXT NOT NULL,
22
+ device_id TEXT,
23
+ token TEXT NOT NULL,
24
+ valid_until_ms BIGINT,
25
+ puppets_user_id TEXT,
26
+ last_validated BIGINT,
27
+ UNIQUE(token)
28
+ );
29
+
30
+ INSERT INTO access_tokens2(id, user_id, device_id, token)
31
+ SELECT id, user_id, device_id, token FROM access_tokens;
32
+
33
+ DROP TABLE access_tokens;
34
+ ALTER TABLE access_tokens2 RENAME TO access_tokens;
35
+
36
+ CREATE INDEX access_tokens_device_id ON access_tokens (user_id, device_id);
37
+
38
+
39
+ -- Re-adding foreign key reference in event_txn_id table
40
+
41
+ CREATE TABLE event_txn_id2 (
42
+ event_id TEXT NOT NULL,
43
+ room_id TEXT NOT NULL,
44
+ user_id TEXT NOT NULL,
45
+ token_id BIGINT NOT NULL,
46
+ txn_id TEXT NOT NULL,
47
+ inserted_ts BIGINT NOT NULL,
48
+ FOREIGN KEY (event_id)
49
+ REFERENCES events (event_id) ON DELETE CASCADE,
50
+ FOREIGN KEY (token_id)
51
+ REFERENCES access_tokens (id) ON DELETE CASCADE
52
+ );
53
+
54
+ INSERT INTO event_txn_id2(event_id, room_id, user_id, token_id, txn_id, inserted_ts)
55
+ SELECT event_id, room_id, user_id, token_id, txn_id, inserted_ts FROM event_txn_id;
56
+
57
+ DROP TABLE event_txn_id;
58
+ ALTER TABLE event_txn_id2 RENAME TO event_txn_id;
59
+
60
+ CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_event_id ON event_txn_id(event_id);
61
+ CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_txn_id ON event_txn_id(room_id, user_id, token_id, txn_id);
62
+ CREATE INDEX IF NOT EXISTS event_txn_id_ts ON event_txn_id(inserted_ts);
0 commit comments