Database Migration
Overview
Structured workflow for safe database schema changes with mandatory RLS policies and rollback planning.
When to Use
- •Creating new tables
- •Adding/modifying columns
- •Writing or updating RLS policies
- •Creating database functions or triggers
- •Seeding reference data
Workflow
Phase 1: Design
- •Define schema (columns, types, constraints)
- •Plan relationships (foreign keys)
- •Plan RLS policies (who can CRUD)
- •Identify indexes needed
- •Plan seed data if reference table
Phase 2: Write Migration
bash
supabase migration new <descriptive_name>
Template:
sql
create table if not exists public.<table_name> ( id uuid primary key default gen_random_uuid(), user_id uuid references auth.users(id) on delete cascade not null, created_at timestamptz default now() not null, updated_at timestamptz default now() not null ); alter table public.<table_name> enable row level security; create policy "<table_name>_select_own" on public.<table_name> for select to authenticated using (user_id = auth.uid()); create policy "<table_name>_insert_own" on public.<table_name> for insert to authenticated with check (user_id = auth.uid()); create policy "<table_name>_update_own" on public.<table_name> for update to authenticated using (user_id = auth.uid()) with check (user_id = auth.uid()); create policy "<table_name>_delete_own" on public.<table_name> for delete to authenticated using (user_id = auth.uid()); create index idx_<table_name>_user_id on public.<table_name>(user_id);
SQL style: lowercase keywords, snake_case names, text over varchar, always include id, created_at, updated_at.
Phase 3: Apply & Verify
bash
supabase db push
Verify:
- •Table exists
- •RLS enabled
- •All 4 CRUD policies exist
- •INSERT as auth user works
- •SELECT returns only own rows
Checklist
- • Migration file with timestamp prefix
- • Table has
id,created_at,updated_at - • User-owned tables have
user_idFK - • RLS enabled
- • SELECT/INSERT/UPDATE/DELETE policies with
auth.uid() - • Indexes on
user_idand query columns - • Applied and verified
References
- •
.claude/supabase/references/MIGRATIONS.md - •
.claude/supabase/references/RLS-POLICIES.md - •
.claude/supabase/references/SCHEMA.md - •
.claude/supabase/references/SQL-STYLE.md