package com.dalab.discovery.config; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import liquibase.integration.spring.SpringLiquibase; import tech.jhipster.config.liquibase.SpringLiquibaseUtil; @Configuration @Profile("test") public class TestLiquibaseConfiguration { private final Logger log = LoggerFactory.getLogger(TestLiquibaseConfiguration.class); private final Environment env; public TestLiquibaseConfiguration(Environment env) { this.env = env; } @Bean public SpringLiquibase liquibase( LiquibaseProperties liquibaseProperties, // Directly inject the primary DataSource bean DataSource dataSource, DataSourceProperties dataSourceProperties) { log.info("Creating TestLiquibaseConfiguration for 'test' profile using explicit DataSource."); // For tests, we'll use the synchronous SpringLiquibaseUtil setup. // Pass null for liquibaseDataSource (the first DataSource argument to createSpringLiquibase) // to ensure it uses the explicitly provided primary dataSource (the third argument). SpringLiquibase liquibase = SpringLiquibaseUtil.createSpringLiquibase( null, // liquibaseDataSource: pass null to prioritize the general dataSource liquibaseProperties, dataSource, // dataSource: use the explicitly injected primary H2 DataSource dataSourceProperties ); // Explicitly set the changelog file, even if Liquibase is disabled for tests. // This is needed for the Liquibase bean to initialize correctly. // Use the same path as in the main CrawlerLiquibaseConfiguration. liquibase.setChangeLog("classpath:config/liquibase/master.xml"); // Set common properties. These are typically sourced from LiquibaseProperties, // which SpringLiquibaseUtil.createSpringLiquibase already handles. // We can explicitly set them if needed, but usually, it's not necessary here. // liquibase.setChangeLog("classpath:config/liquibase/master.xml"); // Default is usually fine // liquibase.setContexts(liquibaseProperties.getContexts()); // liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema()); // ... and other properties as needed // The most crucial part for tests, if spring.liquibase.enabled=false, // this ensures migrations don't run. SpringLiquibaseUtil should handle this // by honoring liquibaseProperties.isEnabled(). if (!liquibaseProperties.isEnabled()) { log.info("Liquibase is disabled for 'test' profile via spring.liquibase.enabled=false."); } liquibase.setShouldRun(liquibaseProperties.isEnabled()); log.debug("Configuring Liquibase for 'test' profile. ShouldRun: {}", liquibaseProperties.isEnabled()); return liquibase; } }