Repository navigation
Make schema installation idempotent - #433
Open
SajawalSajjad786 wants to merge 1 commit into
Open
SajawalSajjad786 wants to merge 1 commit into
SajawalSajjad786 wants to merge 1 commit into
Conversation
Install re-ran migration scripts against objects that already existed whenever the recorded schema version lagged behind the schema that is actually present, failing with 42701 (column "updatecount" of relation "lock" already exists). The trailing version update matched the loop's expected previous version, so it silently recorded nothing when the stored version was out of sync - the script's DDL was committed without advancing the version, leaving every subsequent startup to run the very same script again. - Record the new version unconditionally. A script only runs when no version greater than or equal to its own is recorded, so this always moves the schema forward, and it collapses a stale multi-row schema table back to a single row. - Make v3, v4, v7, v8, v15 and v18 replayable via IF (NOT) EXISTS. - Cast to text in v20's pre-conversion updates so they stay valid once the columns have become jsonb, where comparing to '' raises 22P02. Adds installer tests that reproduce the reported failure and assert that a replay from every recorded version converges on the same columns and indexes as a fresh install. Verified against PostgreSQL 16 and 10. Fixes hangfire-postgres#425 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Author
|
The verification build hasn't started on this PR — no checks have been queued at all. Could a maintainer approve the workflow run when you get a chance, so the suite runs here as well as locally? Unrelated, but worth noting while you're in that file: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #425.
Problem
PostgreSqlObjectsInstaller.InstallwalksInstall.v3.sql→Install.v23.sql, skipping any script whose version is already recorded in theschematable. Two things make that fragile:1. The migration scripts are not replayable. If the recorded version ever lags behind the objects that are actually present, the installer re-runs a script whose bare DDL collides with what is already there.
Install.v4.sqlis the one in the report:It fails on
lockrather thancounterprecisely becausev8later dropscounter.updatecount, so the firstALTER TABLEofv4succeeds and the second one collides.2. The version bump could silently record nothing. The trailing statement was
where
@PreviousVersioncame from a counter in the installer loop, not from the database. When the stored version was out of sync, that matched zero rows — the script's DDL was committed while the version stayed put, so every subsequent startup ran the very same script again. WithEnableResilientStartup(1.21) each failed attempt now also costs the full backoff chain (~31 s at the defaults) before repeating, so a restart sits in that loop instead of completing startup.Changes
PostgreSqlObjectsInstaller: record the new version unconditionally, replacing the conditionalUPDATEwithA script only ever executes when no version greater than or equal to its own is recorded — both
VersionAlreadyAppliedand theversion-already-appliedguard inside every script enforce that — so recording it unconditionally always moves the schema forward. It also collapses a stale multi-rowschematable back to a single row.previousVersionis no longer needed and has been removed.Scripts made replayable:
v3ON CONFLICT DO NOTHINGon the seed version rowv4ADD COLUMN IF NOT EXISTS(×10)v7ADD COLUMN IF NOT EXISTS acquiredv8DROP COLUMN IF EXISTS updatecountv15CREATE INDEX IF NOT EXISTS(×4)v18CREATE TABLE IF NOT EXISTS aggregatedcounterv20WHERE "invocationdata"::text = ''instead of= ''The
v20change surfaced only after the others: once those columns arejsonb, the pre-conversionUPDATE ... WHERE "invocationdata" = ''raises22P02: invalid input syntax for type json. The cast is a no-op while the column is stilltextand cannot match anything once it isjsonb.The
ALTER COLUMN ... TYPEscripts (v5,v9–v12,v19) are left untouched — they are naturally idempotent, and the new tests confirm a replay converges on the same column types.Tests
Four new facts in
PostgreSqlInstallerFacts:InstallingSchemaTwiceKeepsVersionAndShouldNotThrowAnExceptionInstallingSchemaWithStaleVersionRowShouldNotThrowAnExceptionAndShouldUpdateVersionInstallingSchemaWithoutVersionRowShouldNotThrowAnExceptionAndShouldUpdateVersionInstallingSchemaWithStaleVersionRowShouldProduceTheSameObjectsAsAFreshInstall— walks the recorded version back to every value from 3 to 22 and asserts the installer recovers to 23 each time and that the resulting columns and indexes are identical to a fresh install, so a replay cannot leave a subtly different schema behind.Three of them fail on
masterwith the exact reported error. Full suite: 259/259 on PostgreSQL 16, and the installer facts also pass on PostgreSQL 10, confirming the new syntax stays within the floor the scripts already assume (CREATE INDEX IF NOT EXISTSandDROP INDEX IF EXISTSwere in use before this change).Not covered
The genuinely concurrent case — two processes installing simultaneously — still raises a serialization or collision error on the loser and relies on
EnableResilientStartupto retry. With the version bump fixed, that retry now skips the completed migrations cleanly instead of re-running them.