Skip to content

Commit

Permalink
teach synapse_port_db about new sequences
Browse files Browse the repository at this point in the history
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 <guac@amolie.nl>
  • Loading branch information
vanguacamolie committed Jul 20, 2023
1 parent 19796e2 commit 2109233
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/15965.bugfix
@@ -0,0 +1 @@
Fix `synapse_port_db` not configuring the `application_services_txns` and `un_partial_stated_event_stream_sequence` sequences. Contributed by @vanguacamolie.
26 changes: 26 additions & 0 deletions synapse/_scripts/synapse_port_db.py
Expand Up @@ -769,6 +769,10 @@ def alter_table(txn: LoggingTransaction) -> None:
await self._setup_state_group_id_seq()
await self._setup_user_id_seq()
await self._setup_events_stream_seqs()
await self._setup_sequence(
"un_partial_stated_event_stream_sequence",
("un_partial_stated_event_stream",),
)
await self._setup_sequence(
"device_inbox_sequence", ("device_inbox", "device_federation_outbox")
)
Expand All @@ -779,6 +783,7 @@ def alter_table(txn: LoggingTransaction) -> None:
await self._setup_sequence("receipts_sequence", ("receipts_linearized",))
await self._setup_sequence("presence_stream_sequence", ("presence_stream",))
await self._setup_auth_chain_sequence()
await self._setup_application_services_sequence()

# Step 3. Get tables.
self.progress.set_state("Fetching tables")
Expand Down Expand Up @@ -1133,6 +1138,27 @@ def r(txn: LoggingTransaction) -> None:
r,
)

async def _setup_application_services_sequence(self) -> None:
curr_tnx_id: Optional[
int
] = await self.sqlite_store.db_pool.simple_select_one_onecol(
table="application_services_txns",
keyvalues={},
retcol="COALESCE(max(txn_id), 0)",
)

def r(txn: LoggingTransaction) -> None:
txn.execute(
"ALTER SEQUENCE application_services_txn_id_seq RESTART WITH %s",
(curr_tnx_id + 1,),
)

if curr_tnx_id is not None:
await self.postgres_store.db_pool.runInteraction(
"_setup_application_services_sequence",
r,
)


##############################################
# The following is simply UI stuff
Expand Down

0 comments on commit 2109233

Please sign in to comment.