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.
- 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.
- 01Supabase MCP reads the schema
- 02Migration Assistant command
- 03Local test
- 04Remote Guard hook
- 05You run db push
The Three Components
- Supabase (MCP,
database/supabase): the official@supabase/mcp-server-supabasepackage, pinned to version 0.16.1 and started with--read-onlyand--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-assistantslash 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): aPreToolUsehook that deniessupabaseCLI 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:
npx claude-code-templates@latest --mcp database/supabase --command database/supabase-migration-assistant --hook security/supabase-remote-guardThe 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?
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--localor--dry-run),db reset --linked,migration repair,functions deploy,secrets set,config pushand similar commands, however they are launched (npx,sh -c,&&chains). It also denies any command that names a hosted database host such asdb.<ref>.supabase.co, sopsqlorpg_dumpwith a production connection string does not get through. - MCP: it is an allowlist. Only
list_*,get_*,search_docs,generate_typescript_typesandexecute_sqlwith a single read-only statement pass. Every other tool is denied, includingapply_migrationand 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:
{
"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
supabase start
claude
> /mcpThe 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
> /supabase-migration-assistant --alter add a nullable team_id to invoices with a foreign key to teams and an indexClaude 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:
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 itStep 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:
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:
> ! supabase db pushA 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.
- Ask Claude how many rows
invoiceshas and which policies referenceuser_id. It answers from the hosted project withlist_tablesand a read-onlyselect. - Run
/supabase-migration-assistant --alterwith the change. Claude writes the migration, a backfill and the rollback. - 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.
- Claude runs
supabase db push --dry-runand shows you the migrations that are pending. - 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_URLor a REST call with a secret key is not caught. Keep--read-onlyon 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.pyand.mcp.json. The token stays in each person's environment. execute_sqlpasses only with one statement, no comments and no write keywords. If a harmless query is denied, ask Claude to send it as a single plainselect.- 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