Files
spring-boot-demo/db-migrations-expand-contract/src/test/java/com/ankurm/expandcontract/DualWriteConsistencyTest.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

50 lines
2.1 KiB
Java

package com.ankurm.expandcontract;
import com.ankurm.expandcontract.customer.CustomerService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Deploy 2 (MIGRATE WRITES): Stage 2 code writes every create and update to both
* columns. This is the deploy that makes Deploy 3's read switch safe later - if this
* one is wrong, nothing downstream can be trusted. See docs/04-the-dual-write.md.
*/
class DualWriteConsistencyTest {
@Test
void createAndUpdatePopulateBothColumnsIdentically(@TempDir Path tmp) throws Exception {
Path db = tmp.resolve("dual-write");
TestSupport.migrateTo(db, "2");
Transcript t = Transcript.start("04-dual-write-consistency",
"Deploy 2 (MIGRATE WRITES): Stage 2 writes land in both columns");
CustomerService stage2 = new CustomerService(TestSupport.jdbcClient(db), 2);
long id = stage2.create("Margaret Hamilton", "[email protected]");
try (Connection conn = DriverManager.getConnection(TestSupport.jdbcUrl(db), "sa", "")) {
t.section("after create()");
String row = DbDump.table(conn, "select name, email, email_address from customers where id = " + id);
t.line(row);
assertThat(row).contains("[email protected]");
assertThat(row.indexOf("[email protected]")).isNotEqualTo(row.lastIndexOf("[email protected]"));
}
stage2.updateEmail(id, "[email protected]");
try (Connection conn = DriverManager.getConnection(TestSupport.jdbcUrl(db), "sa", "")) {
t.section("after updateEmail() - the old value is gone from BOTH columns, not just one");
String row = DbDump.table(conn, "select name, email, email_address from customers where id = " + id);
t.line(row);
assertThat(row).contains("[email protected]").doesNotContain("[email protected]");
}
t.write();
}
}