Node.js › Databases

What is database migration, and why is it important to manage schema changes through versioned migration files rather than manual changes?

Intermediate

Answer

A migration is a versioned, incremental script describing a specific schema change (adding a column, creating a table) that can be applied and, ideally, reversed in a controlled, repeatable, and auditable way -- managing schema changes through migration files (checked into version control alongside application code) rather than manual database edits ensures every environment's schema stays in a known, consistent, and reproducible state, and lets the team track exactly when and why each change was made.
// Example migration file (using a tool like Knex or Sequelize)
exports.up = async (knex) => {
  await knex.schema.table('users', (table) => {
    table.string('phone_number').nullable();
  });
};
exports.down = async (knex) => {
  await knex.schema.table('users', (table) => table.dropColumn('phone_number'));
};
Real-world example A team deploying a new feature requiring an additional database column writes a migration file rather than manually running an ALTER TABLE statement against production, ensuring the exact same schema change is automatically and consistently applied across development, staging, and production databases.

Common follow-ups: How do you handle a migration that needs to run against a production database with live traffic and zero downtime?;What's the risk of a migration that isn't properly reversible via a working 'down' script?