Repository navigation
feat(storage): use pgmigrate to support downing migrations - #5263
Merged
Merged
Conversation
Implemented: - Delegated Storage's embedded PostgreSQL bundle to pgmigrate with the serve context, logger, schema storage, path postgres, and explicit target 5. - Added a service-owned short-lived migration pool whose close error is joined with the migration error, adapter and target guards, and the pgmigrate Go sources to Storage's hermetic fileset. Why: - Target-oriented migration lets Storage move up to 5 or down from a newer cataloged state instead of only running Up(). Decisions: - Registered lib/pq explicitly; injected runner and closer through unexported parameters for white-box tests; filtered the pgmigrate fileset to Go sources; and preserved the PostgreSQL opt-in and Hasura-after-PostgreSQL sequencing. Plan variance: - Added services/storage/migrations/postgres_internal_test.go for unexported adapter behavior under the repository's white-box convention. - Added Darwin-only pkgs.darwin.libresolv to Storage check inputs because vips forces external linking in the Nix sandbox. - User-approved: asserted the deterministic darwin/arm64 JPEG-to-HEIC digest while preserving the existing exact golden elsewhere; the test remains enabled on every platform. Validation: - go test ./services/storage/migrations/... — passed - go test -run TestManipulate ./services/storage/image/... — passed - make -C services/storage check — passed Paper-Trail-Type: implementation Plan-Commit: 0ad861c6ced9134380a64c159968eb822f2e2c66 Plan-Digest: 766e78b574048fc120751fcb9bdc522dd9963198 Plan-Phase: 4 Review-Verdict: ACCEPT
Review issue 003: Review disposition: ADDRESSED — *traits improved: correctness, reversibility, regression coverage; confidence HIGH* Review snapshot: # Review finding 03 — Storage's version-5 down body drops the wrong table **Severity:** blocking **Scope:** adjacent **Scope rationale:** This adoption makes Storage's version-5 down body executable and immutable in the catalog, so its failure to revert version 5 directly prevents the required older-image downgrade. **File:** services/storage/migrations/postgres/000005_add-viruses-table.down.sql **Line:** non-inline ## Finding `000005_add-viruses-table.up.sql` creates `storage.virus`, but the unchanged down body runs `DROP TABLE IF EXISTS viruses;`. A PostgreSQL 18.6 run through Storage's real migration entry point reported a clean 5→4 downgrade while `storage.virus` survived and an unrelated `public.viruses` table was destroyed. Once version 5 is published, a corrected body is rejected because it no longer matches the cataloged bytes, pinning installations to the broken migration until operator-driven lineage recovery. ## Suggested plan - Before merge, change the body to drop the trigger and `storage.virus` explicitly. - Audit Storage versions 1–4 and add a PostgreSQL-backed up/down/up round-trip test for the embedded bundle. === End review snapshot === Resolution: - Schema-qualify the version-5 down body so it drops `storage.virus`, not the unqualified `viruses`. - Add a PostgreSQL-backed 5 -> 1 -> 5 catalog round trip that preserves an unrelated `public.viruses` table. - Provision PostgreSQL 18 in the storage Nix check so the regression suite runs in CI. Why: - The old body could silently leave `storage.virus` in place while dropping an unrelated public table. - Publishing the archived body would pin installations to defective immutable migration bytes. Decisions: - Downgrade to version 1 to execute historical down bodies for versions 4, 3, and 2 and matching up bodies on re-upgrade. - Reuse the pgmigrate test DSN contract and leave the redundant pre-existing trigger drop unchanged. Review-plan variance: - Versions 2-4 are covered functionally; version 1 down remains unreachable because the inferred target cannot be version 0. Traits improved: - correctness, reversibility, regression coverage — confidence HIGH Accepted concerns: - Version 1 down is structurally unreachable with an inferred bundle target and remains unexercised. - The redundant unguarded trigger drop is pre-existing and intentionally unchanged. Supervisor Q/A: - The full storage check reaches a pre-existing local GraphQL generation dependency; targeted build, lint, Nix, and PostgreSQL gates cover this work unit. Validation: - `go build ./services/storage/...` — exit 0: storage builds. - `go vet ./services/storage/migrations/...` — exit 0: no findings. - `golines --dry-run --base-formatter=gofumpt services/storage/migrations/` — exit 0: formatting is clean. - `golangci-lint run ./services/storage/migrations/...` — exit 0: 0 issues. - `nixfmt --check services/storage/project.nix` and `nix-instantiate --parse services/storage/project.nix` — exit 0: Nix formatting and syntax pass. - `nix develop .#storage --command bash /tmp/nhost-address-9885024505754917112/WU-003-postgres-test.sh` — exit 0: PostgreSQL-backed round trip passes. - `make -C services/storage check` — exit 2: pre-existing gqlgenc generation cannot reach localhost:8080; targeted gates pass. Paper-Trail-Type: review-resolution Review-Change-Type: code-changing Review-Work-Unit: PR_pgmigrate_storage/RUN-3564193530983901644/WU-003 Review-Finding: .nhost-code/review/PR_pgmigrate_storage_COMMENT_03.md@c301991a6f32f7c38a77677fae0250e82d81df25 Review-Bytes: 1156 Review-Scope: adjacent Review-Disposition: ADDRESSED Review-Verdict: ACCEPT
Review issue 004: Review disposition: ADDRESSED — *traits improved: reliability, testability; confidence HIGH* Review snapshot: # Review finding 04 — Storage's embedded bundle is not validated by tests **Severity:** warning **Scope:** in-scope **Scope rationale:** The adoption adds strict bundle requirements that `migrate.Up()` did not enforce, but the branch removes the only adopter-side guard for the resulting startup failure mode. **File:** services/storage/migrations/postgres_internal_test.go **Line:** 43 ## Finding Both remaining tests inject a stub `postgresMigrator` and only check that the supplied filesystem is non-nil, so Storage's embedded `postgres/` directory never reaches `loadBundle`. The branch deletes `postgres_test.go` and removes `pgmigrate.ValidateBundle`, leaving no adopter-side contract check. An overlay probe confirmed that package tests remain green with an unpaired `000006_orphan.up.sql`, while `ApplyPostgresMigration` fails at startup because version 6 lacks a down migration. ## Suggested plan - Restore a minimal exported bundle-validation entry point and call it from a black-box Storage test over the embedded bundle. - Verify the test fails for missing and blank down bodies. === End review snapshot === Resolution: - Restore `pgmigrate.ValidateBundle` as a thin wrapper over the startup bundle loader. - Cover valid, missing-down, and blank-down bundles through the public API. - Validate Storage's real `postgres/*.sql` files in an always-on black-box test. Why: - Existing stub tests and the DSN-gated integration test could allow malformed embedded migrations to ship and fail at startup. Decisions: - Export the shared validator instead of exposing Storage internals through a forbidden test-only production re-export. - Delegate directly to `loadBundle` so validation cannot diverge from startup parsing. Review-plan variance: - None. Traits improved: - reliability, testability — confidence HIGH Accepted concerns: - The test-local embed glob and path duplicate production values, so a later production embed-pattern change could escape the guard. Supervisor Q/A: - Full Nix checks remain blocked by the separately tracked mockgen toolchain mismatch and the storage gqlgenc localhost dependency; targeted gates cover both touched packages. Validation: - `go build ./internal/lib/pgmigrate/... ./services/storage/migrations/...` — exit 0. - `go vet ./internal/lib/pgmigrate/... ./services/storage/migrations/...` — exit 0. - `go test -count=1 ./internal/lib/pgmigrate/... ./services/storage/migrations/...` — exit 0. - `golines --dry-run --base-formatter=gofumpt internal/lib/pgmigrate/ services/storage/migrations/` — exit 0. - `golangci-lint run ./internal/lib/pgmigrate/... ./services/storage/migrations/...` — exit 0: 0 issues. - `make -C internal/lib/pgmigrate check` — exit 2: pre-existing Go 1.26 mockgen cannot load the Go 1.27 module. - `make -C services/storage check` — exit 2: pre-existing gqlgenc generation cannot reach localhost:8080. Paper-Trail-Type: review-resolution Review-Change-Type: code-changing Review-Work-Unit: PR_pgmigrate_storage/RUN-3564193530983901644/WU-004 Review-Finding: .nhost-code/review/PR_pgmigrate_storage_COMMENT_04.md@df56a615e091015d7e0e75382ceaaf5f7b0bbdf5 Review-Bytes: 1099 Review-Scope: in-scope Review-Disposition: ADDRESSED Review-Verdict: ACCEPT_WITH_CONCERNS
Review issue 005: Review disposition: ADDRESSED — *traits improved: focus, portability, maintainability; confidence HIGH* Review snapshot: # Review finding 05 — Platform-specific image golden is outside migration scope **Severity:** suggestion **Scope:** out-of-scope **Scope rationale:** The Darwin ARM64 HEIC digest is a pre-existing image-toolchain property, neither caused by PostgreSQL migration adoption nor required to migrate Storage to target version 5. **File:** services/storage/image/image_test.go **Line:** 145 ## Finding `TestManipulate` adds a Darwin ARM64-only expected digest for `jpeg to heic`, but Storage CI runs only on Linux, so this branch is never exercised in CI and can rot after a libvips/libheif change. The image-pipeline golden and related Darwin `libresolv` check input are also independent of the PostgreSQL migration work, making the migration change harder to review and revert. ## Suggested plan - Move the Darwin ARM64 golden and `libresolv` input to a separate change. - Document why only `jpeg to heic` diverges while the other HEIC cases remain architecture-independent. === End review snapshot === Resolution: - Remove the Darwin ARM64-only image digest branch and restore the image test byte-identically to the parent boundary. - Remove the paired Darwin-only `libresolv` check input from the Storage Nix project. Why: - The platform-specific image enablement is independent of PostgreSQL migration adoption and was never exercised by Linux-only Storage CI. Decisions: - Restore parent behavior rather than retaining an alternate platform conditional. - Remove both coupled Darwin artifacts; any desired Darwin enablement must return as a separate change. Review-plan variance: - The requested divergence documentation was not added because no Darwin-specific behavior remains in this branch. Traits improved: - focus, portability, maintainability — confidence HIGH Supervisor Q/A: - Focused HEIC tests pass; the restored parent test exposes the same pre-existing Darwin ARM64 `jpeg_to_heic` digest mismatch. Validation: - `go build ./services/storage/image` and `go vet ./services/storage/image` — exit 0. - `go test ./services/storage/image -run '^TestManipulate$/^(heic|webp_to_heic)$' -count=1` — exit 0. - `golines --dry-run --base-formatter=gofumpt services/storage/image/` — exit 0. - `golangci-lint run ./services/storage/image/...` — exit 0: 0 issues. - `nixfmt --check services/storage/project.nix`, Nix parse, and aarch64-darwin storage derivation evaluation — exit 0. - `go test ./services/storage/image -run '^TestManipulate$' -count=1` — exit 1: pre-existing parent-boundary Darwin ARM64 `jpeg_to_heic` digest mismatch. - `make -C services/storage check` — exit 2: pre-existing gqlgenc generation cannot reach localhost:8080. Paper-Trail-Type: review-resolution Review-Change-Type: code-changing Review-Work-Unit: PR_pgmigrate_storage/RUN-3564193530983901644/WU-005 Review-Finding: .nhost-code/review/PR_pgmigrate_storage_COMMENT_05.md@1ddb1ed4ee73d202ad2abf743f2967f5199352dd Review-Bytes: 978 Review-Scope: out-of-scope Review-Disposition: ADDRESSED Review-Verdict: ACCEPT
Follow up the accepted concern from review finding 004. Resolution: - Move Storage's bundle validation into the white-box migration test package. - Validate the production `postgresMigrations` filesystem and `postgresMigrationPath` directly. - Remove the duplicated test-local embed directive and path constant. Why: - A separate test embed could drift from production and allow a changed production glob to escape validation. Validation: - `go test -count=1 -run '^TestPostgresMigrationBundle$' ./services/storage/migrations` — exit 0. - `go test -count=1 ./services/storage/migrations/...` — exit 0. - `go vet ./services/storage/migrations/...` — exit 0. - `golines --dry-run --base-formatter=gofumpt services/storage/migrations/` — exit 0. - `golangci-lint run ./services/storage/migrations/...` — exit 0: 0 issues. - `make -C services/storage check` — exit 2: pre-existing gqlgenc generation cannot reach localhost:8080. Paper-Trail-Type: review-follow-up Review-Work-Unit: PR_pgmigrate_storage/RUN-3564193530983901644/WU-004 Review-Concern: production-embed-drift Review-Concern-Resolution: ADDRESSED
skoveit
approved these changes
Oct 28, 2026
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.
Goal
Move Storage's PostgreSQL schema to the embedded migration bundle's target version, including upgrades to version 5 and downgrades from newer cataloged states. The previous startup path only ran
Up(), so it could not bring a database back to an older Storage image's schema target. Preserve the PostgreSQL migration opt-in and run Hasura migrations only after PostgreSQL migration succeeds.Type: Refactoring
Changes
golang-migratesetup with the sharedpgmigrate.Migraterunner, passing the embeddedpostgres/bundle andstorageschema. The bundle determines the target; the shared catalog supplies the migration history needed to move a newer database down to that target. Pass the serve context and logger through the existing PostgreSQL-before-Hasura sequence.lib/pqand give the migration adapter its own short-lived SQL pool. Close that pool on success and failure, joining a close error with any migration error so neither failure is lost. Keep the delegated runner and closer injectable through a private helper for adapter tests.storage.virusinstead of an unrelated unqualifiedvirusestable. Add a PostgreSQL-backed 5 → 1 → 5 round trip that checks clean version state, virus-table removal/restoration, and preservation of a separatepublic.virusestable.PGMIGRATE_TEST_DSNserver URL and clean them up afterward;PGMIGRATE_TEST_DATABASE_REQUIREDmakes a missing DSN fatal rather than a skip.Flow
flowchart TD A[Storage startup: PostgreSQL migrations enabled] --> B[Open migration SQL pool] B --> C[pgmigrate: embedded bundle + storage schema + catalog] C --> D[Move database to bundle target] D --> E[Close pool; preserve migration and close errors] E -->|success| F[Apply Hasura migrations] E -->|error| G[Fail startup]Key files
services/storage/cmd/serve.go(+1/-1) — startup context/logger propagation.services/storage/migrations/postgres.go(+52/-24) — shared-runner adapter and pool lifecycle.services/storage/migrations/postgres/000005_add-viruses-table.down.sql(+1/-1) — schema-qualified virus-table removal.services/storage/migrations/postgres_internal_test.go(+553/-0) — bundle, delegation, error, database-isolation, and round-trip checks.services/storage/project.nix(+4/-0) — source closure and required database-test environment.