package com.ankurm.expandcontract; import com.ankurm.expandcontract.customer.Customer; import com.ankurm.expandcontract.customer.CustomerService; 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 static org.assertj.core.api.Assertions.assertThat; /** * Deploy 4 (CONTRACT), in both directions. First the case that must work: once every * replica is confirmed on Stage 4 and the drop migration (V3) has run, Stage 4 code * keeps working exactly as before - it never referenced "email" to begin with. Second * the case that must fail loudly: a Stage 1, 2 or 3 instance that is somehow still * running against the post-drop schema (a rollback gone wrong, a forgotten canary) * gets a real SQL error the moment it tries to touch the column that no longer exists. * That failure is not a bug in this article's design - it is *why* Deploy 4 has to wait * for confirmation that the fleet is 100% on Stage 4 first. See * docs/09-the-contract-migration.md and docs/10-what-happens-if-you-drop-too-soon.md. */ class ContractSafetyTest { @Test void stage4KeepsWorkingAfterTheColumnIsDropped(@TempDir Path tmp) throws Exception { Path db = tmp.resolve("contract-ok"); TestSupport.migrateTo(db, "2"); JdbcClient jdbc = TestSupport.jdbcClient(db); Transcript t = Transcript.start("09-contract-safety", "Deploy 4 (CONTRACT): Stage 4 after the drop, and what breaks if you drop too soon"); CustomerService stage4 = new CustomerService(jdbc, 4); long id = stage4.create("Annie Easley", "annie@example.test"); // Deploy 4b: drop the old column, live, with Stage 4 already the only code running. TestSupport.migrateTo(db, "latest"); Customer afterDrop = stage4.findById(id).orElseThrow(); t.section("Stage 4 read, after V3 dropped the email column"); t.line(afterDrop.toString()); assertThat(afterDrop.email()).isEqualTo("annie@example.test"); long postDropId = stage4.create("Mary Allen Wilkes", "mary@example.test"); Customer postDrop = stage4.findById(postDropId).orElseThrow(); t.section("Stage 4 create + read, entirely after the drop"); t.line(postDrop.toString()); assertThat(postDrop.email()).isEqualTo("mary@example.test"); t.write(); } @Test void stage1CodeFailsLoudlyIfItIsStillRunningAfterTheColumnIsDropped(@TempDir Path tmp) throws Exception { Path db = tmp.resolve("contract-too-soon"); TestSupport.migrateTo(db, "latest"); JdbcClient jdbc = TestSupport.jdbcClient(db); Transcript t = Transcript.start("10-drop-too-soon", "What a lingering Stage 1 instance sees if the drop runs before it is retired"); CustomerService stage1 = new CustomerService(jdbc, 1); Throwable thrown = catchThrowable(() -> stage1.create("Too Late", "too.late@example.test")); Throwable root = thrown; while (root.getCause() != null) { root = root.getCause(); } t.line("Stage 1 create() after V3 dropped \"email\": " + thrown.getClass().getName()); t.line("message: " + thrown.getMessage()); t.line("root cause: " + root.getClass().getName() + ": " + root.getMessage()); assertThat(root.getMessage()).contains("EMAIL").containsIgnoringCase("not found"); t.write(); } private static Throwable catchThrowable(org.assertj.core.api.ThrowableAssert.ThrowingCallable callable) { try { callable.call(); return new AssertionError("expected an exception but none was thrown"); } catch (Throwable t) { return t; } } }