带有Spring Boot和多个模式的Liquibase,如何指定执行顺序

我们有一个数据迁移作业,它需要按顺序初始化模式AB。我们通过定义多个模式来处理多个模式SpringLiquibase,每个模式一个,每个模式都有自己的数据源和自己的主变更集。(注意,通常在 Spring Boot 中,您不需要定义 SpringLiquibase,因为它会检测单个数据源并使用该数据源为您自动配置 SpringLiquibase。)

执行顺序似乎有所不同,具体取决于作业是在 IDE 中本地运行,还是捆绑为单个 JAR Spring Boot 应用程序。

我们如何确保 liquibase 的两次执行按照我们想要的顺序发生?

(为什么顺序很重要:A包含一些表,而B包含引用表的视图。我们在尝试之前A必须确保我们,否则由于权限不足导致 B 的创建失败。)grant select on A.* to Bcreate view B.some_view (...) as select ... from A.xyz


长风秋雁
浏览 518回答 2
2回答

慕虎7371278

经过一番摸索并深入研究源代码后,事实证明它非常简单。SpringLiquibase 在方法中实现InitializingBean并执行 Liquibase 更新InitializingBean.afterPropertiesSet()。Spring 在完成每个 bean 的初始化后,对每个 bean 一个接一个地调用此方法。所以要强制一个特定的顺序,你需要强制在 Spring 上下文中定义 bean 的顺序。最简单的方法是使用@DependsOn注释。所以我们设置了类似的东西:@Beanpublic 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}

HUX布斯

这不适用于 Spring Boot,但如果您通过更改日志管理迁移,则此解决方法将有所帮助。这假设您有不同模式的不同数据源。<bean id="liquibase1" class="liquibase.integration.spring.SpringLiquibase">&nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSource1" />&nbsp; &nbsp; &nbsp; <property name="changeLog" value="classpath:db1-changelog1.xml" />&nbsp;</bean>&nbsp;<bean id="liquibase2" depends-on="liquibase1" class="liquibase.integration.spring.SpringLiquibase">&nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSource2" />&nbsp; &nbsp; &nbsp; <property name="changeLog" value="classpath:db2-changelog1.xml" />&nbsp;</bean><bean id="liquibase3" depends-on="liquibase2"&nbsp; class="liquibase.integration.spring.SpringLiquibase">&nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSource1" />&nbsp; &nbsp; &nbsp; <property name="changeLog" value="classpath:db1-changelog2.xml" />&nbsp;</bean><bean id="liquibase4" depends-on="liquibase3"&nbsp; class="liquibase.integration.spring.SpringLiquibase">&nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSource2" />&nbsp; &nbsp; &nbsp; <property name="changeLog" value="classpath:db2-changelog2.xml" />&nbsp;</bean>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java