DAT-20Recommended
Change the schema only with migrations
Every schema change is a numbered migration file in version control. Nobody changes a shared database by hand.
How to do it
Use a migration tool, such as Flyway, Liquibase, Alembic, Prisma Migrate or the framework's own. Each file runs once, in order, and the database records which ran. The deploy runs pending migrations before the new code starts.
How to check
Build a fresh database from the migrations alone. Its schema matches production exactly, compared with a schema dump.
Sources
Related rules
- ENG-13 Keep every change in version control in Engineering
- ENG-43 Run one-off tasks as reviewed scripts in Engineering
- STK-14 Learn SQL and write it with parameters in Software choices
- STK-21 Use Django for a Python web app in Software choices
- STK-32 Use the migration tool that comes with your ORM in Software choices
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/migrations/versioned-migrations">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.