Add profiles.full_user_id column

Signed-off-by: Sean Quah <seanq@matrix.org>
This commit is contained in:
Sean Quah 2023-04-14 23:34:38 +01:00
parent 8810abab33
commit 03ee93ee1a
2 changed files with 72 additions and 0 deletions

View file

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

View file

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