

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: Creating a Table
incomplete
2: Create Table Practice
incomplete
3: Altering Tables
incomplete
4: Intro to Migrations
incomplete
5: Up Migration
incomplete
6: Down Migration
incomplete
7: Migration Review
incomplete
8: SQL Data Types
incomplete
9: Practice – Posts Table
incomplete
10: Practice – Posts Table Migration
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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.
Roll out schema and application changes in backward-compatible phases. Keep the old schema available until no running code depends on it.