summary refs log tree commit diff
diff options
context:
space:
mode:
authorSean Quah <seanq@matrix.org>2023-04-14 23:34:38 +0100
committerSean Quah <seanq@matrix.org>2023-04-15 02:52:42 +0100
commit03ee93ee1a07dc21c03f27380ccc7dceda5bc70b (patch)
tree85a88f8a4dfb5c81123565077888c44cda18d0aa
parentBump the schema version (diff)
downloadsynapse-03ee93ee1a07dc21c03f27380ccc7dceda5bc70b.tar.xz
Add `profiles.full_user_id` column
Signed-off-by: Sean Quah <seanq@matrix.org>
-rw-r--r--synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.postgres32
-rw-r--r--synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.sqlite40
2 files changed, 72 insertions, 0 deletions
diff --git a/synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.postgres b/synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.postgres
new file mode 100644
index 0000000000..a125dac419
--- /dev/null
+++ b/synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.postgres
@@ -0,0 +1,32 @@
+/* Copyright 2023 The Matrix.org Foundation C.I.C
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ALTER TABLE profiles ADD COLUMN full_user_id TEXT;
+
+-- Add a new constraint on the new column, mirroring the `profiles_user_id_key`
+-- constraint.
+ALTER TABLE ONLY profiles
+    ADD CONSTRAINT profiles_full_user_id_key UNIQUE (full_user_id);
+
+-- Also ensure that new rows have the `full_user_id` field populated.
+-- TODO: Move this to phase two of the migration. In a multi-worker deployment, it will
+--       prevent un-updated workers from doing any UPDATEs. That is, it effectively
+--       prevents rollback of Synapse to an earlier version when the column has not been
+--       fully populated.
+ALTER TABLE ONLY profiles
+    ADD CONSTRAINT profiles_full_user_id_not_null CHECK (full_user_id IS NOT NULL) NOT VALID;
+-- `profiles` can contain on the order of 10s/100s of millions of rows. We use
+-- `NOT VALID` so that we do not lock the table to check existing rows. New rows will
+-- still be checked.
diff --git a/synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.sqlite b/synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.sqlite
new file mode 100644
index 0000000000..210d189756
--- /dev/null
+++ b/synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.sqlite
@@ -0,0 +1,40 @@
+/* Copyright 2023 The Matrix.org Foundation C.I.C
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ALTER TABLE profiles ADD COLUMN full_user_id TEXT;
+
+-- Add a new constraint on the new column, mirroring the `user_id` constraint.
+--
+-- SQLite doesn't support modifying constraints on an existing table, so it must be
+-- recreated.
+CREATE TABLE profiles_new(
+    full_user_id TEXT,
+    user_id TEXT NOT NULL,
+    displayname TEXT,
+    avatar_url TEXT,
+    UNIQUE (full_user_id),
+    UNIQUE (user_id)
+);
+
+-- Copy the data.
+INSERT INTO profiles_new (full_user_id, user_id, displayname, avatar_url)
+    SELECT NULL, user_id, displayname, avatar_url
+    FROM profiles;
+
+-- Drop the old table.
+DROP TABLE profiles;
+
+-- Rename the table.
+ALTER TABLE profiles_new RENAME TO profiles;