A database migration is a change to the structure of a relational database. You can think of it like a commit in Git, but for your database schema. Every migration records how the structure of your data evolves over time.
For example, when we previously used an ALTER TABLE statement to add a new column, we were performing a migration.
Migrations are essential for adapting your database to changing requirements, fixing mistakes, and rolling out new features. In a team setting, migrations ensure everyone applies the same changes in the same order.
Good migrations are small, incremental and ideally reversible changes to a database. As you can imagine, when working with large databases, making changes can be scary! We have to be careful when writing database migrations so that we don't break any systems that depend on the old database schema.
Click to play video
Let’s say the CashPal backend runs this SQL regularly:
SELECT * FROM people;
If we rename the table from people to users in a migration but forget to update the code, this query will break because the people table no longer exists.
Migrations must be carefully coordinated with application changes to avoid outages.