Sub topic 14
Backward-compatible schema changes with expand and contract
This chapter assumes you can write versioned SQL migrations and know that Flyway runs them at startup. It also assumes you know how ddl-auto: validate behaves.
Why a single migration can break a deployment
Flyway runs migrations when the new application version starts. During a rolling deployment, old instances keep running against the migrated schema until they are replaced. If the migration renames or drops something the old code uses, those instances fail. A rollback of the application has the same problem, because the old code meets a schema it does not understand.
The fix is to split a breaking change into steps. Each step works with both the old and the new application version.
The three phases
- Expand. Add the new structure and keep the old one. The old code ignores the new column.
- Migrate. Deploy code that writes to both structures and reads from the new one. Copy existing data across.
- Contract. After no old instance is running, remove the old structure in a later migration.
Worked example: renaming a column
Suppose app_user.username should become login. A single ALTER TABLE ... RENAME COLUMN would break every running old instance, so you do it in three releases.
Release N (expand). Add the column and copy the data:
-- src/main/resources/db/migration/V4__add_login_column.sql
ALTER TABLE app_user ADD COLUMN login VARCHAR(100);
UPDATE app_user SET login = username WHERE login IS NULL;Old code never touches login, so it keeps working.
Release N+1 (migrate). The entity maps both columns. Writes go to both, and reads use the new one:
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "app_user")
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username", nullable = false)
private String legacyUsername;
@Column(name = "login")
private String login;
public void setLogin(String value) {
this.login = value;
this.legacyUsername = value;
}
public String getLogin() {
return login;
}
}The setter keeps the old column filled, so instances from release N still see correct data. Rows written by old instances during the rollout have a null login. Run a second backfill, or have the code fall back to username when login is null.
Release N+2 (contract). Once monitoring shows no old instances, remove the old column and the extra mapping:
-- src/main/resources/db/migration/V5__drop_username.sql
ALTER TABLE app_user DROP COLUMN username;With ddl-auto: validate, Hibernate checks the entities at startup. The entity must match the schema in each release, so update the mapping in the same release as the migration that changes the table.
Which changes are safe
| Change | Safe in one step? | Approach |
|---|---|---|
| Add a nullable column | Yes | Single migration |
| Add a table | Yes | Single migration |
| Drop a column | No | Stop using it first, drop it in a later release |
| Rename a column or table | No | Expand, migrate, contract |
| Add `NOT NULL` | No | Add nullable, backfill, then add the constraint |
| Change a column type | No | Add a new column and migrate |
Gotchas
- Never edit an applied migration to "fix" a phase. Add a new versioned migration, because checksum validation rejects edits.
- Large backfills can hold locks and slow startup. For big tables, run the copy in batches, for example from a Java migration.
- Keep the contract migration out of the release that stops using the structure. Merging the phases brings back the original problem.
- Record the planned contract step as a ticket. Forgotten contract steps leave unused columns in the schema.
Exercise
Your table has a nullable column phone that you want to make NOT NULL. Name the steps that keep a rolling deployment safe.
Show answer
First release: make the application always write a value for phone, and add a migration that backfills existing null rows. Later release, once no old instance can insert nulls: add a migration with ALTER TABLE ... ALTER COLUMN phone SET NOT NULL. Adding the constraint in the first release would make inserts from old instances fail.