Say you want to set up a Liquibase changelog to insert some data, but you don't know the IDs of some entity, and you can express it as a query. With Liquibase's computed
column attribute, you can embed subqueries directly into seed data:
column1;column2
(select id from table2 where some_column = 'foo');BAR
(select id from table2 where some_column = 'foo');BAZ
Mind the parentheses! Liquibase converts a loadData
change into an INSERT INTO ... VALUES (...)
statement, so your subqueries need to be tucked in and cozy.
Your changelog should look something like this:
databaseChangeLog:
- changeSet:
id: 1
author: xxx
objectQuotingStrategy: QUOTE_ONLY_RESERVED_WORDS
preConditions:
- onFail: MARK_RAN
- sqlCheck:
expectedResult: 0
sql: select count(*) from my_table
changes:
- loadData:
tableName: my_table
file: db/seeds/my_table.csv
separator: ;
encoding: UTF-8
columns:
- column:
header: column1
name: column1
computed: true
- column:
header: column2
name: column2
computed: false
And that's it! Let me know if this is useful to you! Liquibase's documentation is pretty rough in parts, and it took me a little bit to figure out how to assemble these pieces into a solution to my problem.