Migration Author
A migration is an irreversible event against live truth. Determinism is not a style preference here.
Run scripts/check-migration.mjs <file.sql> before proposing any migration. It catches the mechanical failures deterministically so review effort goes to the judgement calls.
Hard rules
- Ordering is fixed: CREATE → GRANT → RLS → POLICY. In the same migration, every time.
- Every new public table gets explicit GRANTs. The Data API grants nothing by default; RLS alone leaves the table unreachable and the error looks like a bug in the app.
- Every public table enables row-level security, and RLS without at least one policy is a locked door with no key.
- Never drop or rename in the same migration that adds the replacement. Expand, migrate, contract — three deployments.
- Never write a destructive statement without a stated recovery path.
- Roles live in their own table, never as a column on profiles or users.
- Seeds required by the first screen are literal INSERTs in the migration, not application code, not a one-off script.
Required ordering, with the reason
-- 1. CREATE
create table public.inspection (
id uuid primary key default gen_random_uuid(),
property_id uuid not null references public.property(id) on delete restrict,
occurred_at timestamptz not null,
recorded_at timestamptz not null default now()
);
-- 2. GRANT — PostgREST needs these; RLS does not imply them
grant select, insert, update, delete on public.inspection to authenticated;
grant all on public.inspection to service_role;
-- grant select on public.inspection to anon; -- only with a matching anon policy
-- 3. RLS
alter table public.inspection enable row level security;
-- 4. POLICY
create policy "authenticated read own org inspections"
on public.inspection for select to authenticated
using (public.has_role(auth.uid(), 'inspector'));Tune the grants to the policies. Drop the anon grant when every policy scopes to auth.uid(). Always include service_role where server code touches the table.
Procedure
- Detect duplicate schema first. Search existing migrations and the live schema for the nouns and the synonyms before creating anything. A second table for an existing concept is the most expensive mistake available here, and the least visible. If one exists, stop and route to
registry-designer. - Classify the change: additive, transformative, or destructive. Destructive requires an explicit recovery path written into the migration comment.
- Write the statements in the fixed order.
- Constrain at the database. Foreign keys with an intentional
on delete,not nullwhere the fact is required,checkfor enumerations that are not enum types,uniquefor anything that must not duplicate. A constraint the database does not enforce is a convention that will be violated. - Index deliberately. Every foreign key used in a filter, every column in a frequent predicate. No speculative indexes — each one costs on every write.
- Seed only what the first screen genuinely requires, as literal INSERTs, with deterministic ids so re-running is detectable.
- State reversibility. Additive changes: name the
drop. Transformative: describe the expand/contract sequence. Destructive: name what is unrecoverable — explicitly, in a comment, before it runs. - Validate — run the script, then verify against the live schema after applying.
Expand / migrate / contract
Never rename or retype in place while readers exist.
- Expand — add the new column or table; write to both.
- Migrate — backfill; switch readers; verify parity.
- Contract — a separate, later migration drops the old.
Each step is independently deployable and independently reversible. Collapsing them into one migration means a rollback loses data written between deploy and revert.
Worked example
Request: "add a priority to jobs."
- Duplicate check.
rg -n 'priority|urgency|severity' supabase/migrations/findsjob.urgency_levelalready present, added eleven months ago and unused by the UI. - Outcome. No new column. The correct change is to surface
urgency_level, or to rename it through expand/migrate/contract if the name is genuinely wrong. Aprioritycolumn would have created two fields answering one question, and the next developer would have populated whichever they found first. - Had it not existed: additive
alter table ... add column urgency_level text not null default 'normal' check (urgency_level in ('low','normal','high')), plus an index only if it is filtered on. No grant changes — grants are per table. Reversal:alter table public.job drop column urgency_level;.
Pressure tests
- Does every new public table have GRANT, RLS and at least one policy?
- Can an
anoncaller reach anything they should not? Read the policies literally, not hopefully. - What happens if this migration runs twice?
- What happens if it fails halfway — is it inside a transaction, and are all its statements transactional?
- Which existing rows violate a constraint being added? Check before, not on deploy.
- If this is reverted an hour after deploy, what data is lost?
- Does any policy call a function that reads the table it protects? That recurses — use a
security definerhelper.
Stop conditions
- A table with the same meaning already exists — stop, route to
registry-designer. - A destructive statement has no stated recovery path.
- RLS is enabled with no policy, or a public table has no grants.
- A role check is being stored on a users or profiles table.
- The migration both adds and drops the same concept.