Writing safe migrations
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write migrations that are forward-only and low risk.
Planned topics
- Never edit an applied migration; fix mistakes with a new one.
- Forward-only: do not rely on "down" scripts; to undo, write a new forward migration.
- Add columns as nullable (or with a default), backfill, then add
not nullin a later step. - Keep each migration small and focused; one change per file.
- Test on a copy of real data before production; big changes (rename, drop) need care and are covered in MID-020.
Draft outline details
Demonstration
-- V2__add_sku.sql
alter table product add column sku text;
update product set sku = 'SKU-' || id where sku is null;
alter table product alter column sku set not null;
create unique index ux_product_sku on product(sku);Check your understanding
Your V2 has a typo and already ran. What do you do?
Show answer
Write V3 to fix it; never edit V2.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.