DevTUPLE

Basic maven commands for Liquibase with Springboot

Liquibase is a version control system for database systems. This blog gives a quick guide to run the maven liquibase commands.

Requirements:

  1. Create a liquibase project using spring boot and maven.

  2. Give the liquibase-maven plugin in the pom.xml file as given below.

    <plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.4.2</version>
    <configuration>
        <propertyFile>liquibase/liquibase.properties</propertyFile>
        <changeLogFile>liquibase/changelog-master.xml</changeLogFile>
    </configuration>
    </plugin>
    • Here the tag <propertyFile> will have the MySQL connection and the tag <changeLogFile> will have the location to master changelog file or single changelog file.
  3. Create liquibase.properties file that has properties as given below

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3307/lastliqui
username=user_name
password=your_password
  1. Create changelog files that contains set of changeSets and give tag to each files.
  2. Changelog files

Sample Changelog master file

      <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"> 
      <include file="liquibase/changelog-1.0.xml"/> 
      </databaseChangeLog>

Sample Changelog file

     <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"> 
      
    <changeSet id="1" author="ab">
        <createTable tableName="DEMO_TABLE">
            <column name="id" type="int"></column>
            <column name="name" type="varchar(50)"></column>
        </createTable>
    </changeSet>
	  <changeSet id="tag-1.0" author="ab">
       <tagDatabase tag="1.0" />
    </changeSet>
    </databaseChangeLog>

To Update Database using Changelog files:

The update command will execute all the changelog files hence we get the database with the updated tables.

mvn liquibase:update

To Rollback to specific version of changelog file:

The Rollback command will rollback the execution the the verion that is specified on the rollbackTag. In the below example the value 1.0 tag number.

mvn liquibase:rollback -Dliquibase.rollbackTag=1.0

This rollback can also be done by

  • Tag Number - rollback to specific version that can be specified in each changelog files.
  • RollbackCount - rollback by crossing specific number of changesets.
  • RollBackDate - rollback to the version that was available at a specific date.

For updating to specific version of changelog file:

The updation will be done upto the specified version. In the below example the value 1.1 is the version upto which the database needs to be updated.

mvn liquibase:update -Dliquibase.toTag=1.1

For taking snapshot of an existing database:

The generateChangeLog command will take snapshot and store the snapshot in the file as mentioned in outputChangeLogFile. This output file can be of anytype that is supported by liquibase.

mvn liquibase:generateChangeLog -Dliquibase.outputChangeLogFile=d:\output.xml

Written by Aravind

Do you want to know when I post something new?
Then subscribe to my newsletter.