Skip to content

Database migrations

Introduction

Write "changing the table structure" as numbered scripts that are committed, reviewed, and rolled back like code, instead of someone logging into the database to change it by hand.

Why use it

  • Repeatable: a new teammate or server runs all migrations and gets the same schema as production.
  • Reviewable: schema changes go through code review instead of happening quietly.
  • Reversible: each migration usually has both an “up” and a “down” direction.
  • Automatable: the deploy pipeline runs them; nobody has to remember.

How to do it

  1. Change the model code (entity classes), not the database directly.
  2. Let the tool compare the model to the database and generate a migration script.
  3. Review the script by hand (especially drops and type changes).
  4. Run the migration during deploy.

Component comparison

EF Core Migrations

Built into the frameworkOpsLow

The migration feature built into C# EF Core: after you change the data model in code, it automatically generates the database change script.

Best forC# projects using EF Core.

Avoid whenThe project does not use EF Core, or you need to hand-write complex data-modification scripts.

Pros

After a model change, one command generates the migration, saving hand-written SQL

Tightly integrated with EF Core, so the team picks it up quickly

Can generate SQL scripts for ops to review before running

Cons

Generated scripts are sometimes not what you want (for example, a rename is detected as drop-then-add and may lose data), so they must be checked by hand

When several people change the model at once, migration files conflict easily

Only for projects using EF Core

Official docs ↗

DbUp

Self-hosted open sourceOpen sourceOpsLow

A lightweight C# migration library: you write SQL scripts one by one, and it runs them in order and records which have already run.

Best forC# projects that do not use EF Core and want full control of SQL.

Avoid whenThe team is unfamiliar with SQL, or wants changes generated automatically from the model.

Pros

You write all the SQL, so behavior is predictable with no surprise generation

No ORM dependency; works for Dapper and raw SQL projects

Simple way of working, easy to troubleshoot

Cons

Every change needs hand-written SQL, more work than auto-generation

No automatic rollback; to revert you must write the reverse script yourself

SQL differs across databases, so switching databases means rewriting scripts

Official docs ↗

Alembic

Self-hosted open sourceOpen sourceOpsLow

The migration tool that goes with SQLAlchemy, which compares the model against the database and generates change scripts automatically.

Best forPython projects using SQLAlchemy, such as FastAPI and Flask.

Avoid whenThe project uses Django (which has built-in migrations), or does not use SQLAlchemy.

Pros

It can compare the models and generate a migration script automatically

Every migration provides both an upgrade and a downgrade

It is not tied to a web framework, so FastAPI and others can use it

Cons

Automatic generation is not always accurate, and a rename needs a person to look

It only helps if you are using SQLAlchemy

Config and the environment are yours to set up, and the first time is fiddly

Official docs ↗

Django migrations

Built into the frameworkOpsLow

The migration feature built into the Django framework: after you change a model, it generates migration files and applies them to the database automatically.

Best forProjects using Django.

Avoid whenThe project does not use Django.

Pros

It is built in with the framework, and almost no extra configuration is needed

It can notice model changes on its own, and the workflow is mature

Migration files can be committed, so the team shares them

Cons

It applies only to the ORM that ships with Django

Once there are many migration files, merge conflicts have to be handled

Changing structure on a large table can lock that table for a long time

Official docs ↗

Flyway / Liquibase

Self-hosted open sourceOpen sourceOpsLow

Language-agnostic standalone migration tools that run scripts in version order and record the database's current version.

Best forProjects where several services share one database, or where the team wants to unify its migration approach.

Avoid whenSmall projects that already have framework-built-in migrations and need no extra layer.

Pros

Not tied to any language or ORM, suited to multi-language or mixed-stack teams

Scripts go into version control, so the upgrade process is identical in every environment

Commonly run automatically in CI/CD pipelines

Cons

Must be integrated into the deployment flow, and it is one more tool to learn

Scripts are mostly hand-written, with limited auto-generation

It needs its own runtime environment, so using its container image is recommended

Official docs ↗

Prisma Migrate

Open-source libraryOpen sourceOpsLow

Generates and runs SQL migration files from the Prisma schema.

Best forProjects that use Prisma.

Avoid whenYou are not using Prisma.

Pros

Migrations are generated automatically from schema diffs

Clearly separated development and production commands (dev / deploy)

Shares a source with the Prisma client

Cons

Only applicable to projects that use Prisma

Complex changes require hand-editing the generated SQL

Drift detection across environments must be ensured by process

Official docs ↗

drizzle-kit

Open-source libraryOpen sourceOpsLow

The migration generation and execution tool that accompanies Drizzle ORM.

Best forProjects that use Drizzle ORM.

Avoid whenYou are not using Drizzle.

Pros

Generates SQL migrations from schema diffs

Migration files are plain SQL and easy to review

Shares a source with Drizzle

Cons

Only applicable to projects that use Drizzle

The tool iterates quickly, so watch the release notes

Breaking changes must be split up manually

Official docs ↗

goose

Open-source libraryOpen sourceOpsLow

A database migration tool for Go; migrations are written in SQL or Go and can run embedded in your program.

Best forGo projects that use sqlc or hand-written SQL.

Avoid whenYou want migrations generated automatically from models.

Pros

SQL migration files are intuitive

Go migrations handle complex data fixes

Can be embedded as a library and also has a command line

Cons

Migration ordering and conflicts must be managed by team process

Does not generate migrations from models automatically

You must write and verify rollback scripts yourself

Official docs ↗

SQLx migrate

Open-source libraryOpen sourceOpsLow

SQLx's built-in SQL migration tool: write plain SQL files and run them with sqlx-cli or at process start.

Best forRust projects that use SQLx.

Avoid whenYou want migrations generated from entity models (see SeaORM migrations).

Pros

Migration files are plain SQL and easy to review

Same stack as SQLx, the usual choice for Axum projects

Can run migrate in CI and at startup

Cons

Does not generate migrations from models

You must write and verify rollback scripts

Cross-environment drift must be caught by process and checks

Official docs ↗

Checklist

  • Migration scripts are committed to version control
  • The deploy pipeline runs migrations automatically
  • Destructive changes (drop column / change type) are done in steps
  • The database is backed up before production migrations