Liquibase: Database change management

Analyzing data.
Reading Time: 3 minutes

Liquibase is one of the most protean tools for database migration. It operates on almost all SQL database outlets. So whether you use MySQL, SQL Garçon, Oracle, Firebird, or a combination of these or any other common database platforms, it’s got you covered. It also runs in any CI/ CD channel so long as you can run Java, install the connectors, and connect to your databases. This means you can use Liquibase to emplace database changes along with your new features that are released behind point toggles. It takes a bit of planning — especially when you are releasing clashing or destructive changes. Luckily, Liquibase has some mechanisms to make database changes less discouraging. It functions all the DDL procedures and several veritably usable refactorings. occasionally you have to be careful about when you run those database changes on your product cases since they can lock your tables for quite a while. In this post, you will learn how to use Liquibase to make all effects database migration easier.

Features of Liquibase

Some features claimed by Liquibase are

  • All changes to the database are stored in XML/ JSON/ Yaml or SQL lines and linked by a combination of an “ ID ” and “ author ” marker as well as the name of the train itself.
  • Updates database to the current interpretation
  • Rollback last X changes to the database, date/ time bylabel4.SQL for Database updates, and Rollbacks can be saved for manual review
  • Stand-alone IDE and decline draw-in
  • Database diff report, diff changelog generation
  • Capability to produce a changelog to induce a being database
  • Database change documentation generation
  • DBMS Check, user review, and SQL tab preconditions
  • Executable via command line, Ant, Maven, Servlet vessel, or Spring
  • Support for further than 10 RDBMS
  • Grails DB migration plugin uses Liquibase.

Manual tracking DB changes Possible issues:

  • easy to lose sync between law and DB state
  • hard to recover from crimes during development( partner in case of applying the wrong statement to DB)
  • Frequently bear tore-create DB from scrape during development
  • hard to find out the state of a particular DB terrain
  • may bear support for 2 performances of changes full and incremental

What makes liquibase different?

  • DDL abstraction DSL
  • Support for different changelog format
  • community- managed trendy and check
  • make- in XML, YAML, JSON, and SQL
  • Over 30 erected- in database refactorings
  • Rollback database changes feature
  • Database diff report
  • Extensibility

Database Change Log File

  • The root of all Liquibase changes is the database changelog file.
  • The Sample Change Log File exists as shown below:
<databaseChangeLog> <changeSet id=”1 author=”Chetan Bhagat”>″ 

<createTable tableName=”event”> 

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

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

</column> 

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

<constraints nullable=” false”/>

</column> 

<column name=”active” type=”boolean” defaultValueBoolean=”true”/> 

</createTable> 

</changeSet> 

<changeSet author=”sought” id=”tag database-example”> 

<tagDatabase tag=”0.1 />″ 

</changeSet> 

</databaseChangeLog>

Rollback

  • Specifying a tag to rollback will roll back all change sets that were executed against the target database after the given tag was applied.
  • You can specify the number of change sets to roll back.
  • You can determine the date to roll back to.
<changeSet id=”changeRollback” author=”Chetan Bhagat”> 

<createTable tableName=”event”> 

<column name=”id” type=”int”/> 

</createTable> 

<rollback> 

<dropTable tableName=”event”/> 

</rollback> 

</changeSet>

Generating ChangeLog File

  • For generating change log files from an existing database, we used the command line tool.
    • Run ./liquibase –changeLogFile=changelog.xml generateChangeLog
  • Here is an example of the Liquibase command:

Dump data to XML file Run

./liquibase --changeLogFile=data.xml --diffTypes=data generateChangeLog 

Be ready for OutOfMemoryError

Best Practices Some of the best practices we follow are as mentioned below:

• Organize Change Log File

• We organize our application version files in separate changeLog files and we have a master ChangeLog File.

Below is the master changelog file.

<?xml version=”1.0 encoding=”UTF-8 ?>″ ″ <databaseChangeLog> <include 

file=”com/myapp/db/changelog/db.changelog-1.0.0.xml”/> <include 

file=”com/myapp/db/changelog/db.changelog-1.0.1.xml”/> <include 

file=”com/myapp/db/changelog/db.changelog-1.0.2.xml”/> </databaseChangeLog>

• Change Set ID

  • Supply change set ID for all the modifications.

• Use Liquibase from the initial stage of development.

How to run liquibase

On-Demand :

  • Command Line
  • Ant
  • Maven

Automated:

  • Servlet Listener
  • Spring Listener
  • JEE CDI Listener

Java APIs

Liquibase can smoothly be ingrained and managed through its Java APIs.

1. Create property file liquibase. property with connection parameters:

spring.liquibase.enabled=true

spring.application.name=sample-liquibase-app

spring.liquibase.change-log=classpath:db/changelog/changelog-master.xml

spring.datasource.url=jdbc:mysql://localhost:3306/liquibase

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.username=root

spring.datasource.password=pass

2. Run liquibase as

 ./liquibase <command> 

Conclusion :

In this Blog, we demonstrated different ways to use Liquibase for different database changes.

To learn more about Manage Your Database Schema, you can visit this link.

To read more tech blogs, feel free to visit Knoldus Blogs.

Written by 

He is a Software Consultant at Knoldus Inc. He has done B.Tech from Dr. APJ Kalam Technical University Uttar-Pradesh. He is passionate about his work and having the knowledge of various programming languages like Java, C++, Python. But he is passionate about Java development and curious to learn Java Technologies. He is always impatient and enthusiastic to learn new things. He is good skills of Critical thinking and problem solving and always enjoy to help others. He likes to play outdoor games like Football, Volleyball, Hockey and Kabaddi. Apart from the technology he likes to read scriptures originating in ancient India like Veda,Upanishad,Geeta etc.

Leave a Reply