This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
N + 3
: Read from column full_user_id
rather than user_id
of tables profiles
and user_filters
#15649
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1e8908d
update `get_profileinfo` to read from column `full_user_id`
H-Shay 02981c3
update `get_profile_displayname` to read from column `full_user_id`
H-Shay 0bedee1
update `get_profile_avatar_url` to read from column `full_user_id`
H-Shay 5c7fa25
Merge branch 'develop' into shay/localpart_mig_3
H-Shay 14d7253
add functions to validate constraint/add check on tables `profiles` a…
H-Shay 382ba52
update `get_user_filter` and `add_user_filter` to read from column `f…
H-Shay 3559530
test upgrade functions
H-Shay ab51a41
newsfragement + fix stray space
H-Shay 39861db
add space back
H-Shay f6a484b
Merge branch 'develop' into shay/localpart_mig_3
H-Shay 17fa2c0
fix sql alignment
H-Shay 27bde22
remove tests
H-Shay ea60b0c
fix stray line
H-Shay cf458a7
fix stray line pt 2
H-Shay 2236845
Update synapse/storage/schema/main/delta/78/01_validate_and_update_pr…
H-Shay c0841e4
add some docstrings to the update function
H-Shay eebafcb
Merge branch 'shay/localpart_mig_3' of https://github.com/matrix-org/…
H-Shay 8561a54
update list of schema changes for 78
H-Shay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
synapse/storage/schema/main/delta/78/01_validate_and_update_profiles.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# 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. | ||
|
||
from synapse.config.homeserver import HomeServerConfig | ||
from synapse.storage.database import LoggingTransaction | ||
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine | ||
|
||
|
||
def run_upgrade( | ||
cur: LoggingTransaction, | ||
database_engine: BaseDatabaseEngine, | ||
config: HomeServerConfig, | ||
) -> None: | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hostname = config.server.server_name | ||
|
||
if isinstance(database_engine, PostgresEngine): | ||
# check if the constraint can be validated | ||
check_sql = """ | ||
SELECT user_id from profiles WHERE full_user_id IS NULL | ||
""" | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cur.execute(check_sql) | ||
res = cur.fetchall() | ||
|
||
if res: | ||
# there are rows the background job missed, finish them here before we validate the constraint | ||
process_rows_sql = """ | ||
UPDATE profiles | ||
SET full_user_id = '@' || user_id || ? | ||
H-Shay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
WHERE user_id IN (SELECT user_id FROM profiles WHERE | ||
full_user_id IS NULL) | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
cur.execute(process_rows_sql, (f":{hostname}",)) | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Now we can validate | ||
validate_sql = """ | ||
ALTER TABLE profiles VALIDATE CONSTRAINT full_user_id_not_null | ||
""" | ||
cur.execute(validate_sql) | ||
|
||
else: | ||
# in SQLite we need to rewrite the table to add the constraint | ||
cur.execute("DROP TABLE IF EXISTS temp_profiles") | ||
H-Shay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
create_sql = """ | ||
CREATE TABLE temp_profiles ( | ||
full_user_id text NOT NULL, | ||
user_id text, | ||
displayname text, | ||
avatar_url text, | ||
UNIQUE (full_user_id), | ||
UNIQUE (user_id) | ||
) | ||
""" | ||
cur.execute(create_sql) | ||
|
||
copy_sql = """ | ||
INSERT INTO temp_profiles ( | ||
user_id, | ||
displayname, | ||
avatar_url, | ||
full_user_id) | ||
SELECT user_id, displayname, avatar_url, '@' || user_id || ':' || ? FROM profiles | ||
""" | ||
cur.execute(copy_sql, (f"{hostname}",)) | ||
|
||
drop_sql = """ | ||
DROP TABLE profiles | ||
""" | ||
cur.execute(drop_sql) | ||
|
||
rename_sql = """ | ||
ALTER TABLE temp_profiles RENAME to profiles | ||
""" | ||
cur.execute(rename_sql) |
89 changes: 89 additions & 0 deletions
89
synapse/storage/schema/main/delta/78/02_validate_and_update_user_filters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 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. | ||
|
||
from synapse.config.homeserver import HomeServerConfig | ||
from synapse.storage.database import LoggingTransaction | ||
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine | ||
|
||
|
||
def run_upgrade( | ||
cur: LoggingTransaction, | ||
database_engine: BaseDatabaseEngine, | ||
config: HomeServerConfig, | ||
) -> None: | ||
hostname = config.server.server_name | ||
|
||
if isinstance(database_engine, PostgresEngine): | ||
# check if the constraint can be validated | ||
check_sql = """ | ||
SELECT user_id from user_filters WHERE full_user_id IS NULL | ||
""" | ||
cur.execute(check_sql) | ||
res = cur.fetchall() | ||
|
||
if res: | ||
# there are rows the background job missed, finish them here before we validate constraint | ||
process_rows_sql = """ | ||
UPDATE user_filters | ||
SET full_user_id = '@' || user_id || ? | ||
WHERE user_id IN (SELECT user_id FROM user_filters | ||
WHERE full_user_id IS NULL) | ||
""" | ||
cur.execute(process_rows_sql, (f":{hostname}",)) | ||
|
||
# Now we can validate | ||
validate_sql = """ | ||
ALTER TABLE user_filters VALIDATE CONSTRAINT full_user_id_not_null | ||
""" | ||
cur.execute(validate_sql) | ||
|
||
else: | ||
cur.execute("DROP TABLE IF EXISTS temp_user_filters") | ||
create_sql = """ | ||
CREATE TABLE temp_user_filters ( | ||
full_user_id text NOT NULL, | ||
user_id text NOT NULL, | ||
filter_id bigint NOT NULL, | ||
filter_json bytea NOT NULL, | ||
UNIQUE (full_user_id), | ||
UNIQUE (user_id) | ||
) | ||
""" | ||
cur.execute(create_sql) | ||
|
||
index_sql = """ | ||
CREATE UNIQUE INDEX IF NOT EXISTS user_filters_unique ON | ||
temp_user_filters (user_id, filter_id) | ||
""" | ||
cur.execute(index_sql) | ||
|
||
copy_sql = """ | ||
INSERT INTO temp_user_filters ( | ||
user_id, | ||
filter_id, | ||
filter_json, | ||
full_user_id) | ||
SELECT user_id, filter_id, filter_json, '@' || user_id || ':' || ? FROM user_filters | ||
""" | ||
cur.execute(copy_sql, (f"{hostname}",)) | ||
|
||
drop_sql = """ | ||
DROP TABLE user_filters | ||
""" | ||
cur.execute(drop_sql) | ||
|
||
rename_sql = """ | ||
ALTER TABLE temp_user_filters RENAME to user_filters | ||
""" | ||
cur.execute(rename_sql) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The notes from #15691 are very helpful when trying to understand if this PR is the correct next step in the migration process 💡
And a breakdown of the PRs for this migration process so far:
N + 1
): BumpSCHEMA_VERSION
to76
. Addfull_user_id
column, start writing to both the old and new column moving forwardN + 2
): BumpSCHEMA_VERSION
to77
andSCHEMA_COMPAT_VERSION
to76
because we added aNOT VALID
constraint that only ensures that new inserts/updates always fill infull_user_id
which isn't backwards compatible because older Synapse versions don't write to the newfull_user_id
column. Also add a background update to backfillfull_user_id
for all rowsN + 3
): BumpSCHEMA_VERSION
to78
, add a foreground update to finish off the backfill and turn theNOT VALID
constraint into a valid one ->full_user_id NOT NULL
, and start reading fromfull_user_id
N + 4
andN + 5
where we stop writing touser_id
and drop the columnThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️ Looks good based on the notes linked above but I've never done this kind of thing before and I'm definitely not an authoritative source of knowledge on this kind of thing.