Back to results for “CI/CD”
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?

Intermediate

Answer

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:migrate
Real-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.

Common follow-ups: Why is it considered risky to manually alter a production database schema outside of the migration system?;How do you handle a migration that needs to backfill data for existing rows, not just alter the schema structure?