---
name: supabase-schema-change
description: Make a database schema change in Supabase safely by designing the migration with RLS policies and indexes, applying it on a branch first, regenerating TypeScript types and checking advisors. Use when the user asks to add a table or column, change the schema, write a migration or add RLS policies in Supabase.
---

# Change the Supabase schema safely

## Steps

1. Read the current state with `list_tables`, including the columns of related tables, and `list_migrations`.
2. Write the migration SQL:
   - tables with a primary key, `created_at timestamptz default now()`, and foreign keys with explicit on delete behaviour;
   - `alter table … enable row level security` on every new table in an exposed schema, plus policies for each role and action, usually tied to `auth.uid()`;
   - indexes on foreign keys and on columns used in filters;
   - no destructive changes, such as dropping or renaming a column, without calling them out.
3. Prefer a branch. If the project uses branching, apply there first. Creating a branch has a cost: show it with `get_cost`, and only after the user accepts it run `confirm_cost` and `create_branch`.
4. Show the SQL and the target. After the user's yes, apply it with `apply_migration`, using a snake_case name such as `add_invoices_table`.
5. Check the result with `get_advisors` for security and performance, for example missing RLS or unindexed foreign keys, and fix what it flags with another approved migration.
6. Regenerate types with `generate_typescript_types` and update the app's types file when the repo is open.
7. To ship a branch, use `merge_branch`, after the user approves.
