×
☰ See All Chapters

Liquibase example

In this tutorial we will learn to create liquibase example step by step. We have given simple and easy steps to create liquibase example. We will learn to create change log file, liquibase property file. At last we will learn to execute liquibase from command prompt.

Before creating example, you should have installed the below prerequisites:

  1. JDK and java path set up in environment variables 

  2. Data base like MySQL, Oracle etc... we have used MySQL 

  3. Liquibase installation, refer our previous tutorial to install liquibase and never miss out any steps in that 

Step 1: Create a folder to keep liquibase change Log file and connection property file. Make sure the folder name and all the folder names in this path doesn’t have an empty space. Example folder name should not be “liquibase example” instead use underscore in folder name like “liquibase_example”.

Step 2: Create the files changelog_1.0.xml and liquibase.properties. In liquibase.properties file provide the information according to your database installation. We have used MySQL 8.0.

liquibase-example-0
 

liquibase.properties

driver=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/liquibase

username=root

password=root

changelog_1.0.xml

<databaseChangeLog

        xmlns="https://www.liquibase.org/xml/ns/dbchangelog"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.liquibase.org/xml/ns/dbchangelog

                        https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">

 

        <changeSet id="create_student" author="manu.manjunatha">

                <createTable tableName="student">

                        <column name="id" type="int">

                                <constraints primaryKey="true" nullable="false" />

                        </column>

                        <column name="name" type="varchar(50)">

                                <constraints nullable="false" />

                        </column>

                </createTable>

        </changeSet>

 

        <changeSet id="create_employee" author="manu.manjunatha">

                <createTable tableName="employee">

                        <column name="id" type="int">

                                <constraints primaryKey="true" nullable="false" />

                        </column>

                        <column name="firstname" type="varchar(50)">

                                <constraints nullable="false" />

                        </column>

                        <column name="lastname" type="varchar(50)">

                                <constraints nullable="false" />

                        </column>

                </createTable>

        </changeSet>

 

</databaseChangeLog>

Step 3: Execute liquibase. Open command prompt for the folder having changelog_1.0.xml and liquibase.properties file and execute the below command:

liquibase --defaultsFile=C:\Liquibase_example\liquibase.properties --changeLogFile=C:\Liquibase_example\changelog_1.0.xml update

liquibase-example-1
 

Step 4: Check the database for output

liquibase-example-2
 

All Chapters
Author