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?"