Wysmedia.com

Icon

~ We make IT easy for you ~

Using Liquibase (tutorial)

Liquibase is the powerful database migration that available for free. For long time I am envy of rails database migration system. It is nice and have rich features.

Few weeks ago, I found that Grails has Liquibase plugin (there are 2, liquibase and autobase). I tried both plugins and autobase won my heart because the simplicity they offer (not using XML is the biggest advantages, for me at least). Liquibase however build as stand alone java application that you can run with any operating system that support java.

Here I will show you how to configure and using Liquibase on command line, so you can apply whether you are using php, python, or others programming language/framework

  • First, download and extract Liquibase files from www.liquibase.org
  • Create a file named liquibase.properties
  • Write the following, you can modify it to suit your needs
    1
    2
    3
    4
    5
    6
    7
    
    classpath: ../;lib/mysql-connector-java-5.1.6-bin.jar
    changeLogFile=changelogs/root.changelog.xml
    username=root
    password=
    url=jdbc:mysql://localhost/tester
    driver=com.mysql.jdbc.Driver
    logLevel=SEVERE

    * you must download mysql-connector-java-5.xxxx.jar (mysql jdbc driver) from www.mysql.com

  • Create a directory changelogs
  • Inside, create root.changelog.xml and write the following
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    <?xml version="1.0" encoding="UTF-8"?>
     
    <databaseChangeLog
            xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
        <preConditions>
            <dbms type="mysql"/>
            <sqlCheck expectedResult="1">select 1</sqlCheck>
            <runningAs username="root"/>
        </preConditions>
     
        <changeSet id="1" author="nvoxland">
            <comment>
                You can add comments to changeSets.
                They can even be multiple lines if you would like.
                They aren't used to compute the changeSet MD5Sum, so you can update them whenever you want without causing
                problems.
            </comment>
            <createTable tableName="person">
                <column name="id" type="int" autoIncrement="true">
                    <constraints primaryKey="true" nullable="false"/>
                </column>
                <column name="firstname" type="varchar(50)"/>
                <column name="lastname" type="varchar(50)">
                    <constraints nullable="false"/>
                </column>
            </createTable>
        </changeSet>
        <changeSet id="2" author="nvoxland">
            <comment>Add a username column so we can use "person" for authentication</comment>
            <addColumn tableName="person">
                <column name="usernae" type="varchar(8)"/>
            </addColumn>
        </changeSet>
        <changeSet id="3" author="nvoxland">
            <comment>Fix misspelled "username" column</comment>
            <renameColumn tableName="person" oldColumnName="usernae" newColumnName="username" columnDataType="varchar(8)"/>
        </changeSet>
    </databaseChangeLog>
  • When you finished, go to command line and type :

    java -jar liquibase-1.9.0.jar migrate

  • This command will create databasechangelog and databasechangeloglock first. Databasechangelog contains record migration that you have done in the past. If you delete one record, next time you run migrate, it will re-run again

Now I will explain what is inside the changelog

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
 
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
    <preConditions>
        <dbms type="mysql"/>
        <sqlCheck expectedResult="1">select 1</sqlCheck>
        <runningAs username="root"/>
    </preConditions>
..... 
</databaseChangeLog>

It is the header of changelog that you need to have.
There are preConditions tag that will help you to know whether the database is mysql or not.

next :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<changeSet id="1" author="nvoxland">
        <comment>
            You can add comments to changeSets.
            They can even be multiple lines if you would like.
            They aren't used to compute the changeSet MD5Sum, so you can update them whenever you want without causing
            problems.
        </comment>
        <createTable tableName="person">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="firstname" type="varchar(50)"/>
            <column name="lastname" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

This change set will generate a table Person for you. having column firstname and lastName

next:

1
2
3
4
5
6
<changeSet id="2" author="nvoxland">
        <comment>Add a username column so we can use "person" for authentication</comment>
        <addColumn tableName="person">
            <column name="usernae" type="varchar(8)"/>
        </addColumn>
    </changeSet>

The second update will add “usernae” (typo mistakes is on purpose) with width 8 characters

next: we need to fix the usernae become username

1
2
3
4
    <changeSet id="3" author="nvoxland">
        <comment>Fix misspelled "username" column</comment>
        <renameColumn tableName="person" oldColumnName="usernae" newColumnName="username" columnDataType="varchar(8)"/>
    </changeSet>

They are lots of command that you can use on Liquibase, just make sure you read the manual first on http://www.liquibase.org/manual/

Category: Programming, database, java

Tagged: , , ,

Leave a Reply