feat(db): add --use-shadow-db flag to test db command#4871
feat(db): add --use-shadow-db flag to test db command#4871lightstrike wants to merge 3 commits intosupabase:developfrom
Conversation
Runs pgTAP tests against an ephemeral shadow database built from migrations, keeping the local dev database untouched. Reuses the existing CreateShadowDatabase/MigrateShadowDatabase machinery from db diff. Uses host networking so pg_prove can reach the shadow container via 127.0.0.1:<shadow_port>.
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughSummary by CodeRabbit
WalkthroughAdds a new --use-shadow-db flag to the test CLI and updates internal/db/test.Run to accept a boolean useShadowDb. When true, Run provisions a temporary shadow Postgres container, waits for it to become healthy, applies migrations, overrides the DB connection to point at the shadow instance, runs tests against it, and removes the container afterwards. Call sites were updated to pass the new parameter (existing tests pass false). Sequence Diagram(s)sequenceDiagram
participant CLI as Test CLI
participant Runner as internal/db/test.Run
participant Diff as internal/db/diff
participant Start as internal/db/start
participant Docker as Docker Engine
participant DB as Shadow Postgres
CLI->>Runner: Run(ctx, args, config, useShadowDb=true, fs)
alt useShadowDb == true
Runner->>Diff: CreateShadowDatabase()
Diff->>Docker: Launch container (shadow_port)
Docker->>DB: Start DB instance
Diff-->>Runner: Return shadow connection info
Runner->>Start: WaitForHealthyService(shadow host, port)
Start->>DB: Health probe
DB-->>Start: Healthy
Start-->>Runner: Ready
Runner->>Diff: MigrateShadowDatabase(shadow conn)
Diff->>DB: Apply migrations
DB-->>Diff: Migrations applied
Runner->>Runner: Override config -> shadow conn
Runner->>DB: Run tests (pgTAP / queries)
DB-->>Runner: Test results
Runner->>Docker: Defer DockerRemove(container)
Docker->>DB: Stop & remove container
else useShadowDb == false
Runner->>Runner: Use provided config / run tests against configured DB
end
Runner-->>CLI: Return test results
Comment |
Pull Request Test Coverage Report for Build 22208754227Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
Document the --use-shadow-db flag in the CLI man page with usage details, shadow port config, and CI guidance. Add a shadow-db example to examples.yaml and fix the stale shadow_port comment in testdata/config.toml to match the production template.
Running go generate picks up the upstream OpenAPI spec change that added ReplicationConnected to V1ServiceHealthResponseInfo1. Fixes the codegen CI check.
Runs pgTAP tests against an ephemeral shadow database built from migrations, keeping the local dev database untouched. Reuses the existing CreateShadowDatabase/MigrateShadowDatabase machinery from db diff. Uses host networking so pg_prove can reach the shadow container via 127.0.0.1:<shadow_port>.
What kind of change does this PR introduce?
Feature — adds a new
--use-shadow-dbflag to bothsupabase db testandsupabase test db.What is the current behavior?
supabase db test(andsupabase test db) always runs pgTAP tests against the local development database. This means:What is the new behavior?
When
--use-shadow-dbis passed, the CLI:CreateShadowDatabase(same machinery used bydb diff).MigrateShadowDatabase.127.0.0.1:<shadow_port>).defer DockerRemove).The local dev database is never touched.
Files changed (5):
cmd/db.go— flag wiring ondbTestCmdcmd/test.go— mirror flag ontestDbCmdinternal/db/test/test.go— shadow DB lifecycle + host networkinginternal/db/test/test_test.go— updated call sites withfalseparampkg/config/templates/config.toml— updatedshadow_portdoc commentAdditional context
Prior art: ephemeral test databases in other frameworks
Using a dedicated, disposable database for test runs is a well-established pattern across mature frameworks:
test_<dbname>database, applies all migrations, runs tests inside transactions for per-test isolation, and destroys the DB afterward (docs).testenvironment with its own database defined indatabase.yml. The schema is loaded fromdb/schema.rb(orstructure.sql) before each test run, and each test is wrapped in a transaction that rolls back (guide).:memory:databases or a dedicated MySQL/Postgres test DB. TheRefreshDatabasetrait migrates or transaction-wraps each test automatically (docs).prisma migrate devto detect schema drift and validate migrations. For integration tests, Prisma recommends Docker-based ephemeral databases (docs).The
--use-shadow-dbflag brings the Supabase CLI in line with this industry-standard practice: tests run against a clean, ephemeral database built from migrations, leaving development data untouched.Why a flag (not the default)?
The flag is opt-in to ensure safe backwards compatibility — existing workflows that expect tests to run against the local dev database continue to work unchanged. Once the shadow DB path has been validated in real-world usage and deemed stable, we recommend making it the default behavior (with an opt-out flag if needed).