Creating migrations in Supabase - Performance & Efficiency
When we create migrations in Supabase, we want to know how the time it takes grows as we add more changes.
We ask: How does the work increase when we add more migration steps?
Analyze the time complexity of the following migration creation process.
// Create a new migration
const { data, error } = await supabase.rpc('create_migration', {
migration_name: 'add_users_table',
sql_commands: 'CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);'
});
// Apply migration
const { data: applyData, error: applyError } = await supabase.rpc('apply_migration', {
migration_id: data.id
});
This sequence creates a migration record and applies it to the database.
Look at what repeats when creating multiple migrations.
- Primary operation: Calling the remote procedure to create and apply each migration.
- How many times: Once per migration step added.
Each migration requires one create call and one apply call.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 20 calls (10 create + 10 apply) |
| 100 | 200 calls (100 create + 100 apply) |
| 1000 | 2000 calls (1000 create + 1000 apply) |
Pattern observation: The number of calls grows directly with the number of migrations.
Time Complexity: O(n)
This means the time to create and apply migrations grows in a straight line as you add more migrations.
[X] Wrong: "Creating multiple migrations happens all at once, so time stays the same no matter how many migrations."
[OK] Correct: Each migration requires separate calls and processing, so time adds up with each one.
Understanding how migration steps add up helps you plan database changes efficiently and shows you can think about scaling processes clearly.
"What if we batch multiple migration commands into one call? How would the time complexity change?"
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
