diff --git a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java index 15fc741..8d4bcdc 100644 --- a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java +++ b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrationLockWaitIT.java @@ -3,6 +3,7 @@ package dev.caskeleton.grpc.ledger; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; +import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -13,9 +14,10 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.params.provider.MethodSource; import org.postgresql.ds.PGSimpleDataSource; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -31,6 +33,13 @@ import org.testcontainers.postgresql.PostgreSQLContainer; * Flyway runs it, in one transaction. Unbounded it waits until the writer leaves, with every claim * queued behind it, and then applies; bounded it fails with {@code lock_not_available} first and * its rollback leaves the ledger as it was. + * + *

Every migration after V001 is held to this, read from the tree itself ({@link + * GrpcOperationLedgerMigrations#inOrder()}), so a V004 is checked without anyone naming it here. + * This is the ledger tree's lock-wait rule: the optional build cannot reach the persistence-jpa + * testkit whose text check the other trees run, and a copy of that check would drift (re-review + * 2026-11-03 round 3, RR3-02-04). A later migration that does not touch the ledger at all fails + * here too, on purpose: say so in this test rather than let the rule pass it unseen. */ @Testcontainers(disabledWithoutDocker = true) class GrpcOperationLedgerMigrationLockWaitIT { @@ -47,12 +56,13 @@ class GrpcOperationLedgerMigrationLockWaitIT { .withUsername("grpc") .withPassword("grpc"); + /** Every ledger migration after V001, the one that creates the ledger. */ + static Stream migrationsAfterTheFirst() throws IOException { + return GrpcOperationLedgerMigrations.inOrder().stream().skip(1); + } + @ParameterizedTest - @ValueSource( - strings = { - "V002__index_grpc_operation_ledger_finished_rows.sql", - "V003__add_grpc_operation_ledger_claim_attempt.sql" - }) + @MethodSource("migrationsAfterTheFirst") @DisplayName("ledger에 쓴 트랜잭션이 열려 있으면 ledger를 잠그는 마이그레이션은 상한 안에 실패하고 아무것도 바꾸지 않는다") void aLedgerMigrationGivesUpOnAnOpenWriterAndChangesNothing(String migration) throws Exception { PGSimpleDataSource dataSource = dataSource(); @@ -73,6 +83,10 @@ class GrpcOperationLedgerMigrationLockWaitIT { String.valueOf(attempt.sqlState()), attempt.took().compareTo(GIVES_UP_WITHIN) < 0, schema(statement))) + .as( + "%s must put SET LOCAL lock_timeout = '5s'; before it locks the ledger" + + " (docs/jpa/migration-guide.md, \"Bound the lock wait\")", + migration) .containsExactly(LOCK_NOT_AVAILABLE, true, schemaBefore); } } diff --git a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java index 749302b..78aeb65 100644 --- a/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java +++ b/grpc/grpc-operation-ledger-jpa/src/integrationTest/java/dev/caskeleton/grpc/ledger/GrpcOperationLedgerMigrations.java @@ -3,24 +3,44 @@ package dev.caskeleton.grpc.ledger; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; +import java.util.stream.Stream; /** * The ledger's Flyway scripts in version order, as one SQL text for a test database. * - *

One list for every PostgreSQL lane, so a new migration is applied by all of them or by none; - * two copies of the list drift the first time someone adds a V003 to one of them. + *

One list for every PostgreSQL lane, so a new migration is applied by all of them or by none. + * The list is the tree on disk, not a copy of it: a hand-kept list let a V004 that nobody added to + * it go unapplied and unchecked by every lane (re-review 2026-11-03 round 3, RR3-02-04). */ final class GrpcOperationLedgerMigrations { - private static final List IN_ORDER = - List.of( - "db/migration/grpc/V001__create_grpc_operation_ledger.sql", - "db/migration/grpc/V002__index_grpc_operation_ledger_finished_rows.sql", - "db/migration/grpc/V003__add_grpc_operation_ledger_claim_attempt.sql"); + /** The ledger's tree from this leaf's directory, where the lanes run. */ + private static final Path TREE = Path.of("src/main/resources/db/migration/grpc"); + + /** The same tree on the classpath, where the scripts are read from. */ + private static final String LOCATION = "db/migration/grpc/"; private GrpcOperationLedgerMigrations() {} + /** + * The file name of every versioned migration in the tree ({@code V__.sql}), in + * Flyway's order: by version, its parts compared as numbers. + */ + static List inOrder() throws IOException { + try (Stream files = Files.list(TREE)) { + return files + .map(file -> file.getFileName().toString()) + .filter(name -> name.matches("V\\d+(?:[._]\\d+)*__.+\\.sql")) + .sorted(Comparator.comparing(GrpcOperationLedgerMigrations::version, Arrays::compare)) + .toList(); + } + } + /** Every migration's SQL, concatenated in version order. */ static String all() throws IOException { return before(null); @@ -29,26 +49,31 @@ final class GrpcOperationLedgerMigrations { /** The SQL of every migration before the one named, concatenated in version order. */ static String before(String fileName) throws IOException { StringBuilder sql = new StringBuilder(); - for (String path : IN_ORDER) { - if (path.endsWith("/" + fileName)) { + for (String name : inOrder()) { + if (name.equals(fileName)) { break; } - sql.append(read(path)).append('\n'); + sql.append(read(name)).append('\n'); } return sql.toString(); } /** One migration's SQL, by its file name. */ static String named(String fileName) throws IOException { - for (String path : IN_ORDER) { - if (path.endsWith("/" + fileName)) { - return read(path); - } + if (!inOrder().contains(fileName)) { + throw new IOException("no gRPC operation-ledger migration is named " + fileName); } - throw new IOException("no gRPC operation-ledger migration is named " + fileName); + return read(fileName); } - private static String read(String path) throws IOException { + private static int[] version(String fileName) { + return Arrays.stream(fileName.substring(1, fileName.indexOf("__")).split("[._]")) + .mapToInt(Integer::parseInt) + .toArray(); + } + + private static String read(String fileName) throws IOException { + String path = LOCATION + fileName; try (InputStream stream = GrpcOperationLedgerMigrations.class.getClassLoader().getResourceAsStream(path)) { if (stream == null) {