Database Migrations Guide
The Tuturuuu platform uses Supabase migrations to manage database schema changes with version control and reproducibility.Migration Workflow
Creating Migrations
Method 1: Generate from Studio Changes
Best for: Visual schema editing, quick table creation-
Make changes in Supabase Studio:
-
Generate migration:
-
Review generated file:
Method 2: Manual SQL Migration
Best for: Complex changes, custom functions, data migrations-
Create blank migration:
-
Edit migration file:
Migration File Structure
Naming Convention
File Template
Common Migration Patterns
Creating Tables
Adding Columns
Modifying Columns
Creating Indexes
Creating Functions
Creating Triggers
Creating Enums
Data Migrations
Safe Data Migration Pattern
Batch Processing Large Migrations
Testing Migrations
Local Testing
Test RLS Policies
Verify Indexes
Migration Checklist
Before committing a migration:- Migration file has descriptive name
- All tables have RLS enabled
- RLS policies are tested
- Indexes added for foreign keys
- Indexes added for frequently queried columns
- Comments added for complex logic
- Data migration is idempotent
- Migration tested with
bun sb:reset
- Type generation updated (
bun sb:typegen
) - Breaking changes documented
Deploying Migrations
⚠️ AGENTS: NEVER RUN THESE COMMANDS
Agents should prepare migrations but NEVER deploy them. Only users should run:Agent Workflow
Agents should:- Create migration file
- Test locally with
bun sb:reset
- Run
bun sb:typegen
- Commit files
- Instruct user to run
bun sb:push
- Run
bun sb:push
- Run
bun sb:linkpush
- Deploy to production
Rollback Strategy
Simple Rollback
Complex Rollback
Common Issues
Issue: Migration Fails on Production
Cause: Local Supabase version differs from production Solution:Issue: Type Generation Fails
Cause: Migration has syntax errors Solution:Issue: RLS Policy Conflicts
Cause: Multiple policies on same table/operation Solution:Best Practices
✅ DO
-
Use descriptive names
-
Add indexes for foreign keys
-
Enable RLS on all tables
-
Add comments for clarity
-
Test migrations locally first
❌ DON’T
-
Don’t skip RLS
-
Don’t use DROP without backup
-
Don’t modify old migrations
-
Don’t deploy without testing