Files
spring-boot-demo/db-migrations-expand-contract/src/test/java/com/ankurm/expandcontract/ExpandMigrationBackwardCompatibleTest.java
T
asmhatreandClaude Sonnet 5 e478eafda3 Add db-migrations-expand-contract: zero-downtime schema migrations proven with a real 4-deploy rolling run
Companion code for Zero-Downtime Database Migrations: Expand-Contract in Practice
with Spring Boot: a full expand/migrate-writes/migrate-reads/contract sequence run
as an actual rolling deploy across two live replicas, with a load generator sending
continuous HTTP traffic through all four deploys (99.98% success, every residual
error traced to a root cause rather than left unexplained). Findings include a real
NOT NULL constraint trap in the expand migration, a backfill-window bug in the read
switch, H2's AUTO_SERVER=TRUE single-point-of-failure behavior under a rolling
restart, the drain-before-SIGTERM fix needed to close a health-check gap during
graceful shutdown, and H2 silently discarding a concurrently committed INSERT during
an ALTER TABLE ADD/DROP COLUMN rebuild - confirmed, by primary source, to be an
H2-specific behavior rather than a property of the technique itself.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019Fb7vW8vLyLKngBc4R3huA
2026-09-16 19:22:48 +00:00

73 lines
3.6 KiB
Java

package com.ankurm.expandcontract;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.jdbc.core.simple.JdbcClient;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Deploy 1 (EXPAND) in isolation: migrate to V2 and confirm two things a real rollout
* depends on. First, that every pre-existing row got backfilled in the same migration.
* Second - the part that is easy to get wrong - that Stage 1 code, completely unaware
* the new column exists, still inserts rows exactly as it always has. If this second
* assertion ever failed, the migration would not be additive and expand-contract would
* not apply to it. See docs/02-the-expand-migration.md.
*/
class ExpandMigrationBackwardCompatibleTest {
@Test
void backfillsExistingRowsAndStaysCompatibleWithStage1Code(@TempDir Path tmp) throws Exception {
Path db = tmp.resolve("expand");
Transcript t = Transcript.start("02-expand-backward-compatible",
"Deploy 1 (EXPAND): additive column + backfill, Stage 1 code untouched");
// Seed one row the way Stage 1 always has, before the expand migration exists.
TestSupport.migrateTo(db, "1");
JdbcClient preExpand = TestSupport.jdbcClient(db);
preExpand.sql("INSERT INTO customers(name, email) VALUES (?, ?)")
.param("Ada Lovelace").param("[email protected]").update();
t.section("schema before Deploy 1");
try (Connection conn = DriverManager.getConnection(TestSupport.jdbcUrl(db), "sa", "")) {
t.line(DbDump.table(conn, "select column_name from information_schema.columns "
+ "where table_name = 'CUSTOMERS' order by ordinal_position"));
}
// Deploy 1: the expand migration runs against the live database. No app restart.
TestSupport.migrateTo(db, "2");
t.section("schema after Deploy 1 (email_address added)");
try (Connection conn = DriverManager.getConnection(TestSupport.jdbcUrl(db), "sa", "")) {
t.line(DbDump.table(conn, "select column_name from information_schema.columns "
+ "where table_name = 'CUSTOMERS' order by ordinal_position"));
t.section("Ada's row was backfilled by the migration itself");
String row = DbDump.table(conn, "select name, email, email_address from customers where name = 'Ada Lovelace'");
t.line(row);
// Backfilled: both columns hold the same value for a row that predates dual-write code.
assertThat(row).contains("[email protected]");
assertThat(row.indexOf("[email protected]")).isNotEqualTo(row.lastIndexOf("[email protected]"));
}
// Stage 1 code has not been redeployed and does not know email_address exists.
// Its insert statement is byte-for-byte what it was before Deploy 1.
JdbcClient postExpand = TestSupport.jdbcClient(db);
postExpand.sql("INSERT INTO customers(name, email) VALUES (?, ?)")
.param("Grace Hopper").param("[email protected]").update();
t.section("Stage 1's original INSERT still works, unmodified, after the migration");
try (Connection conn = DriverManager.getConnection(TestSupport.jdbcUrl(db), "sa", "")) {
String row = DbDump.table(conn, "select name, email, email_address from customers where name = 'Grace Hopper'");
t.line(row);
assertThat(row).contains("[email protected]").contains("NULL");
}
t.write();
}
}