|
13 | 13 | # |
14 | 14 |
|
15 | 15 | import logging |
16 | | -from typing import NewType |
| 16 | +from typing import TYPE_CHECKING, NewType |
17 | 17 |
|
18 | 18 | import attr |
19 | 19 |
|
20 | 20 | from synapse.api.errors import NotFoundError |
21 | 21 | 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 | +) |
23 | 28 | from synapse.storage.engines import PostgresEngine |
24 | 29 | from synapse.types import JsonDict, RoomID |
25 | 30 | from synapse.util import stringutils |
26 | 31 | from synapse.util.json import json_encoder |
27 | 32 |
|
| 33 | +if TYPE_CHECKING: |
| 34 | + from synapse.server import HomeServer |
| 35 | + |
28 | 36 | logger = logging.getLogger(__name__) |
29 | 37 |
|
30 | 38 |
|
@@ -55,6 +63,26 @@ class DelayedEventDetails(EventDetails): |
55 | 63 |
|
56 | 64 |
|
57 | 65 | 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 | + |
58 | 86 | async def get_delayed_events_stream_pos(self) -> int: |
59 | 87 | """ |
60 | 88 | Gets the stream position of the background process to watch for state events |
|
0 commit comments