File size: 3,317 Bytes
442299c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
}