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
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughSummary by CodeRabbitRelease Notes
WalkthroughThis PR introduces shadow database support to the test command. A new Sequence Diagram(s)sequenceDiagram
participant User
participant CLI as CLI Handler
participant Runner as Test Runner
participant Diff as Shadow DB Setup
participant Health as Health Check
participant Docker as Docker Container
participant TestEngine as Test Engine
User->>CLI: test db --use-shadow-db
CLI->>Runner: Run(ctx, testFiles, config, useShadowDb=true, fs)
rect rgba(100, 150, 200, 0.5)
Note over Runner,Docker: Shadow Database Initialization
Runner->>Diff: CreateShadowDatabase()
Diff->>Docker: Create container
Docker-->>Diff: Container created
Diff-->>Runner: Shadow DB ready
Runner->>Health: WaitForHealthyService()
Health->>Docker: Check health status
Docker-->>Health: Healthy
Health-->>Runner: Service ready
Runner->>Diff: MigrateShadowDatabase()
Diff->>Docker: Apply migrations
Docker-->>Diff: Migrations complete
Diff-->>Runner: Migrations applied
end
rect rgba(200, 150, 100, 0.5)
Note over Runner,TestEngine: Test Execution
Runner->>TestEngine: Run tests against shadow DB
TestEngine-->>Runner: Tests complete
end
rect rgba(150, 100, 200, 0.5)
Note over Runner,Docker: Cleanup
Runner->>Docker: DockerRemove()
Docker-->>Runner: Container removed
Runner-->>User: Results
end
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 |
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>.
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.
Add three tests exercising the useShadowDb=true code path in Run(): happy path with full shadow DB lifecycle (create, health check, migrate, pg_prove), error on shadow creation failure, and error on shadow migration failure. Raises test.go coverage from 15% to 85%.
d87792f to
335fd98
Compare
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).