fix(replica): tolerate per-statement errors in schema migration#39
Merged
Conversation
The persistent_schemas migration uses pg_dump | psql to copy schemas between restores. Until now psql ran with `ON_ERROR_STOP=1` so the first failing statement killed the whole migration. That blocks the replica from coming up whenever some object in the persistent schema references upstream state that has since changed — e.g. dbt views referencing tamanu columns that have been renamed or dropped upstream. Replicas don't control upstream and can't keep dbt strictly in sync. The user's preference is firmly: replica availability over schema completeness. Clients can regenerate broken views afterward. Drop ON_ERROR_STOP, capture psql's stderr to count `^ERROR:` lines, and report the outcome via the callback as either `success` (no errors) or `partial: N statement error(s)`. The script always exits 0 unless something catastrophic happens upstream of psql (network, auth, pg_dump segfault, etc.). On the operator side, route the callback body: `partial` results log a warn, emit a Warning event (`SchemaMigrationPartial`) for visibility, and set `schemaMigrationPhase: "partial"`. The sweep gate accepts `partial` alongside `complete` and `None`, since the migration has run and we no longer depend on the previous restore. Add a unit test on the migration script content asserting the tolerance guarantees.
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.
The persistent_schemas migration uses
pg_dump | psqlto copy schemas between restores. Until now psql ran withON_ERROR_STOP=1, so the first failing statement killed the whole migration. That blocks the replica from coming up whenever some object in the persistent schema references upstream state that has since changed — e.g. dbt views referencing tamanu columns that have been renamed or dropped upstream.Observed in production on palau-prod: an upstream column rename (
notes.note_type→notes.note_type_id) made one dbt view fail to recreate, which aborted the whole migration. The restore stayed stuck in Switching for 10+ hours with ~130 migration Job pods retrying the same failure.Replicas don't control upstream and can't keep dbt strictly in sync. Stated preference: replica availability over schema completeness. Clients can regenerate broken views afterward; what they can't do is wait days for a replica to come back.
Drop
ON_ERROR_STOP, capture psql's stderr to count^ERROR:lines, and report the outcome via the callback as eithersuccess(no errors) orpartial: N statement error(s). The script always exits 0 unless something catastrophic happens upstream of psql (network, auth, pg_dump segfault, etc.).On the operator side, route the callback body:
partialresults log a warn, emit a Warning event (SchemaMigrationPartial) for visibility, and setschemaMigrationPhase: "partial". The sweep gate acceptspartialalongsidecompleteandNone, since the migration has run and we no longer depend on the previous restore.