Node.js › Databases & ORMs (MongoDB/Mongoose, SQL/Sequelize)
What is a database migration, and how do tools like Sequelize's migration system or a standalone tool like Knex help manage schema changes over time?
IntermediateAnswer
A migration is a version-controlled script describing an incremental change to a database schema (adding a column, creating a table, adding an index) -- migration tools track which migrations have already been applied to a given database and let a team apply pending changes consistently across development, staging, and production environments, and often support reversing ('down') a migration if it needs to be undone, providing an auditable, repeatable history of how the schema evolved over time.
// Sequelize migration file
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('Users', 'phoneNumber', { type: Sequelize.STRING, allowNull: true });
},
down: async (queryInterface) => {
await queryInterface.removeColumn('Users', 'phoneNumber');
},
};
// npx sequelize-cli db:migrateReal-world example A team adding a new required 'phoneNumber' column writes a migration rather than manually altering the production database table by hand, ensuring the exact same schema change is applied consistently and in the correct order across every developer's local database, staging, and production.