0
0
Supabasecloud~10 mins

Why migrations version your database schema in Supabase - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why migrations version your database schema
Start Migration
Check Current Schema Version
Compare with Migration Version
If Newer
Apply Migration Changes
Update Schema Version
End Migration
If Not Newer
Skip Migration
End Migration
The migration process checks the current schema version, applies changes only if the migration is newer, then updates the version to keep track.
Execution Sample
Supabase
-- Migration script example
BEGIN;
-- Add new column
ALTER TABLE users ADD COLUMN age INT;
-- Update version
INSERT INTO schema_migrations (version) VALUES ('20240601_add_age');
COMMIT;
This migration adds a new column and records its version to track schema changes.
Process Table
StepActionCurrent VersionMigration VersionDecisionResult
1Start migration20240530_initial20240601_add_ageCheck versionsProceed
2Compare versions20240530_initial20240601_add_ageMigration is newerApply changes
3Apply changes20240530_initial20240601_add_ageAdd column 'age'Column added
4Update version20240530_initial20240601_add_ageRecord new versionVersion updated
5End migration20240601_add_age20240601_add_ageMigration completeSchema up to date
6Start migration20240601_add_age20240530_initialCheck versionsSkip migration
7End migration20240601_add_age20240530_initialMigration older or sameNo changes applied
💡 Migration stops when the migration version is not newer than the current schema version.
Status Tracker
VariableStartAfter Step 2After Step 4Final
Current Schema Version20240530_initial20240530_initial20240601_add_age20240601_add_age
Migration Version20240601_add_age20240601_add_age20240601_add_age20240601_add_age
Schema Changes AppliedNoNoYesYes
Key Moments - 2 Insights
Why do migrations check the current schema version before applying changes?
To avoid reapplying the same changes or applying older migrations, ensuring the database schema stays consistent as shown in steps 1 and 2 of the execution table.
What happens if a migration version is older than the current schema version?
The migration is skipped to prevent overwriting newer schema changes, as seen in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Current Schema Version after step 4?
A20240601_add_age
B20240530_initial
CNot updated yet
DMigration version
💡 Hint
Check the 'Current Schema Version' column at step 4 in the execution table.
At which step does the migration decide to skip applying changes?
AStep 5
BStep 6
CStep 2
DStep 3
💡 Hint
Look for the step where the decision is 'Skip migration' in the execution table.
If the migration version was the same as the current version, what would happen?
AMigration applies changes again
BMigration version updates to a new value
CMigration skips applying changes
DMigration fails with error
💡 Hint
Refer to the logic in the concept flow and steps 6-7 in the execution table.
Concept Snapshot
Migrations track schema changes by versioning.
They check current schema version before applying.
Only newer migrations apply changes.
Version updates prevent repeated or outdated changes.
This keeps database schema consistent and safe.
Full Transcript
Migrations version your database schema to keep track of changes over time. When a migration runs, it first checks the current schema version stored in the database. If the migration's version is newer, it applies the schema changes like adding or modifying tables or columns. After applying, it updates the stored version to mark the schema as up to date. If the migration version is older or the same, it skips applying changes to avoid conflicts or repeated work. This process ensures the database schema evolves safely and consistently as your application grows.