Skip to content

Commit bb3d5b4

Browse files
author
Philip Z
committed
Preserve Business ID shape diagnostic after creation race
1 parent f00f893 commit bb3d5b4

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎teaql-business-id-jdbc/src/main/java/io/teaql/businessid/jdbc/JdbcBusinessIdAllocator.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ public void ensureSchema(UserContext context) {
5858
+ "updated_at " + dialect.mapColumnType("BIGINT") + " NOT NULL)");
5959
} catch (RuntimeException createFailure) {
6060
// Another instance may have created the table after inspection.
61-
// Only accept that race when the installed table can be inspected.
61+
// Only accept that race when the installed table passes the same
62+
// validation as a table that was already present at startup.
63+
List<Map<String, Object>> concurrent;
6264
try {
63-
List<Map<String, Object>> concurrent = inspectColumns();
64-
if (!concurrent.isEmpty()) {
65-
requireColumns(concurrent);
66-
return;
67-
}
65+
concurrent = inspectColumns();
6866
} catch (RuntimeException inspectFailure) {
6967
createFailure.addSuppressed(inspectFailure);
68+
throw new IllegalStateException(
69+
"Cannot create or inspect Business ID table " + table,
70+
createFailure);
71+
}
72+
if (!concurrent.isEmpty()) {
73+
requireColumns(concurrent);
74+
return;
7075
}
7176
throw new IllegalStateException(
7277
"Cannot create Business ID table " + table, createFailure);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.teaql.businessid.jdbc;
2+
3+
import io.teaql.core.sql.portable.TeaQLDatabase;
4+
import java.lang.reflect.Proxy;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
public class JdbcBusinessIdAllocatorTest {
12+
13+
@Test
14+
public void concurrentWrongShapeTableReportsItsMissingColumns() {
15+
AtomicInteger inspections = new AtomicInteger();
16+
TeaQLDatabase database = (TeaQLDatabase) Proxy.newProxyInstance(
17+
TeaQLDatabase.class.getClassLoader(),
18+
new Class<?>[] {TeaQLDatabase.class},
19+
(proxy, method, args) -> {
20+
if (method.getName().equals("getTableColumns")) {
21+
return inspections.incrementAndGet() == 1
22+
? List.of()
23+
: List.of(Map.of("column_name", "scope_key"));
24+
}
25+
if (method.getName().equals("execute")) {
26+
throw new IllegalStateException("table already exists");
27+
}
28+
throw new AssertionError("Unexpected database call " + method.getName());
29+
});
30+
31+
IllegalStateException failure = Assert.assertThrows(
32+
IllegalStateException.class,
33+
() -> new JdbcBusinessIdAllocator(database).ensureSchema(null));
34+
Assert.assertTrue(failure.getMessage(),
35+
failure.getMessage().contains("missing required columns"));
36+
Assert.assertEquals(2, inspections.get());
37+
}
38+
}

0 commit comments

Comments
 (0)