
Every July, millions of animals journey between the Serengeti and Maasai Mara in search of fresh grazing land. The Great Migration features approximately 1.3 million blue wildebeest, 500,000 Thomson’s gazelles, 97,000 topi, 18,000 common elands, and 200,000 Grant’s zebras. Photo by David DiGregorio.
What is Database Migrations?
Database migrations also called schema migrations or simply migrations are structured sets of changes designed to modify the organization of a relational database. They allow your database to evolve from its current state to a desired state, whether that means adding or removing tables and columns, splitting fields, or updating data types and constraints.
Migrations handle incremental and often reversible changes programmatically, ensuring that database updates are repeatable, shareable, and testable without risking data loss. Migration tools generate artifacts that precisely define the steps needed to move a database from one version to another.
Think of migrations as a version control system for your database schema.
Django Migrations
Django migrations allow you to apply changes made to your models such as adding a field or deleting a model—directly to the database schema. For each model, Django generates a migration file inside the migrations folder, which defines the corresponding table structure. Each table in the database is mapped to the model for which the migration was created.
Django provides several commands to manage migrations:
makemigrations – Creates a migration file containing the code for a model’s table schema.migrate – Applies the migration to create or update tables in the database according to the schema.sqlmigrate – Displays the raw SQL query that will be executed for a migration.showmigrations – Lists all migrations along with their current status.Pros and Cons of Committing Django Migrations to Git
Reasons Not to Commit Migrations
Reasons to Commit Migrations
“The migration files for each app live in a ‘migrations’ directory inside of that app, and are designed to be committed to, and distributed as part of its codebase. You should create them on your development machine and then run the same migrations on your colleagues’ machines, staging, and production. Migrations produce consistent results, ensuring that what you see in development and staging is exactly what will happen in production.”
Conclusion
Sources