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

e0bqpujr  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(242)

我在一个springboot项目中使用liquibasecore4.2.0,我想使用spring配置文件dev为我的本地执行插入假数据。
我为我的每个配置文件添加了一个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”配置文件重新启动应用程序时(清理数据库之后),它仍然插入了假数据。为什么?

aurhwmvo

aurhwmvo1#

从这里的最后一个音符
从liquibase 3.5开始,可以在中指定上下文属性 <include> 或者 <includeAll> 标签。如果指定,给定的上下文将添加到包含文件中的所有变更集。
所以试着从你的语言中删除上下文 include 它应该有用。

41zrol4v

41zrol4v2#

好像 liquibase.contexts 不是全局变量,但它绑定到与其关联的changelog。我的application.yml中有2个数据库的更改日志:

liquibase:
  change-log: classpath:some_changelog.xml
second-liquibase:
  change-log: classpath:some_other_changelog.xml

插入假数据的changelog来自 second-liquibase ,所以我需要设置变量 second-liquibase.contexts 在my application-dev.yml中:

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

相关问题