Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add replaces_index parameter to register_background_index_update
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed May 13, 2022
1 parent c9fc2c0 commit 2e6068b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions synapse/storage/background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ def register_background_index_update(
where_clause: Optional[str] = None,
unique: bool = False,
psql_only: bool = False,
replaces_index: Optional[str] = None,
) -> None:
"""Helper for store classes to do a background index addition
Expand All @@ -537,6 +538,8 @@ def register_background_index_update(
unique: true to make a UNIQUE index
psql_only: true to only create this index on psql databases (useful
for virtual sqlite tables)
replaces_index: The name of an index that this index replaces.
The named index will be dropped upon completion of the new index.
"""

def create_index_psql(conn: Connection) -> None:
Expand Down Expand Up @@ -568,6 +571,12 @@ def create_index_psql(conn: Connection) -> None:
}
logger.debug("[SQL] %s", sql)
c.execute(sql)

if replaces_index is not None:
# We drop the old index as the new index has now been created.
sql = f"DROP INDEX IF EXISTS {replaces_index}"
logger.debug("[SQL] %s", sql)
c.execute(sql)
finally:
conn.set_session(autocommit=False) # type: ignore

Expand Down Expand Up @@ -596,6 +605,12 @@ def create_index_sqlite(conn: Connection) -> None:
logger.debug("[SQL] %s", sql)
c.execute(sql)

if replaces_index is not None:
# We drop the old index as the new index has now been created.
sql = f"DROP INDEX IF EXISTS {replaces_index}"
logger.debug("[SQL] %s", sql)
c.execute(sql)

if isinstance(self.db_pool.engine, engines.PostgresEngine):
runner: Optional[Callable[[Connection], None]] = create_index_psql
elif psql_only:
Expand Down

0 comments on commit 2e6068b

Please sign in to comment.