0
0
Supabasecloud~10 mins

Creating migrations in Supabase - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - Creating migrations
Write migration SQL file
Run migration command
Supabase applies changes
Database schema updated
Migration recorded in history
Ready for next migration
This flow shows how you write a migration, run it, and Supabase updates the database and records the change.
Execution Sample
Supabase
supabase migration new add_users_table
-- edit migration SQL file
supabase db push
Create a new migration file, edit it to add a users table, then apply it to update the database.
Process Table
StepActionCommand/SQLResultNotes
1Create new migration filesupabase migration new add_users_tableNew SQL file createdFile named with timestamp and description
2Edit migration fileCREATE TABLE users (id UUID PRIMARY KEY, name TEXT);SQL ready to runDefines new users table
3Run migrationsupabase db pushMigration appliedDatabase schema updated
4Record migrationInternal Supabase processMigration loggedPrevents reapplying same migration
5Verify schemaSELECT * FROM users;Empty users table existsSchema change confirmed
6ExitNo more migrationsProcess completeReady for next migration
💡 All migration steps completed and database schema updated
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
migration_filenonecreated empty filecontains CREATE TABLE SQLunchangedunchangedexists with SQL
database_schemaoriginal schemaunchangedunchangedusers table addedunchangedusers table present
migration_historyemptyunchangedunchangedunchangednew migration recordedcontains migration record
Key Moments - 3 Insights
Why do we need to edit the migration file after creating it?
Creating the migration file only makes an empty template. You must add SQL commands to define the changes you want, as shown in step 2 of the execution_table.
What happens if you run 'supabase db push' without editing the migration file?
No changes will be applied because the migration file has no SQL commands. The database schema stays the same, as implied between steps 1 and 3.
How does Supabase prevent applying the same migration twice?
Supabase records each applied migration in its history (step 4). This log ensures migrations are only applied once.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after running 'supabase migration new add_users_table'?
ADatabase schema updated
BMigration applied
CNew SQL file created
DMigration logged
💡 Hint
Check step 1 in the execution_table for the result of the migration new command.
At which step does the database schema get updated with the new users table?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Result' column in the execution_table to find when the schema changes.
If you forget to add SQL commands to the migration file, what will happen when you run 'supabase db push'?
AMigration applies but no schema changes
BMigration applies and updates schema
CMigration fails with error
DMigration file is deleted
💡 Hint
Refer to key_moments about running migration without editing the file.
Concept Snapshot
Creating migrations in Supabase:
1. Run 'supabase migration new <name>' to create a new migration file.
2. Edit the file to add SQL commands for schema changes.
3. Run 'supabase db push' to apply changes to the database.
4. Supabase records the migration to avoid duplicates.
This process updates your database schema safely and trackably.
Full Transcript
Creating migrations in Supabase involves making a new migration file with 'supabase migration new', editing it to add SQL commands like creating tables, then running 'supabase db push' to apply these changes to the database. Supabase updates the schema and records the migration to prevent reapplying it. This step-by-step process ensures your database evolves in a controlled way.