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

Commit d32cd80

Browse files
committed
unify sql and build indexes in background
1 parent 5cb80d7 commit d32cd80

File tree

5 files changed

+45
-49
lines changed

5 files changed

+45
-49
lines changed

synapse/storage/databases/main/filtering.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,37 @@
1616
from typing import Optional, Tuple, Union, cast
1717

1818
from canonicaljson import encode_canonical_json
19+
from typing_extensions import TYPE_CHECKING
1920

2021
from synapse.api.errors import Codes, StoreError, SynapseError
2122
from synapse.storage._base import SQLBaseStore, db_to_json
22-
from synapse.storage.database import LoggingTransaction
23+
from synapse.storage.database import (
24+
DatabasePool,
25+
LoggingDatabaseConnection,
26+
LoggingTransaction,
27+
)
2328
from synapse.types import JsonDict, UserID
2429
from synapse.util.caches.descriptors import cached
2530

31+
if TYPE_CHECKING:
32+
from synapse.server import HomeServer
33+
2634

2735
class FilteringWorkerStore(SQLBaseStore):
36+
def __init__(
37+
self,
38+
database: DatabasePool,
39+
db_conn: LoggingDatabaseConnection,
40+
hs: "HomeServer",
41+
):
42+
super().__init__(database, db_conn, hs)
43+
self.db_pool.updates.register_background_index_update(
44+
"full_users_filters_unique_idx",
45+
index_name="full_users_unique_idx",
46+
table="user_filters",
47+
columns=["full_user_id, filter_id"],
48+
)
49+
2850
@cached(num_args=2)
2951
async def get_user_filter(
3052
self, user_localpart: str, filter_id: Union[int, str]

synapse/storage/databases/main/profile.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,33 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Optional
14+
from typing import TYPE_CHECKING, Optional
1515

1616
from synapse.api.errors import StoreError
1717
from synapse.storage._base import SQLBaseStore
18+
from synapse.storage.database import DatabasePool, LoggingDatabaseConnection
1819
from synapse.storage.databases.main.roommember import ProfileInfo
1920
from synapse.types import UserID
2021

22+
if TYPE_CHECKING:
23+
from synapse.server import HomeServer
24+
2125

2226
class ProfileWorkerStore(SQLBaseStore):
27+
def __init__(
28+
self,
29+
database: DatabasePool,
30+
db_conn: LoggingDatabaseConnection,
31+
hs: "HomeServer",
32+
):
33+
super().__init__(database, db_conn, hs)
34+
self.db_pool.updates.register_background_index_update(
35+
"profiles_full_user_id_key_idx",
36+
index_name="profiles_full_user_id_key",
37+
table="profiles",
38+
columns=["full_user_id"],
39+
)
40+
2341
async def get_profileinfo(self, user_localpart: str) -> ProfileInfo:
2442
try:
2543
profile = await self.db_pool.simple_select_one(

synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.postgres synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
ALTER TABLE profiles ADD COLUMN full_user_id TEXT;
1717

18-
-- Add a new constraint on the new column, mirroring the `profiles_user_id_key`
18+
-- Make sure the column has a unique constraint, mirroring the `profiles_user_id_key`
1919
-- constraint.
20-
ALTER TABLE ONLY profiles
21-
ADD CONSTRAINT profiles_full_user_id_key UNIQUE (full_user_id);
20+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES (7501, 'profiles_full_user_id_key_idx', '{}');

synapse/storage/schema/main/delta/75/01_add_profiles_full_user_id_column.sql.sqlite

-40
This file was deleted.

synapse/storage/schema/main/delta/75/02_add_user_filters_full_user_id_column.sql

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@ ALTER TABLE user_filters ADD COLUMN full_user_id TEXT;
1717

1818
-- Add a unique index on the new column, mirroring the `user_filters_unique` unique
1919
-- index.
20-
CREATE UNIQUE INDEX full_user_filters_unique ON user_filters (full_user_id, filter_id);
21-
-- NB: This will lock the table for writes while the index is being built.
22-
-- There are around 4,000,000 user_filters on matrix.org so we expect this to take
23-
-- a couple of seconds at most.
20+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES (7502, 'full_users_filters_unique_idx', '{}');

0 commit comments

Comments
 (0)