Migrations & Why are they important?
What are Migrations?
Migrations are files that help manage transitions between current database schemas and future database schemas. These could involve adding tables and columns, removing fields, changing types and constraints.
Why are they important?
- Migrations help transport your database changes correctly from one environment to another (Eg. From Staging environment to Production environment).
- This will ensure that any breaking changes w.r.t database are tested in Staging environment (For eg. Adding a new non-empty constraint and this can break existing data with empty fields). It also has rules to specify what to do when a database change affects existing data.
- Migrations are incremental and each new migration file tells you what schema changes have occurred from the last migration file.
Code Review
They help in Code Review, as the reviewer exactly know what is happening in the database and can then identify any problems that can occur in production. Reviewers can catch issues like:
- Adding a new non-empty constraint
- Not having foreign key constraints for a new column
- Not having indexes for some joins/queries you are planning in your near feature
Are there libraries that help implementing migrations?
- For NodeJS backends, Prisma Migrate library can be used among other libraries.
- With Python/Django framework, Django Migrations are already implemented in the framework.
What is a development workflow with migrations?
There are many workflows that one can follow while implementing migration files when adding a new feature. Here’s a basic one:
- During development - Develop your feature first and ensure that you have arrived at a decent version of your feature. Then create migration and commit it to your feature branch.
- During staging or production, you make sure to pull in the migrations and run it. This could be two steps as sometimes migrations dont get applied automatically, as there might be issue with the database state before the migration can be applied.
- So testing in staging is important to ensure that there are no issues with the migration files.
Your production should have automatic backups. And it’s a good idea to do a manual backup before a major migration, if you think there might be an issue.
Have you implemented migrations in your project? What are the challenges you faced?