Migrations help you save and track changes to your database structure. They make it easy to update your database safely and share changes with your team.
Creating migrations in Supabase
supabase migration new <migration_name> supabase db reset supabase migration apply
supabase migration new creates a new migration file with your changes.
supabase db reset resets the local database and applies migrations.
supabase migration apply applies migrations to the remote database.
supabase migration new add_users_table
supabase db reset
supabase migration apply
This migration creates a new table called users with three columns: id, name, and email. The commands create the migration file, reset and apply it locally, and then apply it to the remote database.
-- Migration file: 20240601_add_users_table.sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
-- Commands to run:
supabase migration new add_users_table
supabase db reset
supabase migration applyAlways write clear and descriptive migration names.
Test migrations locally before applying them to production.
Keep migrations small and focused on one change at a time.
Migrations save and track database changes.
Use supabase migration new to create migration files.
Reset and apply migrations locally with supabase db reset and apply migrations remotely with supabase migration apply.
Practice
Solution
Step 1: Understand what migrations do
Migrations are files that record changes to the database schema over time.Step 2: Identify the purpose in Supabase context
Supabase uses migrations to save and track database structure changes so they can be applied consistently.Final Answer:
To save and track changes made to the database structure -> Option AQuick Check:
Migrations = track database changes [OK]
- Confusing migrations with user management
- Thinking migrations deploy frontend code
- Assuming migrations monitor server health
Solution
Step 1: Recall Supabase migration commands
The command to create a new migration file is specifically designed to generate the file for changes.Step 2: Match command to action
supabase migration newcreates a new migration file, while others apply or push changes.Final Answer:
supabase migration new -> Option CQuick Check:
Create migration file = supabase migration new [OK]
- Using 'db push' to create migration files
- Confusing 'migration up' with creation
- Typing incorrect commands like 'start migration'
Solution
Step 1: Understand commands for applying migrations
supabase db pushapplies schema changes to the remote database by pushing local migrations.Step 2: Differentiate commands
supabase migration upapplies migrations locally, whilesupabase db pushpushes changes remotely.Final Answer:
supabase db push -> Option BQuick Check:
Apply remotely = db push [OK]
- Using 'supabase migration up' to apply migrations remotely
- Confusing 'migration new' as apply command
- Thinking 'db pull' applies migrations
supabase migration up but got an error saying migrations are missing. What is the likely cause?Solution
Step 1: Analyze the error about missing migrations
The error indicates no migration files exist to apply locally.Step 2: Identify the cause
This usually happens if migration files were never created withsupabase migration new.Final Answer:
You forgot to create migration files before applying -> Option DQuick Check:
Missing migrations = no files created [OK]
- Assuming connection issues cause missing migrations
- Restarting server without creating files
- Confusing local and remote apply commands
Solution
Step 1: Create migration files first
Usesupabase migration newto save schema changes as migration files.Step 2: Apply changes locally
Runsupabase startto update your local database with these changes.Step 3: Apply migrations remotely
Finally, runsupabase migration upto update the remote database.Final Answer:
supabase migration new -> supabase start -> supabase migration up -> Option AQuick Check:
Create -> local start -> remote up [OK]
- Applying migrations before creating files
- Applying remotely before local test
- Mixing order of commands
Ready to prove it?
Pass the test and this pattern moves unknown
