10

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

在我的应用程序中,我有以下 spring 配置文件-> dev、prod (每个都在应用程序 yaml 文件中正确指定,并在配置文件下指定了相关的配置文件 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 文件(如果我的 spring 配置文件是 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>

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

4

1 回答 1

26

您需要在 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

添加后,以下更改集将仅在您的本地配置文件为“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>
于 2017-07-04T02:52:28.593 回答