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

时间:2016-10-17 14:21:44

标签: postgresql spring-boot liquibase liquibase-hibernate

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

在我的应用中,我有以下弹簧配置文件 - > dev,prod(每个在应用程序yaml文件中正确指定,并在配置文件下指定了relavant配置文件db凭据)。我需要做些什么来完成这项工作。下面是我的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

以下是当前的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>

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

1 个答案:

答案 0 :(得分:7)

您需要定义&#39; liquibase.contexts&#39;属性到您的yaml文件中。像下面的东西。

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

添加此项后,以下更改集只会在您的本地资料为&#39; dev&#39; (即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>