Running migrations locally and remotely in Supabase - Time & Space Complexity
When running database migrations with Supabase, it is important to understand how the time taken grows as the number of migrations increases.
We want to know how the process scales when applying migrations locally and remotely.
Analyze the time complexity of the following operation sequence.
// Run migrations locally
supabase db reset
// Run migrations remotely
supabase db push
This sequence applies all pending migrations first locally, then pushes them to the remote Supabase database.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Applying each migration script one by one to the database.
- How many times: Once per migration file in the migration folder.
As the number of migrations increases, the total time grows roughly in direct proportion.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 migration applies + 1 reset + 1 push |
| 100 | 100 migration applies + 1 reset + 1 push |
| 1000 | 1000 migration applies + 1 reset + 1 push |
Pattern observation: The number of migration applies grows linearly with the number of migrations, while reset and push happen once.
Time Complexity: O(n)
This means the time to run migrations grows directly with the number of migration files.
[X] Wrong: "Running migrations remotely takes the same time regardless of how many migrations there are."
[OK] Correct: Each migration must be applied in order, so more migrations mean more work and longer time.
Understanding how migration time grows helps you plan deployment steps and communicate realistic timelines in real projects.
"What if migrations were batched and applied all at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand migration purpose locally
Running migrations locally allows you to check if changes work without risk to live data.Step 2: Understand migration purpose remotely
Running migrations remotely updates the live database after testing is successful.Final Answer:
To test changes safely before applying them to the live database -> Option BQuick Check:
Test locally first = Safe migration [OK]
- Running migrations directly on live without testing
- Assuming local migrations update remote automatically
- Thinking migrations delete all data by default
Solution
Step 1: Identify the correct Supabase CLI command
The commandsupabase db resetapplies all migrations to the local database by resetting it.Step 2: Check other options
supabase migration runandsupabase db migrateare invalid;supabase db pushupdates the remote database.Final Answer:
supabase db reset -> Option AQuick Check:
Local migration command = supabase db reset [OK]
- Using remote commands like 'supabase db push' for local
- Using incorrect commands like 'supabase migration run'
- Trying to run migrations without Supabase CLI installed
supabase db push supabase db remote commit supabase db remote apply
Solution
Step 1: Analyze each command
supabase db pushapplies local schema changes to remote;supabase db remote commitgenerates a migration from remote schema differences;supabase db remote applyapplies pending migrations on remote.Step 2: Understand the flow
All commands target the remote database; no local application occurs in this sequence.Final Answer:
Remote migrations are applied without local testing -> Option CQuick Check:
db push + remote commit/apply = remote only [OK]
- Thinking 'supabase db push' applies locally
- Expecting local database changes from remote commands
- Believing commands have invalid syntax
supabase db reset locally but forgot to run supabase db remote apply. What issue will you face?Solution
Step 1: Understand local reset effect
supabase db resetreapplies all migrations to the local database only.Step 2: Understand remote apply necessity
Withoutsupabase db remote apply, the remote database remains unchanged, causing out-of-sync databases.Final Answer:
Local and remote databases will be out of sync -> Option DQuick Check:
Missing remote apply = databases out of sync [OK]
- Assuming remote updates automatically after local reset
- Thinking local reset affects remote database
- Believing migration files get deleted after reset
Solution
Step 1: Understand local vs remote migration commands
supabase db resetaffects local;supabase db remote applyapplies pending migrations to remote only.Step 2: Apply only remote changes
Create/edit migration file for new table, then runsupabase db remote apply; skip local commands likesupabase db reset.Final Answer:
Run supabase db remote apply without running supabase db reset -> Option AQuick Check:
Remote apply only = live DB updated, local unchanged [OK]
- Running local reset or push which updates local DB unnecessarily
- Resetting local DB when not needed
- Assuming remote apply requires local reset first
Ready to prove it?
Pass the test and this pattern moves unknown
