Skip to content

Commit fa20174

Browse files
committed
Add unique constraint in background
This satisfies the check-schema-delta script.
1 parent 87752c9 commit fa20174

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

synapse/storage/databases/main/delayed_events.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,26 @@
1313
#
1414

1515
import logging
16-
from typing import NewType
16+
from typing import TYPE_CHECKING, NewType
1717

1818
import attr
1919

2020
from synapse.api.errors import NotFoundError
2121
from synapse.storage._base import SQLBaseStore, db_to_json
22-
from synapse.storage.database import LoggingTransaction, StoreError
22+
from synapse.storage.database import (
23+
DatabasePool,
24+
LoggingDatabaseConnection,
25+
LoggingTransaction,
26+
StoreError,
27+
)
2328
from synapse.storage.engines import PostgresEngine
2429
from synapse.types import JsonDict, RoomID
2530
from synapse.util import stringutils
2631
from synapse.util.json import json_encoder
2732

33+
if TYPE_CHECKING:
34+
from synapse.server import HomeServer
35+
2836
logger = logging.getLogger(__name__)
2937

3038

@@ -55,6 +63,26 @@ class DelayedEventDetails(EventDetails):
5563

5664

5765
class DelayedEventsStore(SQLBaseStore):
66+
def __init__(
67+
self,
68+
database: DatabasePool,
69+
db_conn: LoggingDatabaseConnection,
70+
hs: "HomeServer",
71+
):
72+
super().__init__(database, db_conn, hs)
73+
74+
# In practice, delay_ids are already unique because they are generated
75+
# from cryptographically strong random strings.
76+
# Therefore, adding this constraint is not expected to ever fail,
77+
# despite the current pkey technically allowing non-unique delay_ids.
78+
self.db_pool.updates.register_background_index_update(
79+
update_name="delayed_events_idx",
80+
index_name="delayed_events_idx",
81+
table="delayed_events",
82+
columns=("delay_id",),
83+
unique=True,
84+
)
85+
5886
async def get_delayed_events_stream_pos(self) -> int:
5987
"""
6088
Gets the stream position of the background process to watch for state events

synapse/storage/schema/main/delta/93/01_add_delayed_events.sql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,5 @@
1313

1414
-- Set delayed events to be uniquely identifiable by their delay_id.
1515

16-
-- In practice, delay_ids are already unique because they are generated
17-
-- from cryptographically strong random strings.
18-
-- Therefore, adding this constraint is not expected to ever fail,
19-
-- despite the current pkey technically allowing non-unique delay_ids.
20-
CREATE UNIQUE INDEX delayed_events_idx ON delayed_events (delay_id);
16+
INSERT INTO background_updates (update_name, progress_json) VALUES
17+
('delayed_events_idx', '{}');

0 commit comments

Comments
 (0)