
Liquiabse is used to track changes in our DB throughout time. It is an open-source library that facilitates us in tracking, managing, applying schema on our DB. Taking a simple example, if we want to switch one MySql to Postgresql DB, liquibase facilitates us to copy our existing schema and DB without writing a manual script from the Developer. Basically, it works on Change-Set. Each change-set is having an Author and Unique id. In the change-set, we can pass any schema like creation and alteration of table.
Liquibase internally two tables in our Database which is “DATABASECHANGELOG”, which tracks the change-set, which are applied on our DB until now. And the second table is “DATABASECHANGELOGLOCK” which ensures that at a time only one change-set is performed on our DB. Before applying a change-set our liquiabse acquire this lock and perform the change-set, by which we will achieve Database consistency.
Now, let's jump into the code to check how we can take benefit of liquibase in our day-to-day coding life.
First of all, let us look at our pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.liquibase</groupId>
<artifactId>LiquibaseDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LiquibaseDemo</name>
<description>Demo project for Spring Boot and Liquibase</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<propertyFile>src/main/resources/application.properties</propertyFile>
<!-- <propertyFileWillOverride>true</propertyFileWillOverride> -->
<changeLogFile>src/main/resources/db/changelog/changelog-master.xml</changeLogFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here for liquibase we require liquibase-core dependency and we need to provide the application.properties file where we have given configuration of liquibase and path to changelog file where our all change-set will available.
Now, let's have a look at our application.properties file
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.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.dialect=org.hibernate.dialect.MySQL8Dialect
Here as of now, we have provided our JPA datasource details along with one change-log file location where the application can find the change-log file. Now, let's have a look at our changelog-master file into the mentioned path:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd
http://www.liquibase.org/xml/ns/pro
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.1.xsd">
<include file="classpath:db/changelog/changelog-v1.xml"/>
</databaseChangeLog>
In this apart from XML namespace we have given a reference of a file which is containing our change-set using include tag and our changelog-v1 file looks like:
Here, we have mentioned two change-set one is the schema of the table and one is for making one entry inside that. Now we are ready to run our application and check if our change-set works or not. Once our application runs successfully we will get three tables inside our DB one is a student from our change-set and two are default tables created by liquibase as mentioned earlier. let's check them:
Table: databasechangeloglock
Here, we are able to see that using change-set we can create/initialize our database table.
Now, let's check how we can get the script file of table schema using liquibase. First of all, we need to add some more properties in our application.properties file:
url=jdbc:mysql://localhost:3306/liquibase
driver=com.mysql.cj.jdbc.Driver
username=root
password=root
outputchangelogfile=src/main/resources/db/changelog/changelog-v2.xml
These configurations will be used by liquibase to create a script file. Let's check how we will get the schema file.
To get schema we need to run configuration like this:
Once we get “Build Success”, now we need to check our generated schema file from liquibase, our output file is as below:
This is it, we successfully generated a change-set script from the liquibase. Same we can use anywhere. There are many more commands you can refer to from the below link and explore the liquibase.
https://docs.liquibase.com/commands/home.html
Feel free to comment in case of any mistake.