Skip to content

Commit 460200e

Browse files
committed
teach synapse_port_db about new sequences
This makes sure we that the port script sets up the "application_services_txns" sequence (matrix-org#12209). and the "un_partial_stated_event_stream_sequence" sequence (matrix-org#14545), which fixes errors like the one below from appearing when trying to connect to the postgresql database after running the script: Postgres sequence 'application_services_txn_id_seq' is inconsistent with associated table 'application_services_txns'. This can happen if Synapse has been downgraded and then upgraded again, or due to a bad migration. To fix this error, shut down Synapse (including any and all workers) and run the following SQL: SELECT setval('application_services_txn_id_seq', ( SELECT GREATEST(MAX(txn_id), 0) FROM application_services_txns )); See docs/postgres.md for more information. Signed-off-by: Guacamolie <[email protected]>
1 parent 19796e2 commit 460200e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

synapse/_scripts/synapse_port_db.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ def alter_table(txn: LoggingTransaction) -> None:
769769
await self._setup_state_group_id_seq()
770770
await self._setup_user_id_seq()
771771
await self._setup_events_stream_seqs()
772+
await self._setup_sequence(
773+
"un_partial_stated_event_stream_sequence",
774+
("un_partial_stated_event_stream",),
775+
)
772776
await self._setup_sequence(
773777
"device_inbox_sequence", ("device_inbox", "device_federation_outbox")
774778
)
@@ -779,6 +783,7 @@ def alter_table(txn: LoggingTransaction) -> None:
779783
await self._setup_sequence("receipts_sequence", ("receipts_linearized",))
780784
await self._setup_sequence("presence_stream_sequence", ("presence_stream",))
781785
await self._setup_auth_chain_sequence()
786+
await self._setup_application_services_sequence()
782787

783788
# Step 3. Get tables.
784789
self.progress.set_state("Fetching tables")
@@ -1133,6 +1138,27 @@ def r(txn: LoggingTransaction) -> None:
11331138
r,
11341139
)
11351140

1141+
async def _setup_application_services_sequence(self) -> None:
1142+
curr_tnx_id: Optional[
1143+
int
1144+
] = await self.sqlite_store.db_pool.simple_select_one_onecol(
1145+
table="application_services_txns",
1146+
keyvalues={},
1147+
retcol="COALESCE(max(txn_id), 0)",
1148+
)
1149+
1150+
def r(txn: LoggingTransaction) -> None:
1151+
txn.execute(
1152+
"ALTER SEQUENCE application_services_txn_id_seq RESTART WITH %s",
1153+
(curr_tnx_id + 1,),
1154+
)
1155+
1156+
if curr_tnx_id is not None:
1157+
await self.postgres_store.db_pool.runInteraction(
1158+
"_setup_application_services_sequence",
1159+
r,
1160+
)
1161+
11361162

11371163
##############################################
11381164
# The following is simply UI stuff

0 commit comments

Comments
 (0)