mirror of
https://github.com/supabase/agent-skills.git
synced 2026-03-27 10:09:26 +08:00
2.5 KiB
2.5 KiB
title, tags
| title | tags |
|---|---|
| Test Migrations with supabase db reset | migrations, testing, supabase-cli, local-development |
Test Migrations with supabase db reset
Always test migrations locally before deploying to production. Use
npx supabase db reset to verify migrations run cleanly from scratch.
Incorrect:
# Deploying directly without testing
npx supabase db push # Migration fails in production!
Correct:
# Test migrations locally first
npx supabase db reset # Runs all migrations from scratch
# Verify success, then deploy
npx supabase db push
Testing Workflow
# Start local Supabase
npx supabase start
# Reset database and run all migrations
npx supabase db reset
# Verify tables and data
npx supabase inspect db table-sizes
What db reset Does
- Drops the local database
- Creates a fresh database
- Runs all migrations in order
- Runs
supabase/seed.sqlif present
Seed Data for Testing
Create supabase/seed.sql for test data:
-- supabase/seed.sql
-- Runs after migrations on db reset
-- Use ON CONFLICT for idempotency
insert into categories (name)
values ('Action'), ('Comedy'), ('Drama')
on conflict (name) do nothing;
-- Test users (only in local development!)
insert into profiles (id, username)
values ('00000000-0000-0000-0000-000000000001', 'testuser')
on conflict (id) do nothing;
Test Specific Migration
# Apply all pending migrations
npx supabase migration up
# Check migration status (requires supabase link for remote)
npx supabase migration list
Repair Failed Migration
If local and remote migration histories diverge, use migration repair to
manually update the remote history table without re-executing migrations:
# Mark a migration as applied (inserts record without running it)
npx supabase migration repair --status applied 20240315120000
# Mark a migration as reverted (removes record from history)
npx supabase migration repair --status reverted 20240315120000
Inspect Database State
# View tables
npx supabase inspect db table-sizes
# View indexes
npx supabase inspect db index-usage
# View cache hit rate
npx supabase inspect db cache-hit
CI/CD Integration
# GitHub Actions example
- name: Test migrations
run: |
npx supabase start
npx supabase db reset
npx supabase test db # Run pgTAP tests