We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Intro to Migrations

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

Example of a Bad Migration

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.

A Safer Approach

Roll out schema and application changes in backward-compatible phases. Keep the old schema available until no running code depends on it.