Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a WHERE-clause in a LiquiBase changeset

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>
like image 515
du-it Avatar asked Sep 26 '14 16:09

du-it


People also ask

How do I create a changeset in Liquibase?

To generate a newer version of the changelog file with stored logic objects based on the current database state, you need to delete, rename, or move the Objects directory that was created by running the generate-changelog command previously. Then, you can run the generate-changelog command again.

How do you add precondition to Liquibase?

You can use one <preConditions> tag per changeset and one overarching <preConditions> tag in the changelog (outside any changeset). In XML, YAML, and JSON changelogs, you can use conditional logic to add multiple preconditions within a single <preConditions> tag.

What is a changeset in Liquibase?

The changeset tag is a unit of change that Liquibase executes on a database and which is used to group database Liquibase Change Types together. A list of changes created by multiple changesets are tracked in a changelog.


1 Answers

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>
like image 50
Nathan Voxland Avatar answered Oct 17 '22 23:10

Nathan Voxland