0
0
Expressframework~30 mins

Migrations for schema changes in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Migrations for schema changes
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Make sure both exports.up and exports.down are defined and exported.