'How to reference the default schema in liquibase preCondition sqlCheck

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>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source