如何使用 liquibase 上下文仅在授权配置文件中插入虚假数据?

How do I use liquibase contexts to insert fake data on only authorized profiles?

我在一个使用 liquibase-core 4.2.0 的 SpringBoot 项目上,我想使用 spring 配置文件 'dev'.[=17= 为我的本地执行插入假数据]

我为我的每个配置文件添加了一个 liquibase 上下文(application-dev.yaml 的示例):

spring:
    profiles:
        active: dev
    liquibase:
        contexts: dev

并在我的更新日志文件的 <include /> 中添加了上下文:

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

  <property name="now" value="now()" dbms="h2"/>
  <property name="now" value="GETDATE()" dbms="mssql"/>
  <property name="floatType" value="float4" dbms="h2"/>
  <property name="floatType" value="float" dbms="mssql"/>
  <property name="clobType" value="clob" dbms="h2, mssql"/>
  <property name="uuidType" value="uuid" dbms="h2, mssql"/>

  <include file="changelog/1.0.0/financial_security/schema.xml" relativeToChangelogFile="true"/>
  <include file="changelog/1.0.0/financial_security/local_data_for_dev.xml" relativeToChangelogFile="true" context="dev"/>
  <include file="changelog/1.0.0/financial_security/tag-1.0.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

我的文件 local_data_for_dev.xml 如下所示:

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

  <changeSet id="20200422091200-fakedata" author="me">

    <!-- TABLE UC_250_COUNTRIES -->
    <loadData
            file="db/changelog/1.0.0/financial_security/fake-data/uc_250_countries.csv"
            separator=";"
            tableName="uc_250_countries">
      <column name="id" type="${uuidType}"/>
      <column name="country_code" type="string"/>
      <column name="label" type="string"/>
      <column name="value" type="string"/>
      <column name="created_date" type="datetime"/>
      <column name="updated_date" type="datetime"/>
    </loadData>

    [....]

  </changeSet>
</databaseChangeLog>

它工作得很好,但出于测试目的,我尝试更改我的更改日志以具有另一个上下文:

<include file="changelog/1.0.0/financial_security/local_data_for_dev.xml" relativeToChangelogFile="true" context="prod"/>

并且(清理数据库后)当我使用 'dev' 配置文件重新启动我的应用程序时,它仍然插入了假数据。为什么?

来自上一个笔记 here

Starting with Liquibase 3.5, you can specify a context attribute in <include> or <includeAll> tags. If specified, the given context is added to all changesets in the included file(s).

因此,请尝试从您的 include 中删除上下文,它应该会起作用。

似乎 liquibase.contexts 不是全局变量,但它绑定到与之关联的更新日志。我的 application.yml:

中有 2 个数据库的 2 个更新日志
liquibase:
  change-log: classpath:some_changelog.xml
second-liquibase:
  change-log: classpath:some_other_changelog.xml

插入假数据的变更日志来自second-liquibase,所以我需要在我的应用程序中设置变量second-liquibase.contexts-dev.yml:

liquibase:
  contexts: somecontext
second-liquibase:
  contexts: dev