3

Posting here in case it helps someone.

TLDR: ${database.defaultSchemaName} within sqlCheck to access default schema name

I wanted my database changeset to first check if a certain row is present in the database. If not present, it should execute the changeset.

Example:

myschema.customers:

uuid name age
0001 Bob 22
0002 Sally 25

What I want: liquibase to check if uuid = 0002 is present in the myschema.customers table. If so, run the changeset, otherwise skip over it.

Initially I tried this:

<changeSet id="00000000000007" author="jhipster">
    <preConditions onFail="MARK_RAN">
        <sqlCheck expectedResult="0">
            select count(*) from customers WHERE uuid= '0002'
        </sqlCheck>
    </preConditions>
    <insert tableName="customers">
        <column name="uuid" value="0002"></column>
        <column name="name" value="Sally"></column>
        <column name="age" value="25"></column>
    </insert>
</changeSet>

Bear in mind that I have set the following configuration for liquibase (4.6.1)

spring:
    liquibase:
        default-schema: myschema

But it seems precondition sqlcheck was checking public.customers and not myschema.customers. This would cause an error when uuid 0002 is already present in myschema.customers.


SOLUTION: use ${database.defaultSchemaName}

<preConditions onFail="MARK_RAN">
    <sqlCheck expectedResult="0">
        select count(*) from ${database.defaultSchemaName}.customers WHERE uuid= '0002'
    </sqlCheck>
</preConditions>
lemon
  • 14,875
  • 6
  • 18
  • 38
blimkt
  • 31
  • 2
  • Can you clarify: did you have a question or is this an informational post for other SO users? – tabbyfoo May 17 '22 at 22:00
  • Ah @tabbyfoo, I meant it to be an informational post. Wasn't sure if it's better practice post Qn + Ans separately but decided to combine them – blimkt May 31 '22 at 09:35

0 Answers0