66import java .time .ZoneOffset ;
77import java .util .List ;
88import java .util .Map ;
9+ import java .util .Set ;
10+ import java .util .HashSet ;
11+ import java .util .Locale ;
912
1013/** Portable optimistic Business ID allocator backed by a TeaQLDatabase. */
1114public final class JdbcBusinessIdAllocator
@@ -31,11 +34,66 @@ public JdbcBusinessIdAllocator(TeaQLDatabase database, String table) {
3134 /** Explicit development/test schema operation; construction never executes DDL. */
3235 @ Override
3336 public void ensureSchema (UserContext context ) {
34- database .execute (context , "CREATE TABLE IF NOT EXISTS " + table + " ("
35- + "scope_key VARCHAR(512) PRIMARY KEY, "
36- + "current_value BIGINT NOT NULL, "
37- + "version BIGINT NOT NULL, "
38- + "updated_at BIGINT NOT NULL)" );
37+ List <Map <String , Object >> existing = inspectColumns ();
38+ if (!existing .isEmpty ()) {
39+ requireColumns (existing );
40+ return ;
41+ }
42+ try {
43+ database .execute (context , "CREATE TABLE " + table + " ("
44+ + "scope_key VARCHAR(512) PRIMARY KEY, "
45+ + "current_value BIGINT NOT NULL, "
46+ + "version BIGINT NOT NULL, "
47+ + "updated_at BIGINT NOT NULL)" );
48+ } catch (RuntimeException createFailure ) {
49+ // Another instance may have created the table after inspection.
50+ // Only accept that race when the installed table can be inspected.
51+ try {
52+ List <Map <String , Object >> concurrent = inspectColumns ();
53+ if (!concurrent .isEmpty ()) {
54+ requireColumns (concurrent );
55+ return ;
56+ }
57+ } catch (RuntimeException inspectFailure ) {
58+ createFailure .addSuppressed (inspectFailure );
59+ }
60+ throw new IllegalStateException (
61+ "Cannot create Business ID table " + table , createFailure );
62+ }
63+ requireColumns (inspectColumns ());
64+ }
65+
66+ private List <Map <String , Object >> inspectColumns () {
67+ try {
68+ List <Map <String , Object >> columns = database .getTableColumns (table );
69+ if (columns == null ) {
70+ throw new IllegalStateException ("Column inspection returned null for " + table );
71+ }
72+ return columns ;
73+ } catch (RuntimeException failure ) {
74+ throw new IllegalStateException (
75+ "Cannot inspect Business ID table " + table
76+ + "; the database adapter must provide column metadata" ,
77+ failure );
78+ }
79+ }
80+
81+ private void requireColumns (List <Map <String , Object >> columns ) {
82+ Set <String > found = new HashSet <>();
83+ for (Map <String , Object > column : columns ) {
84+ for (Map .Entry <String , Object > entry : column .entrySet ()) {
85+ if ("column_name" .equalsIgnoreCase (entry .getKey ()) && entry .getValue () != null ) {
86+ found .add (String .valueOf (entry .getValue ()).toLowerCase (Locale .ROOT ));
87+ }
88+ }
89+ }
90+ Set <String > missing = new java .util .TreeSet <>(Set .of (
91+ "scope_key" , "current_value" , "version" , "updated_at" ));
92+ missing .removeAll (found );
93+ if (!missing .isEmpty ()) {
94+ throw new IllegalStateException (
95+ "Business ID table " + table + " is missing required columns " + missing );
96+ }
3997 }
4098
4199 @ Override
0 commit comments