← all artifacts
skillv1.0.0tested

Migration Author

migration-author

Write safe, reversible Postgres migrations with correct grant and row-level-security ordering. Use when creating or altering tables, adding policies or indexes, seeding required data, or reviewing a migration before it runs.

Provenance

Derived procedure. The source project remains authoritative for its own doctrine; nothing here governs it.

Source project
Atlas Project
Snapshot
02260d7e
Source paths
  • supabase/migrations/
Harvested
2026-09-10
Evidence standing
known
Predecessor
unknown
Supersedes
nothing
Superseded by
unknown

Compatibility and state

Compatible targets
GenericAtlasHTC Hub
Runtime needs
Postgres; row-level security; PostgREST-style Data API where grants are not implied
Dependencies
  • Postgres with RLS — required
Tests
structural-and-fixtures-passing; scripts/check-migration.mjs executed against pass and fail fixtures (last verified 2026-09-10)
Content hash
pending_export
Consumers
not_searched — No activation survey has been performed across target projects.
Transformation
CREATE-GRANT-RLS-POLICY ordering observed across 73 Atlas migrations plus project convention. Mechanical checks extracted into scripts/check-migration.mjs rather than prose. Harmony Hub excluded: its database posture is unknown at the snapshot read.

Files in the artifact

  • SKILL.md
  • agents/openai.yaml
  • scripts/check-migration.mjs

Agent metadata

display_name: Migration Author
summary: Writes safe Postgres migrations in fixed CREATE - GRANT - RLS - POLICY order, with duplicate-schema detection and stated reversibility.
category: data
artifact_class: skill
requires:
  - Postgres with row-level security
  - A PostgREST-style Data API where grants are not implied
includes_script: scripts/check-migration.mjs
when_to_use:
  - Creating or altering tables, policies, indexes or required seed data
  - Reviewing a migration before it runs
when_not_to_use:
  - Deciding whether the concept warrants a table at all (use Registry Designer)
  - Auditing an existing schema's security posture (use Security Reviewer)

SKILL.md

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

  1. 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.
  2. Classify the change: additive, transformative, or destructive. Destructive requires an explicit recovery path written into the migration comment.
  3. Write the statements in the fixed order.
  4. Constrain at the database. Foreign keys with an intentional on delete, not null where the fact is required, check for enumerations that are not enum types, unique for anything that must not duplicate. A constraint the database does not enforce is a convention that will be violated.
  5. Index deliberately. Every foreign key used in a filter, every column in a frequent predicate. No speculative indexes — each one costs on every write.
  6. Seed only what the first screen genuinely requires, as literal INSERTs, with deterministic ids so re-running is detectable.
  7. 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.
  8. Validate — run the script, then verify against the live schema after applying.

Expand / migrate / contract

Never rename or retype in place while readers exist.

  1. Expand — add the new column or table; write to both.
  2. Migrate — backfill; switch readers; verify parity.
  3. 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."

  1. Duplicate check. rg -n 'priority|urgency|severity' supabase/migrations/ finds job.urgency_level already present, added eleven months ago and unused by the UI.
  2. 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. A priority column would have created two fields answering one question, and the next developer would have populated whichever they found first.
  3. 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 anon caller 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 definer helper.

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.