Safe Supabase Migrations in Claude Code: MCP, Command and Hook

Learn how to install three Claude Code components together: the MCP server reads the hosted project, the command writes and tests the migration locally, and the hook blocks every write to production.

Dec 1, 2026 7 min read
  • Claude Code
  • MCP
  • Command
  • Hook
  • Supabase

In this article

What is the Safe Supabase Migrations Workflow?

This Claude Code workflow combines three components from the library so that Claude can do the slow part of a schema change and cannot do the risky part: the Supabase MCP server lets Claude read your hosted project, the Supabase Migration Assistant command writes the migration and tests it on your local stack, and the Supabase Remote Guard hook denies every tool call that would change the hosted project. The push to production stays with you.

  1. 01Supabase MCP reads the schema
  2. 02Migration Assistant command
  3. 03Local test
  4. 04Remote Guard hook
  5. 05You run db push

The Three Components

  • Supabase (MCP, database/supabase): the official @supabase/mcp-server-supabase package, pinned to version 0.16.1 and started with --read-only and --project-ref, so it is scoped to one project and the database refuses writes.
  • Supabase Migration Assistant (command, database/supabase-migration-assistant): the /supabase-migration-assistant slash command. It plans the change, writes the migration SQL, tests it, generates TypeScript types and prepares a rollback.
  • Supabase Remote Guard (hook, security/supabase-remote-guard): a PreToolUse hook that denies supabase CLI commands and Supabase MCP tools that write to the hosted project, and leaves local work alone.

Installation

Install all three with one command from your project root:

Terminal
npx claude-code-templates@latest --mcp database/supabase --command database/supabase-migration-assistant --hook security/supabase-remote-guard

The CLI asks where to write the hook. Choose project settings so the whole team gets the guard. The hook needs python3 on your PATH: without it the script cannot run and nothing is blocked.

Where are the components installed?

Terminal
your-project/
β”œβ”€β”€ .claude/
β”‚   β”œβ”€β”€ commands/
β”‚   β”‚   └── supabase-migration-assistant.md   # ← Slash command
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   └── supabase-remote-guard.py          # ← Hook script
β”‚   └── settings.json                         # ← Hook entry (or settings.local.json)
β”œβ”€β”€ .mcp.json                                 # ← Supabase MCP server
└── supabase/
    └── migrations/

How They Connect

The MCP server reads. Its tools reach Claude as mcp__supabase__<tool>, for example mcp__supabase__list_tables. With --read-only, SQL runs as a read-only Postgres user, so Claude can look at the real tables, columns and migration history before it writes anything.

The command writes. Before Claude sees the prompt, /supabase-migration-assistant runs two shell lines that collect your existing migration files and the SQL or TypeScript files with uncommitted changes. What you type after the command arrives as $ARGUMENTS. Claude then works through six steps: plan, generate the SQL, test it, apply it, generate types, and write a rollback.

The hook stands between Claude and production. Its matcher is Bash|mcp__.*[Ss]upabase.*, a regular expression that covers every Bash call and every tool of a Supabase MCP server. For each call, the script decides:

  • Bash: it denies supabase db push (unless --local or --dry-run), db reset --linked, migration repair, functions deploy, secrets set, config push and similar commands, however they are launched (npx, sh -c, && chains). It also denies any command that names a hosted database host such as db.<ref>.supabase.co, so psql or pg_dump with a production connection string does not get through.
  • MCP: it is an allowlist. Only list_*, get_*, search_docs, generate_typescript_types and execute_sql with a single read-only statement pass. Every other tool is denied, including apply_migration and tools the server adds later.

When the hook denies a call, Claude Code shows the reason to Claude. The reason tells Claude that local work is allowed and that it should ask you to run the command yourself. Hooks from settings files also run inside subagents, so a delegated task gets the same guard.

The command's fourth step says to apply the migration through the MCP server. With this setup that step is denied twice, by the hook and by --read-only, and Claude hands the push to you instead. That is the point of the workflow.

Step-by-Step Workflow

Step 1: Point the MCP server at your project

The CLI writes placeholders to .mcp.json. Replace <project-ref> with your project ID, and take the token from an environment variable so it never lands in git:

JSON
{
  "mcpServers": {
    "supabase": {
      "command": "npx",
      "args": [
        "-y",
        "@supabase/mcp-server-supabase@0.16.1",
        "--read-only",
        "--project-ref=your-project-ref"
      ],
      "env": {
        "SUPABASE_ACCESS_TOKEN": "${SUPABASE_ACCESS_TOKEN}"
      }
    }
  }
}

Export SUPABASE_ACCESS_TOKEN in your shell with a personal access token from your Supabase account. Claude Code expands ${VAR} in .mcp.json when it starts the server.

Step 2: Start the local stack and approve the server

Terminal
supabase start
claude

> /mcp

The first time, Claude Code asks you to approve the project server from .mcp.json. /mcp shows whether supabase is connected.

Step 3: Ask for the migration

Terminal
> /supabase-migration-assistant --alter add a nullable team_id to invoices with a foreign key to teams and an index

Claude reads the hosted schema through the MCP server, creates a file under supabase/migrations/ and writes the SQL together with a rollback.

Step 4: Test it locally

The guard allows everything that stays on your machine, so Claude can run these itself:

Terminal
supabase db reset                          # rebuild the local database from all migrations
supabase test db                           # run your pgTAP tests
supabase gen types typescript --local      # refresh the TypeScript types
supabase db push --dry-run                 # list what a push would apply, without applying it

Step 5: Watch the guard stop the push

If Claude tries to finish the job with supabase db push, the hook denies the call and Claude gets this reason:

Text
Blocked by supabase-remote-guard: supabase db push writes migrations to the hosted project.
Local work (supabase start, db reset, test db) is allowed. If the person wants this run
against the hosted project, ask them to run it themselves (in Claude Code: `! <command>`).

Claude stops and asks you to run it.

Step 6: Push it yourself

Read the migration file. When you are happy with it, type the command with the ! prefix:

Terminal
> ! supabase db push

A command typed with ! runs directly in your shell without going through Claude, so it is not a tool call and the hook does not apply. The output is added to the conversation, and Claude can confirm the result with a read-only query.

A Practical Use Case

Your app has an invoices table in production with real customer rows, and you need to move it from per-user to per-team ownership.

  1. Ask Claude how many rows invoices has and which policies reference user_id. It answers from the hosted project with list_tables and a read-only select.
  2. Run /supabase-migration-assistant --alter with the change. Claude writes the migration, a backfill and the rollback.
  3. Claude resets the local database, runs the tests and regenerates the types. If the backfill fails on local seed data, it fixes the SQL and repeats.
  4. Claude runs supabase db push --dry-run and shows you the migrations that are pending.
  5. You review the SQL and run ! supabase db push. At no point did Claude hold the ability to change production.

Tips

  • The hook reads command text. It is a guard against mistakes, not a sandbox: a script that connects through DATABASE_URL or a REST call with a secret key is not caught. Keep --read-only on the MCP server, and keep production connection strings out of the project's .env.
  • For a team, install the hook in project settings and commit .claude/settings.json, .claude/hooks/supabase-remote-guard.py and .mcp.json. The token stays in each person's environment.
  • execute_sql passes only with one statement, no comments and no write keywords. If a harmless query is denied, ask Claude to send it as a single plain select.
  • The hook fails closed: a tool call it cannot parse is denied.

Official Documentation

For more information, see the official Claude Code documentation for MCP, skills and custom commands and hooks.

Created by Daniel Ávila

More from the blog

All articles