Database Migrations
Guide to managing database migrations in Lager Guru.
Overview
Migrations are used to manage database schema changes in a version-controlled way.
Migration Files
Migrations are located in supabase/migrations/ directory.
Creating Migrations
Using Supabase CLI
bash
supabase migration new migration_nameManual Creation
Create a new file in supabase/migrations/ with timestamp prefix:
YYYYMMDDHHMMSS_migration_name.sqlMigration Structure
sql
-- Migration: Add new column to shipments
-- Created: 2025-12-02
ALTER TABLE shipments
ADD COLUMN new_field TEXT;
CREATE INDEX idx_shipments_new_field ON shipments(new_field);Running Migrations
Local Development
bash
supabase db resetProduction
Migrations are applied automatically via CI/CD or manually:
bash
supabase db pushBest Practices
- One logical change per migration
- Include rollback instructions
- Test migrations locally first
- Document breaking changes
- Use transactions when possible
Rollback
Manual Rollback
Create a new migration to reverse changes:
sql
-- Rollback: Remove column from shipments
ALTER TABLE shipments
DROP COLUMN new_field;Migration Checklist
- [ ] Test migration locally
- [ ] Verify RLS policies
- [ ] Check indexes
- [ ] Test rollback
- [ ] Document changes
- [ ] Update schema documentation