Liquibase notes

By | October 13, 2016

1. Generate Change Logs from an existing database

Generate change logs from an existing database. For instance, copy postgresSQL driver to %LINQUIBASE_HOME%/postgresql-8.3-603.jdbc3.jar. Run a following line command in console (single line).

java -jar liquibase.jar 
--password=1234 generateChangeLog

If successful, liquibase will show a message “Liquibase ‘generateChangeLog’ Successful

2. Check a table already presents

Using preCondition tag which follows some form of boolean evaluation to check a table exists. For example, not(tableExists) = not(true) = false, therefore the createTable would not execute.

<changeSet id="1" author="Khiem.Truong">
        <preConditions onFail="MARK_RAN">
                <tableExists tableName="trading"></tableExists>
        <createTable tableName="trading">
            <column name="code" autoIncrement="true" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>

3. Insert a record to database.

Similarly, if the select query below >= 1, sqlCheck will return false and raise onFail

<changeSet id="9" author="Khiem.Truong">
        <preConditions onFail="MARK_RAN">
            <sqlCheck expectedResult="0">select count(*) from id_code_data 
where symbol='gbst' and assigned_prefix='NAS'</sqlCheck>
        <insert tableName="id_code_data">
            <column name="symbol" value="gbst"/>
            <column name="name" value="Global banking settlement transaction"/>
            <column name="assigned_prefix" value="NAS"/>

4. Change datatype.

A column updates to a new datatype if it already exists. Otherwise, the changeSet is ignore.

 <changeSet id="change_eicc_sub_section_label" author="Khiem.Truong">
        <preConditions onFail="MARK_RAN">
            <columnExists tableName="id_code_data" columnName="name"/>
        <modifyDataType tableName="id_code_data" columnName="name" newDataType="text" />


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.