I want to be able to edit the contents of my CSV file and every time there is a change it to, the appropriate records be added/modified/deleted.
Using loadUpdateData coupled with runOnChange="true", each time there is a change in the CSV, the entire CSV contents are re-inserted to the DB causing lots of duplicates.
Using liquibase maven plugin 3.0.5 with MySql Community Server 5.7
<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>
Example
Initial state of file --> All rows inserted when table was empty:
intA,Output
1,1
2,2
3,FIZZ
4,4
5,BUZZ
6,FIZZ
New row added --> All rows inserted again
7,7
DB looks like this:
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
Silly me, intA was not marked as primary key in its create table changelog. That fixed the immediate problem. It will update values if it detects a key collision.
That said, if I delete a row from the CSV, it remains in the DB. I've worked around this by having Liquibsae always truncate the table and then run loadData to insert all data fresh.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With