Springboot外部库Liquibase迁移

ttcibm8c  于 2022-11-29  发布在  Spring
关注(0)|答案(1)|浏览(138)

我有一个外部SpringBoot公共库(构建为jar)和一个包含它的主项目。
外部库需要一组默认的数据库表。为此,我设置了Liquibase。主项目也使我们使用Liquibase。两个项目有不同的更改日志文件。假设一个命名为library.xml,另一个命名为main.xml。所有数据库和Liquibase属性都在主项目中配置(以及spring.liquibase.change-log=main.xml)。
该库只有一个属性:spring.liquibase.change-log=library.xml .
我如何告诉Liquibase“运行library.xmlmain.xml“?我想我需要在库项目中配置一些东西,因为主应用程序应该按原样使用它(简单地导入和使用它)。

xxe27gdn

xxe27gdn1#

要组合/构建我们的(liquibase)变更日志,我们可以:
1.使用(liquibaseinclude "attribute" (et al.)。(我还是更喜欢这个!)

Spring Boot(2.7.5)解决方案:

(main项目)application.properties

spring.liquibase.change-log=classpath:/main.xml
# many more with same prefix ...
lib.liquibase.change-log=classpath:/library.xml
# according for lib.liquibase.* ...

主/配置类:

@SpringBootApplication(/* scanBasePackages = "com.example.lib" */)
// We need/want these:
@EnableConfigurationProperties(LiquibaseProperties.class)
public class Demo {

  @Bean // "main" liquibase: 
  // when order matters: @DependsOn("liquibaseLib") 
  @ConfigurationProperties("spring.liquibase") // !
  public SpringLiquibase liquibase( 
      ObjectProvider<DataSource> dataSource,
      @LiquibaseDataSource ObjectProvider<DataSource> liquibaseDataSource,
      LiquibaseProperties properties) {
    return liqui(dataSource, liquibaseDataSource, properties);
  }

  @Bean // lib liquibase:
  @ConfigurationProperties("lib.liquibase")
  public SpringLiquibase liquibaseLib(
      ObjectProvider<DataSource> dataSource,
      /* alternatively: @MyVeryCustomDataSource ... */
      @LiquibaseDataSource ObjectProvider<DataSource> liquibaseDataSource,
      LiquibaseProperties properties) {
    return liqui(dataSource, liquibaseDataSource, properties);
  }

  // pstvm...

  private SpringLiquibase liqui(ObjectProvider<DataSource> dataSource,
      ObjectProvider<DataSource> liquibaseDataSource, LiquibaseProperties properties) {
    // trick: Don't use LiquibaseConfiguration as `@Configuration/Bean`
    // , it is not "repeatable" (in regards of liquibase execution;(
    // , but rather as a "utility object"/factory:
    LiquibaseConfiguration helpr = new LiquibaseConfiguration(properties);
    return helpr.liquibase(dataSource, liquibaseDataSource);
  }
}

www.example.com的标题(包+导入)Demo.java:
package com.example.app;import javax.sql.DataSource; import liquibase.integration.spring.SpringLiquibase; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration.LiquibaseConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSource; import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.DependsOn;
See Also.

简单解决方案

...如2所示:

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import javax.sql.DataSource;

@Configuration // scanned/imported/used by "main" !
public class MultipleLiquiaseConfiguration {

    @Bean
    public SpringLiquibase liquibaseLib(DataSource dataSource) { // !
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:library.xml"); // !
        return liquibase;
    }

    @Bean
    // when order matters: @DependsOn("liquibaseLib")
    public SpringLiquibase liquibaseMain(DataSource dataSource) { // !
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:main.xml"); // !
        return liquibase;
    }
}

关于:
该库只有一个属性...
我 * 希望 * 这是一个 * 测试 * 属性;)!

相关问题