Sophie3591
Sophie3591

Reputation: 71

Check if not null or not 0 in Liquibase Precondition

I have currently an issue with liquibase preconditions. I would like to insert something only if a precondition request does'nt answer 0 or null... I explain :

<changeSet id="myId" author="myName">
<preConditions onFail="MARK_RAN">
    <sqlCheck expectedResult=????>SELECT COUNT(1) FROM tableB WHERE column2 IS NOT NULL;
    </sqlCheck>
</preConditions>
<insert tableName="tableA">
    <column name="column1" valueComputed="(SELECT columnA FROM tableB WHERE columnB IS NOT NULL;)" />
    <column name="column2" valueComputed="(SELECT columnB FROM tableB WHERE columnB IS NOT NULL;)" />
</insert>

I would like for my changeSet to only be played if the first request give me a result. Is there a way to do that without a custom precondition ?

Thanks in advance

Upvotes: 5

Views: 16649

Answers (1)

Roland Weisleder
Roland Weisleder

Reputation: 10511

Liquibase provides the conditional preconditions and/or/not, which can be used with all other preconditions. In your case, wrap the <sqlCheck> with a <not>.

<preConditions onFail="MARK_RAN">
    <not>
        <sqlCheck expectedResult="0">SELECT COUNT(1) FROM tableB WHERE column2 IS NOT NULL;</sqlCheck>
    </not>
</preConditions>

See also the Preconditions documentation.

Upvotes: 14

Related Questions