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 
--classpath=postgresql-8.3-603.jdbc3.jar 
--driver=org.postgresql.Driver 
--changeLogFile=./structure.xml 
--url="jdbc:postgresql://abc.com:5432/store_db" 
--username=postgres 
--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">
            <not>
                <tableExists tableName="trading"></tableExists>
            </not>
        </preConditions>
        <createTable tableName="trading">
            <column name="code" autoIncrement="true" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            ...
        </createTable>
    </changeSet>

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>
        </preConditions>
        <insert tableName="id_code_data">
            <column name="symbol" value="gbst"/>
            <column name="name" value="Global banking settlement transaction"/>
            <column name="assigned_prefix" value="NAS"/>
        </insert>
    </changeSet>

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"/>
        </preConditions>
        <modifyDataType tableName="id_code_data" columnName="name" newDataType="text" />
    </changeSet>

References:

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.