Search code examples
spring-bootliquibase

Can I use liquibase.properties alongside a Spring config?


I tried for ages to figure out a way to set preserveSchemaCase in Spring Boot's application.yaml, until someone pointed out that just a subset of the settings were available through Spring!

So, I tried moving some of the config out into liquibase.properties, but it does not do anything. Some searching led me to believe that the liquibase config file is not actually picked up when using Spring Boot. Is that so? Does that mean that setting these props not exposed by Spring is only achievable through code? As in System.setProperty("liquibase.preserveSchemaCase", "true").


Solution

  • When relying on Spring Boot's auto-configuration to configure the Liquibase bean, you can only get the properties offered by Spring which are, as you've mentioned, a subset of Liquibase's set of properties.

    Spring Boot's Liquibase bean configuration: https://github.com/spring-projects/spring-boot/blob/3d5cdb7715397db2b4b3260544624d0f919315b4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java#L98

    Spring Boot supported Liquibase properties (prefixed with spring.liquibase): https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties.java

    If you'd want to set additional properties, that are not offered by Spring, you could opt for creating a Liquibase bean manually: create a Configuration class and link liquibase.properties to this class via @PropertySource annotation and then declare a Liquibase bean in this class.

    @Configuration
    @PropertySource("classpath:liquibase.properties")
    public class LiquibaseConfig {
    
        @Value("${preserveSchemaCase}")
        private boolean preserveSchemaCase;
    
        @Bean
        public SpringLiquibase liquibase() {
            SpringLiquibase liquibase = new SpringLiquibase();
            liquibase.setChangeLog("classpath:db/changelog/db.changelog-master.xml");
            liquibase.setDataSource(dataSource()); // Set your data source here
    
            // ... set other properties 
            
            liquibase.setChangeLogParameters(Collections.singletonMap("preserveSchemaCase", preserveSchemaCase));
    
            return liquibase;
        }
    }
    

    For the Spring offered Liquibase properties, you'd either want to move them all into liquibase.properties or you could get them from application.yml by providing Environment bean to this LiquibaseConfig configuration class. Either way, you need to call the appropriate setter on the liquibase bean manually for each property.