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.
Change Index on user + filters to Unique
* especially relevant since the current increment code is not atomic
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
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
39 changes: 39 additions & 0 deletions
39
synapse/storage/schema/delta/42/unique_user_filter_index.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,39 @@ | ||
from synapse.storage.engines import PostgresEngine | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def run_create(cur, database_engine, *args, **kwargs): | ||
if isinstance(database_engine, PostgresEngine): | ||
select_clause = ''' | ||
CREATE TABLE user_filters_migration AS | ||
SELECT DISTINCT ON (user_id, filter_id) user_id, filter_id, filter_json | ||
FROM user_filters; | ||
''' | ||
else: | ||
select_clause = ''' | ||
CREATE TABLE user_filters_migration AS | ||
SELECT * FROM user_filters GROUP BY user_id, filter_id; | ||
''' | ||
sql = ''' | ||
BEGIN; | ||
%s | ||
DROP INDEX user_filters_by_user_id_filter_id; | ||
DELETE FROM user_filters; | ||
INSERT INTO user_filters(user_id, filter_id, filter_json) | ||
SELECT * FROM user_filters_migration; | ||
DROP TABLE user_filters_migration; | ||
CREATE UNIQUE INDEX user_filters_by_user_id_filter_id_unique | ||
ON user_filters(user_id, filter_id); | ||
END; | ||
''' % select_clause | ||
if isinstance(database_engine, PostgresEngine): | ||
cur.execute(sql) | ||
else: | ||
cur.executescript(sql) | ||
|
||
|
||
def run_upgrade(cur, database_engine, *args, **kwargs): | ||
pass |