What if one small mistake in your database update could break your entire app?
Creating migrations in Supabase - Why You Should Know This
Imagine you have a growing app and you need to change your database structure. You try to update tables and columns by hand every time you add a feature or fix a bug.
Each time you do this, you write SQL commands manually and run them on your database.
Doing this manually is slow and risky. You might forget a step or make a typo that breaks your app.
Also, if you have multiple developers, everyone might change the database differently, causing confusion and errors.
Creating migrations means writing small, clear steps that change your database structure in a safe and repeatable way.
These steps can be saved, shared, and run automatically, so everyone's database stays the same and your app works smoothly.
ALTER TABLE users ADD COLUMN age INT;
-- migration file create table users (id serial primary key, name text); alter table users add column age int;
It lets you update your database confidently and keep your app working well as it grows.
A team building a social app adds a new feature to track user birthdays. They create a migration to add a birthday column to the users table. Everyone runs this migration, so all databases match perfectly.
Manual database changes are slow and error-prone.
Migrations automate and organize database updates.
Migrations keep all developers' databases in sync.
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
