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", "margaret@example.test"); 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("margaret@example.test"); assertThat(row.indexOf("margaret@example.test")).isNotEqualTo(row.lastIndexOf("margaret@example.test")); } stage2.updateEmail(id, "m.hamilton@example.test"); 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("m.hamilton@example.test").doesNotContain("margaret@example.test"); } t.write(); } }