|
| 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