Database Migrations Guide
Migrations are version-controlled changes to your database schema. This guide walks through creating, testing, and deploying migrations in the FindU ecosystem.Migration Workflow Overview
Creating Your First Migration
1
Create a feature branch
2
Create migration file
3
Write your migration
Edit the generated file with your SQL:
4
Test your migration
- Go to Supabase dashboard
- Navigate to SQL Editor
- Paste your migration SQL
- Run in a transaction:
5
Commit and push
6
Create pull request
Types of Migrations
Schema Migrations
- Creating Tables
- Altering Tables
- Creating Indexes
Data Migrations
Creating Functions
Testing Migrations
Local Testing Checklist
Writing Rollback Migrations
For risky changes, prepare a rollback migration:Deployment Process
To Development
- Automatic: Merging PR to
devbranch triggers deployment - Manual: If needed, run
supabase db pushafter switching to dev
To Production
- Create PR: From
devtomainbranch - Review Required: Another developer must approve
- Automatic Deploy: Merging triggers production deployment
- Monitor: Check Supabase dashboard for any issues
Best Practices
Always Include RLS
Every table should have Row Level Security enabled and appropriate policies
Make Migrations Idempotent
Use
IF NOT EXISTS, IF EXISTS, and proper checksOne Concern Per Migration
Keep migrations focused on a single feature or change
Test Thoroughly
Always run
supabase db reset before pushingCommon Patterns
Adding Audit Fields
Setting Up RLS
Performance Indexes
Troubleshooting
Migration fails with 'already exists' error
Migration fails with 'already exists' error
Your migration isn’t idempotent. Add
IF NOT EXISTS checks:RLS policies blocking access
RLS policies blocking access
Test policies in Supabase dashboard:
- Go to Authentication > Policies
- Use the policy editor to test with different users
- Check policy conditions carefully
Migration works locally but fails in CI
Migration works locally but fails in CI
Common causes:
- Missing
IF EXISTSchecks - Dependency on data that doesn’t exist
- Different Postgres versions
- Timezone issues with timestamps
Need to modify a migration
Need to modify a migration
Never modify a migration that’s been deployed. Instead:
- Create a new migration to fix the issue
- Include proper rollback handling
- Test the upgrade path thoroughly
Next, learn about schema management or data operations.