postgresql - 使用带有 liquibase 变更集上下文属性的 spring boot 配置文件来管理变更集范围

标签 postgresql spring-boot liquibase liquibase-hibernate

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

在我的应用程序中,我有以下 spring 配置文件 -> 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

下面是当前的 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 上下文。这可能吗?

最佳答案

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

关于postgresql - 使用带有 liquibase 变更集上下文属性的 spring boot 配置文件来管理变更集范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40088915/

相关文章:

spring-boot - Spring Boot应用程序的Docker镜像

java - Spring Boot 验证该字段是字符串类型

mysql - Liquibase、MySQL 和多个模式

postgresql - 多列索引不用于仅索引扫描,但部分索引是

用于获取总值的前 10%、20% 和 30% 值的 postgresql 过程

ruby-on-rails - PG::SyntaxError: 错误:在 Rails 中的生产模式

java - 无法从 Spring Boot 应用程序验证 mongodb

mysql - 如何编写mysql查询以仅在foreign_key存在时插入值

mysql - 更改表后更新 changeLog 文件

mysql - NodeJS/Sequelize/MySQL - 为什么需要 postgres 依赖项?