Skip to content

Commit d3b642b

Browse files
aksOpsclaude
andcommitted
fix: DROP and recreate H2 tables on cache version mismatch
CREATE TABLE IF NOT EXISTS doesn't add new columns to existing tables. When the schema changes (new status/detection_method/file_type/snippet columns), old H2 databases kept the old schema and MERGE INTO failed with "Column STATUS not found". Fix: on version mismatch, DROP all tables and recreate from SCHEMA_SQL instead of just deleting rows. This ensures new columns are always present after a schema upgrade. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1ef97f3 commit d3b642b

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/main/java/io/github/randomcodespace/iq/cache/AnalysisCache.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,24 @@ private void checkCacheVersion() throws SQLException {
154154
}
155155
if (!String.valueOf(CACHE_VERSION).equals(storedVersion)) {
156156
if (storedVersion != null) {
157-
log.info("Cache version mismatch (stored={}, current={}) — clearing stale cache",
157+
log.info("Cache version mismatch (stored={}, current={}) — dropping and recreating tables",
158158
storedVersion, CACHE_VERSION);
159-
try (var stmt = conn.createStatement()) {
160-
stmt.execute("DELETE FROM edges");
161-
stmt.execute("DELETE FROM nodes");
162-
stmt.execute("DELETE FROM files");
159+
}
160+
// DROP and recreate tables to pick up schema changes (new columns, types, etc.)
161+
// CREATE TABLE IF NOT EXISTS doesn't add columns to existing tables.
162+
try (var stmt = conn.createStatement()) {
163+
stmt.execute("DROP TABLE IF EXISTS edges");
164+
stmt.execute("DROP TABLE IF EXISTS nodes");
165+
stmt.execute("DROP TABLE IF EXISTS files");
166+
stmt.execute("DROP TABLE IF EXISTS analysis_runs");
167+
}
168+
// Recreate with current schema
169+
for (String sql : SCHEMA_SQL.split(";")) {
170+
String trimmed = sql.trim();
171+
if (!trimmed.isEmpty() && !trimmed.toUpperCase().startsWith("CREATE TABLE IF NOT EXISTS CACHE_META")) {
172+
try (var stmt = conn.createStatement()) {
173+
stmt.execute(trimmed);
174+
}
163175
}
164176
}
165177
try (var stmt = conn.createStatement()) {

0 commit comments

Comments
 (0)