Rolling back migrations in Supabase - Time & Space Complexity
When rolling back migrations in Supabase, it's important to understand how the time to undo changes grows as more migrations exist.
We want to know how the rollback process scales with the number of migrations applied.
Analyze the time complexity of rolling back migrations one by one.
// Rollback the last migration
await supabase.migrations.rollback();
// Rollback multiple migrations in a loop
for (let i = 0; i < n; i++) {
await supabase.migrations.rollback();
}
This sequence rolls back the last migration once, or multiple times to undo several migrations in order.
Look at what happens repeatedly during rollback.
- Primary operation: Calling the rollback API to undo one migration.
- How many times: Once per migration being undone.
Each rollback call undoes one migration, so the total calls grow directly with the number of migrations.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 rollback calls |
| 100 | 100 rollback calls |
| 1000 | 1000 rollback calls |
Pattern observation: The number of rollback calls increases one-to-one with the number of migrations to undo.
Time Complexity: O(n)
This means the time to rollback grows linearly with how many migrations you want to undo.
[X] Wrong: "Rolling back multiple migrations happens all at once in a single call."
[OK] Correct: Each rollback call only undoes one migration, so multiple calls are needed for multiple rollbacks.
Understanding how rollback operations scale helps you design reliable deployment processes and troubleshoot database changes confidently.
"What if the rollback command could undo multiple migrations in one call? How would the time complexity change?"