feat(db): add --use-shadow-db flag to test db command#4871
Open
lightstrike wants to merge 1 commit intosupabase:developfrom
Open
feat(db): add --use-shadow-db flag to test db command#4871lightstrike wants to merge 1 commit intosupabase:developfrom
lightstrike wants to merge 1 commit 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 CodeRabbitRelease Notes
WalkthroughThis PR introduces support for running database tests against a temporary shadow database. A new Sequence DiagramsequenceDiagram
participant CLI as Test CLI Command
participant Runner as test.Run()
participant Diff as diff Module
participant Start as start Module
participant Docker as Docker/Container
participant DB as Shadow Database
CLI->>Runner: Run(ctx, args, config, useShadowDb=true, fs)
alt useShadowDb enabled
Runner->>Diff: CreateShadowDatabase()
Diff->>Docker: Create container from image
Docker->>DB: Initialize database
Diff-->>Runner: Shadow DB created
Runner->>Start: WaitForHealthyService()
Start->>DB: Health check
DB-->>Start: Service healthy
Start-->>Runner: Ready
Runner->>Diff: MigrateShadowDatabase()
Diff->>DB: Apply migrations
DB-->>Diff: Migrations applied
Diff-->>Runner: Migrations complete
Runner->>Runner: Override config (host, port, user, password, database)
Runner->>Runner: Run tests against shadow DB
Runner->>Docker: Defer DockerRemove
Docker->>DB: Remove container
DB-->>Docker: Cleaned up
else useShadowDb disabled
Runner->>Runner: Run tests against configured DB
end
Runner-->>CLI: Tests complete
Comment |
Pull Request Test Coverage Report for Build 22164831089Details
💛 - Coveralls |
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.
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).