Revert to JdbcTemplate for schema init, keep comment-stripping fix
The DriverManager-based approach likely failed because the ClickHouse JDBC driver wasn't registered with DriverManager. The original JdbcTemplate approach worked for route_diagrams and agent_metrics — only route_executions was skipped due to the comment-parsing bug. Reverts to simple JdbcTemplate-based init with unqualified table names (DataSource targets cameleer3 database). The CLICKHOUSE_DB env var on the ClickHouse container handles database creation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package com.cameleer3.server.app.config;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -11,9 +10,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.Statement;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* ClickHouse configuration.
|
||||
@@ -21,9 +18,8 @@ import java.sql.Statement;
|
||||
* Spring Boot auto-configures the DataSource from {@code spring.datasource.*} properties.
|
||||
* This class exposes a JdbcTemplate bean and initializes the schema on startup.
|
||||
* <p>
|
||||
* Schema initialization uses a direct JDBC connection (bypassing the DataSource)
|
||||
* to avoid chicken-and-egg problems: the DataSource targets a specific database
|
||||
* that may not exist yet on a fresh ClickHouse instance.
|
||||
* The ClickHouse container's {@code CLICKHOUSE_DB} env var creates the database;
|
||||
* this class creates the tables within it.
|
||||
*/
|
||||
@Configuration
|
||||
public class ClickHouseConfig {
|
||||
@@ -33,15 +29,6 @@ public class ClickHouseConfig {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
@Value("${spring.datasource.url}")
|
||||
private String datasourceUrl;
|
||||
|
||||
@Value("${spring.datasource.username}")
|
||||
private String datasourceUsername;
|
||||
|
||||
@Value("${spring.datasource.password}")
|
||||
private String datasourcePassword;
|
||||
|
||||
public ClickHouseConfig(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
@@ -53,42 +40,26 @@ public class ClickHouseConfig {
|
||||
|
||||
@PostConstruct
|
||||
void initSchema() {
|
||||
String dbName = extractDatabaseName(datasourceUrl);
|
||||
String rootUrl = datasourceUrl.replaceFirst("/[^/?]+($|\\?)", "/$1");
|
||||
|
||||
try (Connection conn = DriverManager.getConnection(rootUrl, datasourceUsername, datasourcePassword);
|
||||
Statement stmt = conn.createStatement()) {
|
||||
|
||||
// Create the database first
|
||||
stmt.execute("CREATE DATABASE IF NOT EXISTS " + dbName);
|
||||
log.info("Ensured database '{}' exists", dbName);
|
||||
|
||||
// Apply schema files using fully qualified table names
|
||||
for (String schemaFile : SCHEMA_FILES) {
|
||||
var jdbc = new JdbcTemplate(dataSource);
|
||||
for (String schemaFile : SCHEMA_FILES) {
|
||||
try {
|
||||
String sql = new ClassPathResource(schemaFile).getContentAsString(StandardCharsets.UTF_8);
|
||||
// Strip comment lines before splitting into statements
|
||||
// Strip comment lines before splitting — a statement preceded by
|
||||
// comment lines would otherwise be skipped entirely.
|
||||
String stripped = sql.lines()
|
||||
.filter(line -> !line.trim().startsWith("--"))
|
||||
.collect(java.util.stream.Collectors.joining("\n"));
|
||||
.collect(Collectors.joining("\n"));
|
||||
for (String statement : stripped.split(";")) {
|
||||
String trimmed = statement.trim();
|
||||
if (!trimmed.isEmpty()) {
|
||||
stmt.execute(trimmed);
|
||||
jdbc.execute(trimmed);
|
||||
}
|
||||
}
|
||||
log.info("Applied schema: {}", schemaFile);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to apply schema: {}", schemaFile, e);
|
||||
throw new RuntimeException("Schema initialization failed: " + schemaFile, e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("ClickHouse schema initialization failed", e);
|
||||
throw new RuntimeException("ClickHouse schema initialization failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
static String extractDatabaseName(String jdbcUrl) {
|
||||
// jdbc:ch://host:port/dbname or jdbc:ch://host:port/dbname?params
|
||||
String afterScheme = jdbcUrl.substring(jdbcUrl.indexOf("://") + 3);
|
||||
String afterHost = afterScheme.substring(afterScheme.indexOf('/') + 1);
|
||||
int paramIdx = afterHost.indexOf('?');
|
||||
return paramIdx >= 0 ? afterHost.substring(0, paramIdx) : afterHost;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user