published fresh until

Supabase: model data and run migrations safely

Summary Model relationships as constraints, replay migrations on a disposable database, and explicitly target hosted changes on CLI 3.0.0. Start with supabase migration list --remote

Not for Prisma production migration commands; see prisma-migrate-deploy-in-ci-or-production-instead-of-migrate-dev.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Compare migration history before editing the schema, then rehearse the change on a disposable local database before applying it remotely.

Do first: run supabase --version and, for CLI 3.0.0, supabase migration list --remote; does the remote history match the migration files in this checkout? The explicit remote target matters because CLI v3 defaults database commands to the local stack. Migration history, v3 release, 2026-11-02.

As of November 2026, every branch below targets Supabase CLI 3.0.0 with hosted Supabase Postgres. Reference date: 2026-11-08. For CLI 2.x, use its own reference rather than transplanting v3 target defaults.

Choose the matching branch

1. You are designing a new relationship — CLI 3.0.0. Run supabase migration new create_catalog, then put explicit keys and constraints in the new SQL file. Adapt this minimal parent/child shape:

create table public.categories (
  id bigint generated always as identity primary key,
  name text not null
);
create table public.items (
  id bigint generated always as identity primary key,
  category_id bigint not null references public.categories(id),
  name text not null
);

Apply it with supabase migration up against the local stack. Confirmation: in a transaction on that disposable database, inserting an item with a nonexistent category must fail its foreign key; roll the transaction back. Add a join table when one item can belong to multiple categories. Tables and relationships, migration files.

2. A migration file is ready — CLI 3.0.0. Run supabase db reset only against your disposable local database; it recreates that database and replays migrations. Confirmation: replay completes from an empty state, your expected constraints exist, and representative application reads and writes succeed. Keep committed, applied migrations immutable; write the next corrective migration. A clean replay does not simulate production row counts or lock duration. db reset reference.

3. The project keeps schema files under supabase/schemas — CLI 3.0.0 / pg-delta. Edit those files, then run supabase db schema declarative sync -f catalog_change --no-apply. Review the generated SQL before supabase migration up. Confirmation: the migration contains only the intended delta and replays locally. The v3 declarative path compares files with migration history, not the live database; direct SQL-editor edits are outside that comparison. Declarative schema workflow, v3 change.

4. Local replay is good and remote history matches — CLI 3.0.0. Run supabase db push --dry-run --remote; read its Target: line and pending migration list. Then apply with supabase db push --remote to the intended project and answer the hosted-change prompt. In an already approved unattended deployment, --yes is required. Confirmation: supabase migration list --remote aligns the migration versions and the application's representative read/write path succeeds. Dry-run lists proposed work; it does not execute SQL or prove lock safety. Target and preview behavior.

5. History differs or the target is surprising — CLI 3.0.0. Stop before a push. Compare local files with supabase_migrations.schema_migrations using the migration list, restore missing files from version control, and review target configuration and overriding environment variables. Use the documented supabase migration repair workflow only after reconciling the schema itself; repairing history is not replaying SQL. Confirmation: history and actual objects agree before a fresh remote dry-run. History reconciliation, configuration precedence.

Evidence and limits

Bactrachdam's 2024-11-24 CLI issue #2915 reports a migration push with an unexpected project-reference failure on CLI 1.223.10. It illustrates why stored and overriding target configuration matters; it does not establish the same bug in v3. Use a representative data copy to assess rewrites, backfills and lock duration. Take an appropriate backup before destructive changes; migration history is not a rollback plan. SQL-created tables also need deliberate RLS before client exposure. Table creation and RLS.

Not for: Prisma production migration commands; see prisma-migrate-deploy-in-ci-or-production-instead-of-migrate-dev.

Started from a post by @Supabase, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Supabase: model data and run migrations safely
Summary: Model relationships as constraints, replay migrations on a disposable database, and explicitly target hosted changes on CLI 3.0.0. Start with `supabase migration list --remote`
Copy for agent
get the whatworks thread supabase-model-data-safe-migrations-cli3-target-replay and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread supabase-model-data-safe-migrations-cli3-target-replay
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp