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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop writing to column
user_id
of tables profiles
and `user_filte…
…rs` (#15787)
- Loading branch information
Showing
10 changed files
with
123 additions
and
173 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Stop writing to column `user_id` of tables `profiles` and `user_filters`. |
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
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
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
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
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
50 changes: 50 additions & 0 deletions
50
synapse/storage/schema/main/delta/79/01_drop_user_id_constraint_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,50 @@ | ||
from synapse.storage.database import LoggingTransaction | ||
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine | ||
|
||
|
||
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None: | ||
""" | ||
Update to drop the NOT NULL constraint on column user_id so that we can cease to | ||
write to it without inserts to other columns triggering the constraint | ||
""" | ||
|
||
if isinstance(database_engine, PostgresEngine): | ||
drop_sql = """ | ||
ALTER TABLE profiles ALTER COLUMN user_id DROP NOT NULL | ||
""" | ||
cur.execute(drop_sql) | ||
else: | ||
# irritatingly in SQLite we need to rewrite the table to drop the constraint. | ||
cur.execute("DROP TABLE IF EXISTS temp_profiles") | ||
|
||
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, full_user_id FROM profiles | ||
""" | ||
cur.execute(copy_sql) | ||
|
||
drop_sql = """ | ||
DROP TABLE profiles | ||
""" | ||
cur.execute(drop_sql) | ||
|
||
rename_sql = """ | ||
ALTER TABLE temp_profiles RENAME to profiles | ||
""" | ||
cur.execute(rename_sql) |
54 changes: 54 additions & 0 deletions
54
synapse/storage/schema/main/delta/79/02_drop_user_id_constraint_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,54 @@ | ||
from synapse.storage.database import LoggingTransaction | ||
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine | ||
|
||
|
||
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None: | ||
""" | ||
Update to drop the NOT NULL constraint on column user_id so that we can cease to | ||
write to it without inserts to other columns triggering the constraint | ||
""" | ||
if isinstance(database_engine, PostgresEngine): | ||
drop_sql = """ | ||
ALTER TABLE user_filters ALTER COLUMN user_id DROP NOT NULL | ||
""" | ||
cur.execute(drop_sql) | ||
|
||
else: | ||
# irritatingly in SQLite we need to rewrite the table to drop the constraint. | ||
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, | ||
filter_id bigint NOT NULL, | ||
filter_json bytea NOT NULL | ||
) | ||
""" | ||
cur.execute(create_sql) | ||
|
||
index_sql = """ | ||
CREATE UNIQUE INDEX IF NOT EXISTS user_filters_full_user_id_unique ON | ||
temp_user_filters (full_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, full_user_id FROM user_filters | ||
""" | ||
cur.execute(copy_sql) | ||
|
||
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) |
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
Oops, something went wrong.