首页 > 解决方案 > 有没有一种简单的方法来隔离部署时 Liquibase 执行的上下文?

问题描述

我正在为将使用 Spring Boot 的新服务定义架构。我正在寻找一种方法来配置 Liquibase 部署以仅在某个配置文件处于活动状态时执行。如果可能,我还想禁用所有其他组件,以避免在数据库部署完成之前来自服务逻辑的操作。

我已经成功启动了我的服务,并且看到我的变更集执行了。除了对所有组件进行极端配置文件操作之外,我不确定其他方法可以使我的 Liquibase 配置文件成为独立执行,从而避免在应用程序上下文中实例化组件。

这是我的配置:

spring:
  application:
    name: liquibase-spring-postgres-example
  liquibase:
    enabled: false
---
spring:
  profiles: dev
  datasource:
    username: ${service_user}
    password: ${DATASOURCE_DB_PASS}
    url: jdbc:postgresql://{db}:{port}/schema
  liquibase:
    url: jdbc:postgresql://{db}:{port}/schema
---
spring:
  profiles: liquibase
  datasource:
    ## provided to get spring boot to start up
    url: jdbc:postgresql://{db}:{port}/schema
  liquibase:
    user: ${liquibase_user}
    password: ${LIQUIBASE_DB_PASS}
    contexts: release
    enabled: true

我已经尝试在我在这里找到的评论的帮助下成功禁用嵌入式码头服务器之类的东西:

spring:
...
  main:
    web-application-type: none

除了所有 bean 的极端配置文件定义之外,是否还有其他方法,或者在 Spring Boot 之外手动运行 Liquibase 以实现这一点?

标签: spring-bootdeploymentliquibase

解决方案


要针对 spring-boot jar 文件执行脚本,这样的内容应该可以帮助您:

java -cp /path/to/spring-boot-fat.jar \
    -Dloader.system=true \
    -Dloader.main=liquibase.integration.commandline.Main \
    org.springframework.boot.loader.PropertiesLauncher \
    --changeLogFile=db/changelog/db-changelog-master.xml \
    --driver=org.h2.Driver \
    --url="jdbc:h2:~/h2db/liquibase-test;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" \
    --password=sa \
    --username=sa \
    updateSQL

编辑:这是针对 spring-boot 1.5.x 测试的,但我想它也应该针对 2.x 工作。


推荐阅读