# Migrations, Diff and Push — Supabase

Source: https://www.skillbyai.com/en/supabase/w-migrations

> Move schema changes safely between environments.

## Schema changes as files

Create a migration with `supabase migration new <name>` and write SQL by hand, or make changes in local Studio and capture them with `supabase db diff -f <name>`. Review the generated SQL before committing. `supabase link --project-ref <ref>` connects the folder to a hosted project, and `supabase db push` applies pending migrations to it. `supabase db pull` captures an existing remote schema. Run pushes from CI rather than laptops once a team is involved.

## From local change to production

A typical sequence.

```bash
supabase migration new add_tasks_table   # writes supabase/migrations/<timestamp>_add_tasks_table.sql
# ...edit the SQL, or change things in local Studio and then:
supabase db diff -f add_tasks_table      # generate a migration from local changes
supabase db reset                        # prove migrations apply cleanly from scratch

supabase link --project-ref your-project-ref
supabase db push                         # apply pending migrations to the linked project
```

## Avoid editing production in the dashboard

Changes made directly in the hosted dashboard drift from your migration files; if it happens, pull or diff them back into a migration immediately.

**Quiz:** Which command applies local migrations to a linked hosted project?

- [ ] supabase start
- [x] supabase db push
- [ ] supabase db reset
- [ ] supabase init

*Answer:* supabase db push. push sends pending migrations to the remote database.
