What if one small database change could break your whole app? Migrations save you from that nightmare.
Why Migrations for schema changes in Express? - Purpose & Use Cases
Imagine you have a database for your app, and you need to add a new column or change a table structure. You try to update the database manually on your computer, then again on your friend's computer, and again on the server.
Manually changing the database is slow and risky. You might forget a step, cause errors, or make the app stop working. It's hard to keep track of what changed and when, especially if many people work on the project.
Migrations let you write small, clear steps to change the database structure. These steps can run automatically and in order, so everyone's database stays the same and works perfectly with the app.
ALTER TABLE users ADD COLUMN age INT;
await migrationRunner.run('add-age-column'); // runs a saved migration scriptMigrations make it easy to update and share database changes safely across all environments.
When your app grows and you add new features needing new data fields, migrations update the database everywhere without breaking anything.
Manual database changes are error-prone and hard to track.
Migrations automate and organize schema updates.
This keeps all databases consistent and your app stable.
Practice
Solution
Step 1: Understand migrations role
Migrations help track and apply changes to the database schema in a controlled way.Step 2: Compare other options
Options A, B, and C relate to other app features, not schema changes.Final Answer:
To manage and apply database schema changes step-by-step -> Option CQuick Check:
Migrations = Manage schema changes [OK]
- Confusing migrations with routing or middleware
- Thinking migrations handle user login
- Assuming migrations serve static files
Solution
Step 1: Recall Sequelize CLI syntax
The correct command to create a migration issequelize migration:create --name <name>.Step 2: Check other options
Options B, C, and D use incorrect command structures not recognized by Sequelize CLI.Final Answer:
sequelize migration:create --name add-users-table -> Option DQuick Check:
Correct CLI command = sequelize migration:create --name add-users-table [OK]
- Mixing up command order or keywords
- Omitting '--name' flag
- Using 'new' instead of 'create'
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('Users', 'age', { type: Sequelize.INTEGER });
},
down: async (queryInterface) => {
await queryInterface.removeColumn('Users', 'age');
}
};
What will happen when this migration is run?Solution
Step 1: Analyze the 'up' function
The 'up' function adds a new column 'age' of type INTEGER to the 'Users' table.Step 2: Understand the 'down' function
The 'down' function reverses this by removing the 'age' column, but this runs only on rollback.Final Answer:
Adds an 'age' column of type INTEGER to the 'Users' table -> Option BQuick Check:
'up' adds column, 'down' removes column [OK]
- Confusing 'up' and 'down' functions
- Assuming 'down' runs on migration
- Thinking it creates a new table
module.exports = {
up: async (queryInterface) => {
await queryInterface.renameColumn('Users', 'username', 'user_name');
},
down: async (queryInterface) => {
await queryInterface.renameColumn('Users', 'user_name', 'username');
}
};
What is the most likely cause of the error?Solution
Step 1: Check renameColumn usage
renameColumn needs correct table and column names; no type argument is needed.Step 2: Consider error causes
If the table 'Users' does not exist or is misnamed, renameColumn will fail.Final Answer:
The table name 'Users' is incorrect or does not exist -> Option AQuick Check:
Table name must exist for renameColumn [OK]
- Assuming renameColumn needs column type
- Ignoring table name case sensitivity
- Forgetting to verify table existence
Solution
Step 1: Understand migration safety
Adding a column without checking can cause errors if it exists already.Step 2: Use raw SQL for conditional check
Running a raw SQL query to check if the column exists before adding prevents errors.Step 3: Evaluate other options
Directly add the column without checks; rely on migration rollback if error occurs risks errors, C ignores migration purpose, D is destructive and unsafe.Final Answer:
Use a raw SQL query inside the migration to check column existence before adding -> Option AQuick Check:
Check column first with SQL to avoid errors [OK]
- Skipping existence check causing migration failure
- Relying only on model sync without migration
- Dropping tables unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
