Search code examples
sqlliquibase

Liquibase Insert + update from multiple tables


I'm starting to work with Liquibase "properly", by using his syntax and not only using SQL scripts for the management of our Oracle Databases.

For the DML part, I'm struggling to find a nice way to do an insert of data, in a fresh new created table, based on multiple other tables.

Lets take a fictive example:

User_ref

name firstname address_id
Hubert Jacques 22

For the moment, what I do is an Insert of address_id which I will use in 2 successive updates :

<changeSet...>
    <insert tableName="User_ref">
        <column name="address_id"
        valueComputed="(SELECT uc.address_id
                        FROM User_commands uc)/>
    </insert>
    <update tableName="User_ref">
        <column name="name"
        valueComputed="(SELECT ud.name
                        FROM User_data ud, User_ref ur
                        WHERE ud.address_id = ur.address_id)/>
    <update tableName="User_ref">
        <column name="firstname"
        valueComputed="(SELECT ul.firstname
                        FROM User_legal ul, User_ref ur
                        WHERE ul.address_id = ur.address_id)/>
</changeSet>

Is there a better way to execute such Insert ?

The point being to ideally only have one transaction and statement.

Edit: This would also need to be executed for all rows and address_ids... Which apparently isn't supported by insert/update queries of Liquibase.

Is the only way to do such inserts a custom SQL file ?

Thanks for your answers


Solution

  • Would seem more natural and supportable to just use a custom sql statement. Somtimes using the built-in Liquibase change-types makes the change more confusing.

    Custom sql can be used in sql, xml, yaml, or json file formats.