loadUpdateData 总是插入整个 CSV

Aft*_*ess 2 liquibase

我希望能够编辑我的 CSV 文件的内容,每次更改时,都会添加/修改/删除相应的记录。

使用 loadUpdateData 加上 runOnChange="true",每次 CSV 发生更改时,整个 CSV 内容都会重新插入到数据库中,从而导致大量重复。

在 MySql Community Server 5.7 中使用 liquibase maven 插件 3.0.5

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

<changeSet author="foobar" id="fizzbuzzDataLoad"  runOnChange="true">
    <loadUpdateData
            encoding="UTF-8"
            file="src/main/resources/liquibase/fizzbuzz.csv"
            quotchar=""
            separator=","
            primaryKey="ïntA"
            tableName="fizzbuzz">
        <column name="intA" type="NUMERIC"/>
        <column name="output" type="STRING"/>
    </loadUpdateData>
</changeSet>
</databaseChangeLog>
Run Code Online (Sandbox Code Playgroud)

例子

文件的初始状态 --> 表为空时插入的所有行:

intA,Output
1,1
2,2
3,FIZZ
4,4
5,BUZZ
6,FIZZ
Run Code Online (Sandbox Code Playgroud)

添加新行 --> 再次插入所有行

7,7
Run Code Online (Sandbox Code Playgroud)

数据库看起来像这样:

  intA  Output
    1   1
    2   2
    3   FIZZ
    4   4
    5   BUZZ
    6   FIZZ
    1   1
    2   2
    3   FIZZ
    4   4
    5   BUZZ
    6   FIZZ
    7   7
Run Code Online (Sandbox Code Playgroud)

Aft*_*ess 5

愚蠢的我, intA 在其创建表更改日志中没有被标记为主键。这解决了眼前的问题。如果检测到键冲突,它将更新值。

也就是说,如果我从 CSV 中删除一行,它会保留在数据库中。我通过让 Liquibsae 始终截断表然后运行 ​​loadData 来插入所有数据来解决这个问题。

<changeSet author="fooBar" id="fizzbuzzDataClear"
        runAlways="true">
        <delete tableName="fizzBuzz">
        </delete>
    </changeSet>

    <changeSet author="fooBar" id="fizzbuzzDataLoad"
        runAlways="true">
        <loadUpdateData encoding="UTF-8"
            file="src/main/resources/liquibase/data/fizzbuzz.csv" quotchar=""
            separator="," primaryKey="ïntA" tableName="fizzbuzz">
            <column name="intA" type="NUMERIC" />
            <column name="output" type="STRING" />
        </loadUpdateData>
    </changeSet>
Run Code Online (Sandbox Code Playgroud)