使用带有liquibase changeset context属性的spring boot配置文件来管理changset范围

fra*_*ini 2 postgresql liquibase spring-boot liquibase-hibernate

我试图用spring boot和liquibase做一个概念验证应用程序.我基本上想要创建一个可以通过使用名为context的changeset属性管理liquibase更改集的spring boot应用程序,这样没有上下文的changeset可以应用于任何spring引导配置文件,而具有特定上下文的changeset(例如context ="dev")仅当该类型的弹簧引导配置文件处于活动状态时才会应用(例如spring.profiles.active = dev).

在我的应用程序中,我有以下弹簧配置文件 - > dev,prod(每个在应用程序yaml文件中正确指定,并在配置文件下指定了相关配置文件数据库凭据).我需要做些什么来完成这项工作.下面是我的application.yaml

spring:
  application:
    name: liquibase-spring-jpa-postgres-example
liquibase:
  change-log: db.changelog/db.changelog-master.xml

spring:
  profiles: dev
  datasource:
    url: jdbc:postgresql://localhost:5432/dev
    username: postgres
    password: password
    driver-class-name: org.postgresql.Driver

spring:
  profiles: ci
  datasource:
      url: jdbc:postgresql://localhost:5432/ci
      username: postgres
      password: password
      driver-class-name: org.postgresql.Driver

spring:
  profiles: qa
  datasource:
      url: jdbc:postgresql://localhost:5432/qa
      username: postgres
      password: password
      driver-class-name: org.postgresql.Driver

spring:
  profiles: production
  datasource:
      url: jdbc:postgresql://localhost:5432/prod
      username: postgres
      password: password
      driver-class-name: org.postgresql.Driver
Run Code Online (Sandbox Code Playgroud)

以下是当前的databaseChangeLog文件(如果我的弹簧配置文件是prod,则第二个更改集不应该运行).

<changeSet id="20161016_my_first_change" author="fike" context="dev, qa, ci, production">
    <sql>
        CREATE TABLE customer
        (
        id BIGSERIAL PRIMARY KEY,
        firstname character varying NOT NULL,
        lastname character varying NOT NULL
        );
    </sql>
    <rollback>
        drop table customer;
    </rollback>
</changeSet>

<changeSet id="20161016_my_first_change2" author="krudland" context="dev">
    <sql>
        insert into customer (firstname, lastname) values ('Franklin','Ike');
    </sql>
    <rollback>
        delete from customer where firstname = 'Franklin' and lastname = 'Ike';
    </rollback>
</changeSet>
Run Code Online (Sandbox Code Playgroud)

我基本上需要能够使用我的弹簧配置文件来管理我的liquibase上下文.这可能吗?

小智 11

您需要在您的yaml文件中定义'liquibase.contexts'属性.像下面的东西.

spring:
  profiles: dev
  datasource:
    url: jdbc:postgresql://localhost:5432/dev
    username: postgres
    password: password
    driver-class-name: org.postgresql.Driver
liquibase:
   contexts: dev
Run Code Online (Sandbox Code Playgroud)

添加此项后,以下更改集将仅在您的本地配置文件为"dev"时执行(即spring-boot:run -Dspring.profiles.active = dev)

<changeSet id="20161016_my_first_change2" author="krudland" context="dev">
    <sql>
        insert into customer (firstname, lastname) values ('Franklin','Ike');
    </sql>
    <rollback>
        delete from customer where firstname = 'Franklin' and lastname = 'Ike';
    </rollback>
</changeSet>
Run Code Online (Sandbox Code Playgroud)

  • 在 Spring Boot 2.0 中,liquibase 属性已被弃用并移至 spring 根目录下:`spring.liquibase.contexts` (17认同)

bla*_*ekp 10

更好的方法是在 liquibase 上下文设置中引用 spring 配置文件,从 spring boot 版本 2 开始,您只需添加到 main 中application.yml或者application.properties是:

spring.liquibase.contexts=${spring.profiles.active}
Run Code Online (Sandbox Code Playgroud)

您的活动 spring 配置文件将用作活动 liquibase 上下文。