📖 Scenario: You are building a simple Express app that stores user information in a database. Over time, you need to change the database schema safely without losing data. This project will guide you through creating a migration script to update the schema.
🎯 Goal: Create a migration script using Express and a migration tool to add a new column age to the users table.
📋 What You'll Learn
Create an initial users table schema
Add a configuration variable for the new column
Write a migration script to add the new column
Complete the migration setup to run the migration
💡 Why This Matters
🌍 Real World
Migrations help update database schemas safely as apps grow and change, avoiding data loss.
💼 Career
Knowing migrations is essential for backend developers to manage database changes in production environments.
Progress0 / 4 steps
1
Create initial users table schema
Create a migration file that defines a users table with columns id (integer, primary key) and name (string). Use the exports.up function to create the table.
Express
Hint
Use knex.schema.createTable inside exports.up to create the table with id and name.
2
Add configuration for new column
Create a constant called newColumn and set it to the string 'age'. This will be the name of the new column to add.
Express
Hint
Define const newColumn = 'age'; above the migration functions.
3
Write migration to add new column
Modify the exports.up function to add the new column age as an integer to the existing users table using knex.schema.table. Use the newColumn constant for the column name.
Express
Hint
Use knex.schema.table('users', ...) to add the age column as integer in exports.up. Remove it in exports.down.
4
Complete migration setup
Export the migration functions exports.up and exports.down so they can be run by the migration tool. Ensure the file exports both functions correctly.
Express
Hint
Make sure both exports.up and exports.down are defined and exported.
Practice
(1/5)
1. What is the main purpose of migrations in an Express app using a database?
easy
A. To create API routes for the app
B. To handle user authentication automatically
C. To manage and apply database schema changes step-by-step
D. To serve static files like images and CSS
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 C
A. The table name 'Users' is incorrect or does not exist
B. Missing Sequelize parameter in the functions
C. renameColumn requires the column type as a third argument
D. The migration file name is invalid
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 A
Quick Check:
Table name must exist for renameColumn [OK]
Hint: Check table and column names exist before renaming [OK]
Common Mistakes:
Assuming renameColumn needs column type
Ignoring table name case sensitivity
Forgetting to verify table existence
5. You want to add a new column 'email' to the 'Users' table but only if it doesn't already exist, to avoid errors during migration. Which approach best handles this in an Express app migration?
hard
A. Use a raw SQL query inside the migration to check column existence before adding
B. Directly add the column without checks; rely on migration rollback if error occurs
C. Modify the model only and skip migration since model syncs automatically
D. Delete the 'Users' table first, then add the column
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 A
Quick Check:
Check column first with SQL to avoid errors [OK]
Hint: Check column existence with SQL before adding [OK]
Common Mistakes:
Skipping existence check causing migration failure
Relying only on model sync without migration
Dropping tables unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.