Merge remote-tracking branch 'origin/claude/r9-g3-migration-7ce36d' into claude/integration-r9-f1bc5c

This commit is contained in:
DongHyeonka
2026-11-07 10:00:03 +09:00
@@ -2,6 +2,7 @@ package dev.caskeleton.grpc.ledger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import dev.caskeleton.adapter.outbound.persistence.testkit.migration.ConcurrentIndexMigrationInspector;
import dev.caskeleton.adapter.outbound.persistence.testkit.migration.MigrationResource;
@@ -12,6 +13,8 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
@@ -22,6 +25,7 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.testcontainers.junit.jupiter.Container;
@@ -87,6 +91,13 @@ class GrpcOperationLedgerMigrationLockWaitIT {
*/
static final Map<String, String> RUN_OUTSIDE_A_TRANSACTION = Map.of();
/** The file name the list check's fixture cases list. */
private static final String LISTED = "V004__ledger_index.sql";
/** A concurrent index build alone, as the guide asks such a file to be. */
private static final String CONCURRENT_BUILD =
"CREATE INDEX CONCURRENTLY ix_ledger_note ON grpc_operation_ledger (note);\n";
/** Every ledger migration after V001, the one that creates the ledger, that runs in one. */
static Stream<String> migrationsAfterTheFirst() throws IOException {
return GrpcOperationLedgerMigrations.inOrder().stream()
@@ -141,18 +152,94 @@ class GrpcOperationLedgerMigrationLockWaitIT {
void everyMigrationListedAsRunningOutsideATransactionIsOneConcurrentIndexBuildThatSaysSo()
throws IOException {
// given
ConcurrentIndexMigrationInspector inspector = new ConcurrentIndexMigrationInspector();
List<String> tree = GrpcOperationLedgerMigrations.inOrder();
Map<String, LedgerMigration> tree = new LinkedHashMap<>();
for (String migration : GrpcOperationLedgerMigrations.inOrder()) {
tree.put(
migration,
new LedgerMigration(
GrpcOperationLedgerMigrations.named(migration),
GrpcOperationLedgerMigrations.executeInTransaction(migration)));
}
// when
List<String> refused = refusalsOf(RUN_OUTSIDE_A_TRANSACTION.keySet(), tree);
// then
assertThat(refused).isEmpty();
}
@ParameterizedTest
@MethodSource("listedFilesTheGuideDoesNotAllow")
@DisplayName(
"예외 목록 검사는 트리에 없는 파일, 동시 인덱스 문장이 없는 파일, executeInTransaction=false가 없는 파일, 다른 문장과 섞인 파일을 이유와 함께 거부한다")
void theListCheckRefusesAFileTheGuideDoesNotAllow(
String listed, Map<String, LedgerMigration> tree, String reason) {
// when
List<String> refused = refusalsOf(List.of(listed), tree);
// then
assertThat(refused).singleElement().asString().contains(listed).contains(reason);
}
@Test
@DisplayName("예외 목록 검사는 executeInTransaction=false를 곁에 둔, 동시 인덱스 문장 하나뿐인 파일을 받는다")
void theListCheckAcceptsAConcurrentIndexBuildAloneThatSaysSo() {
// given
Map<String, LedgerMigration> tree =
Map.of(LISTED, new LedgerMigration(CONCURRENT_BUILD, false));
// when
List<String> refused = refusalsOf(List.of(LISTED), tree);
// then
assertThat(refused).isEmpty();
}
/** One listed file for each way the guide's rule can be broken, and the reason it is refused. */
static Stream<Arguments> listedFilesTheGuideDoesNotAllow() {
return Stream.of(
arguments(LISTED, Map.of(), "is not a migration in the ledger tree"),
arguments(
LISTED,
Map.of(
LISTED,
new LedgerMigration(
"ALTER TABLE grpc_operation_ledger ADD COLUMN note TEXT;\n", false)),
"holds no concurrent index statement"),
arguments(
LISTED,
Map.of(LISTED, new LedgerMigration(CONCURRENT_BUILD, true)),
"must set executeInTransaction=false"),
arguments(
LISTED,
Map.of(
LISTED,
new LedgerMigration("SET LOCAL lock_timeout = '5s';\n" + CONCURRENT_BUILD, false)),
"mixes a concurrent index statement with other statements"));
}
/**
* Why each migration listed as running outside a transaction may not: one reason per file
* refused, none when every listed file is in the tree and is a concurrent index statement alone
* in its file with {@code executeInTransaction=false} beside it ("Concurrent index builds"). The
* list test runs it over {@link #RUN_OUTSIDE_A_TRANSACTION} and the tree, and the fixture cases
* hold it to every refusal while that list is empty (re-review 2026-11-03 round 8, RR8-03-04: its
* loop never ran, so a broken check passed).
*
* @param listed the file names listed
* @param tree every migration in the tree, by file name
* @return the reasons, in the order the files are listed
*/
static List<String> refusalsOf(Collection<String> listed, Map<String, LedgerMigration> tree) {
ConcurrentIndexMigrationInspector inspector = new ConcurrentIndexMigrationInspector();
List<String> refused = new ArrayList<>();
for (String migration : RUN_OUTSIDE_A_TRANSACTION.keySet()) {
if (!tree.contains(migration)) {
for (String migration : listed) {
LedgerMigration file = tree.get(migration);
if (file == null) {
refused.add(migration + " is not a migration in the ledger tree");
continue;
}
MigrationResource resource =
new MigrationResource(migration, GrpcOperationLedgerMigrations.named(migration));
MigrationResource resource = new MigrationResource(migration, file.sql());
if (!inspector.containsConcurrentIndexStatement(resource.sql())) {
refused.add(
migration
@@ -160,20 +247,17 @@ class GrpcOperationLedgerMigrationLockWaitIT {
+ " outside a transaction");
continue;
}
boolean inATransaction = GrpcOperationLedgerMigrations.executeInTransaction(migration);
Throwable thrown =
catchThrowable(
() -> {
inspector.validate(resource, inATransaction);
inspector.validate(resource, file.executeInTransaction());
inspector.requireIsolatedStatement(resource);
});
if (thrown != null) {
refused.add(thrown.getMessage());
}
}
// then
assertThat(refused).isEmpty();
return refused;
}
/** Runs the migration in one transaction while another one holds a write to the ledger. */
@@ -239,6 +323,15 @@ class GrpcOperationLedgerMigrationLockWaitIT {
return dataSource;
}
/**
* A ledger migration as the list check reads it.
*
* @param sql the script
* @param executeInTransaction whether Flyway runs it in a transaction, as {@link
* GrpcOperationLedgerMigrations#executeInTransaction} reads it from the file beside it
*/
record LedgerMigration(String sql, boolean executeInTransaction) {}
/** What the migration did while the writer held the ledger. */
private record Attempt(Throwable thrown, Duration took) {