0
0
Expressframework~10 mins

Migrations for schema changes in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Migrations for schema changes
Write migration file
Run migration command
Migration tool reads file
Apply schema changes to DB
Update migration history
App uses updated schema
This flow shows how you write a migration, run it, and the database schema updates step-by-step.
Execution Sample
Express
npx sequelize-cli migration:generate --name add-users-table
// Edit migration file to add table
npx sequelize-cli db:migrate
This example generates a migration file, edits it to add a users table, then runs the migration to update the database.
Execution Table
StepActionTool OutputDatabase StateMigration History
1Generate migration fileCreated file: add-users-table.jsNo changeNo new entry
2Edit migration fileFile updated with createTable codeNo changeNo new entry
3Run migration commandExecuting migration scripts...Users table createdMigration 'add-users-table' recorded
4Verify migrationMigration successfulUsers table existsMigration recorded
5Run migration againNo pending migrationsNo changeNo new entry
💡 No pending migrations left to apply, database schema is up to date
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Migration Files[][add-users-table.js][add-users-table.js][add-users-table.js][add-users-table.js][add-users-table.js]
Database SchemaInitial schemaInitial schemaInitial schemaSchema + users tableSchema + users tableSchema + users table
Migration History[][][]['add-users-table']['add-users-table']['add-users-table']
Key Moments - 2 Insights
Why does running the migration command twice not change the database the second time?
Because the migration history shows the migration was already applied (see step 5 in execution_table), so the tool skips it to avoid duplicate changes.
What happens if you forget to edit the generated migration file before running it?
The migration runs but makes no schema changes because the file has no instructions (see step 2 and 3 in execution_table). The database stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the database state after step 3?
ANo change
BUsers table created
CMigration history empty
DMigration failed
💡 Hint
Check the 'Database State' column at step 3 in the execution_table.
At which step does the migration history get updated?
AStep 3
BStep 5
CStep 1
DStep 2
💡 Hint
Look at the 'Migration History' column in execution_table rows.
If you run the migration command again after step 4, what will the tool output say?
AExecuting migration scripts...
BError: migration file missing
CNo pending migrations
DMigration successful
💡 Hint
See step 5 in execution_table for repeated migration run output.
Concept Snapshot
Migrations help update database schema safely.
Write migration files describing changes.
Run migration commands to apply changes.
Migration history tracks applied changes.
Running migrations twice skips already applied ones.
Full Transcript
Migrations for schema changes in Express apps involve creating migration files that describe how to change the database structure. You generate a migration file, edit it to add or modify tables or columns, then run a migration command. The migration tool reads the file and applies the changes to the database. It also records the migration in a history log to avoid repeating the same changes. Running migrations again after all are applied results in no action. This process helps keep the database schema in sync with your app code safely and predictably.