Chapter 18: Rollback and Data Migrations
Learning Objectives
By the end of this chapter, you will be able to:
- Explain why rolling back an image does not roll back a database, and why a point-in-time restore is not a rollback when users keep writing
- Split a schema change into expand and contract steps so that the previous release always runs on the new schema
- Trace where this platform’s migrations run - an initContainer, before the app starts - and what the app checks at startup
- Roll back a release that migrated, prove that no data was lost, and fix a broken schema by rolling forward
Recall Check
Before continuing, quickly recall:
- How do you roll back a release on this platform - and what makes the rollback hold? (Chapter 10)
- What does a point-in-time restore give back, and where does it restore to? (Chapter 12)
- Why is the revert of a release the first thing to check during an incident - and what has to be true before you run it? (Chapter 15)
If you can’t answer these, revisit the corresponding chapter before proceeding.
Context
Chapter 10 made a rollback one line in Git: the previous image tag. That holds while a release changes only code. The backend also owns a database - the app database of the CloudNativePG cluster app-postgres - and a release can change its schema. An image goes back in seconds; the schema stays where the new release left it. If the previous release cannot run on that schema, the rollback fails exactly when it is needed. Chapter 12’s point-in-time restore can take the database back too - and with it every row users wrote since. On a system that people use, that is not a rollback; it is a second incident.
This chapter does not try to cover every way to change a schema safely. It shows one of the most widely used approaches, the one this platform runs: expand/contract, roll forward, migrations in an initContainer. Other approaches work too, each with its own costs:
- down migrations - a reverse script for every change; it undoes the schema, but cannot give back data that a drop removed;
- a migration Job before the deploy - a Kubernetes Job, a Helm hook or an Argo CD PreSync hook instead of an initContainer;
- online schema change tools - pgroll, gh-ost, pt-online-schema-change - which keep the old and new schema live at once, for large tables and zero-downtime changes;
- declarative schema operators - Atlas, SchemaHero - where Git holds the desired schema and a controller computes the change;
- event sourcing - the data is a log of events, and a schema is only a view that can be rebuilt.
Work through each lesson section; the chapter video, once it is published, gives the concept overview.