database migration
A database migration is a versioned, incremental change to a database’s schema or data, written as a script that moves the database from one known state to the next.
Application code and the database it talks to evolve together. When a new feature needs a column, a table, or a database index, the matching change is recorded as a migration and committed to version control alongside the code.
Applying the migrations in order replays the schema’s history. That way, a developer’s laptop, a test server, and production all end up with the same structure.
Migration tools keep a bookkeeping table inside the database itself that records which migrations have already run. Alembic calls it alembic_version, and Flyway calls it flyway_schema_history. On each run, the tool compares that record with the available migration files and applies only the pending ones, in sequence.
In the widget below, three copies of the same database start at different versions. Pick one and upgrade or downgrade it to watch the tool read alembic_version and apply only what’s pending:
Most migration systems share a few properties:
- Ordering: Each migration carries an identifier, such as a timestamp, a sequence number, or a link to its parent revision, that fixes its place in the history.
- Reversibility: A migration can pair its forward step, often called upgrade, with a backward step, often called downgrade, that undoes it. Some changes, like dropping a column, can’t be reversed without losing data.
- Schema and data changes: Schema migrations alter structure with SQL statements like
ALTER TABLE. Data migrations rewrite existing rows to fit the new structure.
Migrations can be written by hand or generated. Frameworks built on object-relational mapping, such as Django, and tools like Alembic can compare model definitions with the current schema and draft a migration automatically.
The term also has a broader sense. It can mean moving an entire database to a different server or database system, such as from MySQL to PostgreSQL.
Related Resources
Tutorial
Django Migrations: A Primer
Get comfortable with Django migrations: create database tables without writing SQL, and keep your database in step as your models change.
For additional information on related topics, take a look at the following resources:
- Digging Deeper Into Django Migrations (Tutorial)
- Django Migrations 101 (Course)
- Data Management With Python, SQLite, and SQLAlchemy (Tutorial)
- SQLite and SQLAlchemy in Python: Move Your Data Beyond Flat Files (Course)
- Python REST APIs With Flask, Connexion, and SQLAlchemy – Part 1 (Tutorial)
- Data Management With Python, SQLite, and SQLAlchemy (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Nov. 25, 2026