From 7cf849269f46654d50292f15526f53d344a592b7 Mon Sep 17 00:00:00 2001 From: hsiegeln <37154749+hsiegeln@users.noreply.github.com> Date: Tue, 31 Mar 2026 18:18:09 +0200 Subject: [PATCH] fix: add @Primary PG DataSource/JdbcTemplate to prevent CH bean conflict When clickhouse.enabled=true, the ClickHouse JdbcTemplate bean prevents Spring Boot auto-config from creating the default PG JdbcTemplate. All PG repositories then get the CH JdbcTemplate and fail with "Table cameleer.audit_log does not exist". Fix: explicitly create @Primary DataSource and JdbcTemplate from DataSourceProperties so PG remains the default for unqualified injections. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../server/app/config/ClickHouseConfig.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/config/ClickHouseConfig.java b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/config/ClickHouseConfig.java index 413102df..663d160f 100644 --- a/cameleer3-server-app/src/main/java/com/cameleer3/server/app/config/ClickHouseConfig.java +++ b/cameleer3-server-app/src/main/java/com/cameleer3/server/app/config/ClickHouseConfig.java @@ -3,9 +3,11 @@ package com.cameleer3.server.app.config; import com.zaxxer.hikari.HikariDataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @@ -15,6 +17,22 @@ import javax.sql.DataSource; @ConditionalOnProperty(name = "clickhouse.enabled", havingValue = "true") public class ClickHouseConfig { + /** + * Explicit primary PG DataSource. Required because adding a second DataSource + * (ClickHouse) prevents Spring Boot auto-configuration from creating the default one. + */ + @Bean + @Primary + public DataSource dataSource(DataSourceProperties properties) { + return properties.initializeDataSourceBuilder().build(); + } + + @Bean + @Primary + public JdbcTemplate jdbcTemplate(DataSourceProperties properties) { + return new JdbcTemplate(dataSource(properties)); + } + @Bean(name = "clickHouseDataSource") public DataSource clickHouseDataSource(ClickHouseProperties props) { HikariDataSource ds = new HikariDataSource();