Liquibase with Spring Boot and multiple schemas, how to specify execution order

Andrew Spencer :

We have an data-migration job which needs to initialize schemas A and B, in that order. We handle multiple schemas by defining multiple SpringLiquibase, one for each schema, each with its own datasource and its own master changeset. (Note, normally in Spring Boot you wouldn't need to define a SpringLiquibase, because it would detect a single datasource and auto-configure the SpringLiquibase for you with that datasource.)

The execution order seems to vary depending whether the job is run locally within the IDE, or bundled as a single-JAR Spring Boot app.

How can we ensure that the two executions of liquibase happen in the order we want?

(Why the order is important: A contains some tables, while B contains views that reference tables in A. We have to make sure that we grant select on A.* to B before attempting to create view B.some_view (...) as select ... from A.xyz, otherwise the creation of B fails due to insufficient privileges.)

Andrew Spencer :

After some scratching of heads and digging into the source code, it turns out to be extremely simple.

SpringLiquibase implements InitializingBean and executes the Liquibase update within the InitializingBean.afterPropertiesSet() method.

Spring calls this method on each bean, one by one, after finishing initializing each one.

So to force a particular order, you need to force the order in which the beans are defined in the Spring context. And the easiest way to do this is with the @DependsOn annotation.

So we put in place something like:

@Bean
public SpringLiquibase liquibaseA(
    @Qualifier("dataSourceA") DataSource dataSource,
    @Qualifier("liquibasePropertiesA") LiquibaseProperties liquibaseProperties
) {
    return instantiateSpringLiquibase(dataSource, liquibaseProperties); 
}

@Bean
@DependsOn("liquibaseA")
public SpringLiquibase liquibaseB(
    @Qualifier("dataSourceB") DataSource dataSource,
    @Qualifier("liquibasePropertiesB") LiquibaseProperties liquibaseProperties
) {
    return instantiateSpringLiquibase(dataSource, liquibaseProperties); 
}

private SpringLiquibase instantiateSpringLiquibase(DataSource dataSource, LiquibaseProperties liquibaseProperties) {
    // set the datasource from dataSource and everything else from liquibaseProperties
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to specify multiple labels for a Liquibase changeset?

Tests not working for multiple datasource with spring boot liquibase

Liquibase multiple changelog execution

Spring Boot: How to use multiple schemas and dynamically choose which one to use at runtime

How to modify the default execution order of sql script in liquibase?

Spring Boot: How to specify the PasswordEncoder?

Entity manager error in spring-boot application with multiple schemas/databases

Spring Boot - How to achieve this execution?

JUnit5: Specify order of execution of multiple @Nested classes

Spring Boot & Liquibase by Example

How to specify sort order on multiple self joins

How to specify multiple "where" or "order_by" conditions?

How to use ORDER BY in Spring Boot?

How to specify prefix for all controllers in Spring Boot?

How do I specify a BeanNamingStrategy with Spring Boot?

How to specify the Launcher in Spring Boot Gradle?

How to specify a fallback request mapping in Spring Boot

Spring Boot - How to specify an alternate start-class? (Multiple Entry Points)

How to specify logging.config when multiple Spring Boot Apps are deployed on tomcat

Correct Execution Order with two Datasources for Flyway Migration Spring Boot

Liquibase execution order of changes inside of changeSet

How to divide Liquibase package structure for dev and prod environment in Spring Boot?

How can I configure Maven Liquibase plugin in Spring Boot?

How to alter databasechangelog.filename for Spring Boot and Liquibase?

How can liquibase banner be turned off in spring boot?

How to generate a liquibase rollback script using spring boot?

How to ensure atomicity of a method execution in spring boot?

Liquibase ,Terraform and Spring Boot question

Spring-boot liquibase integration