How to build a WHERE-clause in a LiquiBase changeset

du-it picture du-it · Sep 26, 2014 · Viewed 17k times · Source

How do I have to define a changeset in 'LiquiBase' notation for updating a table column whith an AND-ed WHERE-clause:

<changeSet id="ddl update tables : modify datatype for MY_TABLE.STATE_ABBREV" author="xxx">
    <preConditions onFail="MARK_RAN" onFailMessage="Column MY_TABLE.STATE_ABBREV doesn't exists.">
        <and>
            <tableExists tableName="MY_TABLE"/>
            <columnExists tableName="MY_TABLE" columnName="STATE_ABBREV"/>
        </and>
    </preConditions>
    <update tableName="MY_TABLE">
        <column name="STATE_ABBREV" value="AS"/>
        <where>AGU   /***AND STATE_ID=3***/  ??????????????????
        </where>
    </update>
</changeSet>

Answer

Nathan Voxland picture Nathan Voxland · Sep 30, 2014

What you put in the <where> tag is simply appended to the end of the UPDATE statement after a " WHERE ". You can put anything in the where tag that you would normally put in SQL.

Example:

<changeSet id="ddl update tables : modify datatype for MY_TABLE.STATE_ABBREV" author="xxx">
    <preConditions onFail="MARK_RAN" onFailMessage="Column MY_TABLE.STATE_ABBREV doesn't exists.">
        <and>
            <tableExists tableName="MY_TABLE"/>
            <columnExists tableName="MY_TABLE" columnName="STATE_ABBREV"/>
        </and>
    </preConditions>
    <update tableName="MY_TABLE">
        <column name="STATE_ABBREV" value="AS"/>
        <where>STATE_ABBREV IS NULL AND STATE_ID=3</where>
    </update>
</changeSet>