Web Development

Database migration

A database migration is a recorded, repeatable change to a database schema or structure that allows software teams to update environments consistently.

also called: schema migration

// definition

A database migration is a version-controlled instruction set that alters the structure or content of a database management system. In web development, applications rely on structured tables, columns, and relationships to process user data. When software developers introduce new features that require structural modifications, such as adding a new table or altering a data field, they write migration scripts. These scripts record exact changes and execute them in a chronological sequence across development, testing, and production environments.

Migrations ensure that every instance of an application runs on a uniform data structure. They typically include both forward instructions to apply changes and rollback instructions to revert them if issues arise. By treating schema updates as code, organizations maintain automated, audit-friendly, and repeatable processes for software deployments.

// why it matters

Operating a web application without database migrations creates severe risks of data corruption, configuration drift, and application downtime. As multiple developers build features simultaneously, manual database updates often lead to mismatched environments where code fails unexpectedly in production. Automated migrations standardize updates, allowing teams to ship new software capabilities safely and frequently. Furthermore, migrations enable rapid recovery by providing a documented, tested path to roll back problematic schema changes without corrupting user data or forcing extended system outages.

// example

An e-commerce platform needs to add a phone number field to its customer accounts. A developer writes a migration script that adds a text column called phone_number to the users table. When deployed, the migration tool automatically detects that the production database lacks this column and executes the command. If the release encounters an unexpected error, the platform executes the corresponding rollback script to remove the column cleanly.

Questions and Answers

What is the difference between a schema migration and a data migration?
A schema migration alters the structural layout of a database, such as modifying tables, columns, or indexes. A data migration moves or converts the actual records stored within those structures, often adjusting existing data to fit a new format. Both types use version-controlled scripts to ensure repeatable deployments across environments.
How do automated migrations prevent downtime during deployments?
Modern application platforms execute database migrations using non-blocking commands or backward-compatible release steps. By breaking complex structural changes into smaller, non-destructive phases, software engineering teams can apply updates while the system remains fully operational, ensuring continuous availability for website users during software releases.
What happens if a database migration fails midway through execution?
Most migration tools wrap individual changes inside database transactions. If an error occurs, the database rolls back the transaction, leaving the schema in its pre-migration state. If transactional rollbacks are not supported for specific operations, teams execute explicit down-migration scripts to safely restore original database structures.