
Introduction
Liquibase is an open-source tool. It is used for tracking, managing, and applying database schema changes. It works across different types of databases and supports various file formats for defining the database structure. The feature that is most attractive in Liquibase is its ability to roll changes back and forward from a specific point.
In any application, if you change any tables for any requirements and if you want to revert to the old versions of the table then Liquibase can be used safely. Also, it provides database neutral way to execute create, update, and delete operations. We can also write the changes in XML files which can be applied to any database.
Suppose four instances of the same application are running in four different databases we apply changes to the database by writing different SQL statements for the different databases is time-consuming. For this, Liquibase provides XML tags and attributes to apply database changes. So, without writing SQL directly against the database to create, update or drop database objects, we can define database changes in XML/JSON/YAML files. These are called database changelog files.
Core Concepts
- ChangeSet:- Liquibase uses the changesets to describe a single change to your database. Every changeset has an (“id” and “author”) attribute which, along with the directory and file name of the changelog file, helps in uniquely identifying it. Liquibase tracks the implementation of all the changes at a ChangeSet level.
- Change:- The Change in Liquibase illustrates a single change that needs to be applied to the database. Liquibase provides various change types like “create table” or “drop column” out of the box which is each an abstraction over a piece of SQL.
- Changelog:- The changelog is a file that contains a record of all your database changes (changesets). So, Liquibase uses this changelog record to audit your database and execute any changes that are not yet applied to your database. These changelog files can be in various formats (SQL, YAML, XML, or JSON).
- Preconditions:- The Preconditions are used to control the execution of changeSets or changelogs. It is used to define the state of the database under which the changeSets or changes logs need to be executed.
- Labels:- The purpose of Labels and context is similar. The difference is that changeSets are always tagged with records of labels(not expressions). And during runtime, we can also pass a label expression to select the changeSets which match the expression.
- Changelog Parameters:- It allows us to have placeholders in changelogs, which it dynamically replace during runtime.
- Context:- A changeSet can be tagged with the context expression. Liquibase will execute this expression to find out, if a changeSet should be executed at runtime, given a specific context. You can compare a context expression with the environment variables.
How to set it up?
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>{{liquibase.verion}}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-maven-plugin -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>{{liquibase.verion}}</version>
</dependency>
We need to add these plugin inside the POM.xml file
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFile>${liquibase.propertyFile}</propertyFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
</plugin>
Generate a ChangeLog From an Existing Database
To generate a changelog from an existing database, We have to use the plugin
mvn liquibase:generateChangeLog
Here are the Liquibase properties:
url=${your urlPath}
username=${your username}
password=${your password}
driver=${your data base driver}
outputChangeLogFile=${your change log file path}
The end result is a changeLog file. Now we can use it either to create an initial database schema or to populate data.
Here’s how that would look:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog ...>
<changeSet author="Pawan(generated)" id="0431439225029-1">
<createTable tableName="MY_TABLE">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints primaryKey="true"/>
</column>
<column name="token" type="VARCHAR(255)"/>
<column name="captcha" type="BIT(1)">
<constraints nullable="false"/>
</column>
<column name="password" type="VARCHAR(255)"/>
<column name="refreshToken" type="VARCHAR(255)"/>
<column name="tokenExpiration" type="datetime"/>
<column name="userName" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="preference_id" type="BIGINT"/>
<column name="userAddress" type="VARCHAR(255)"/>
</createTable>
</changeSet>
...
</databaseChangeLog>
Advantages of Liquibase
Flexible database change:- In Liquibase, Queries are written using database-neutral languages like XML, JSON, and YAML. We can use single query writing for multiple databases. Without this, you have to write separate SQL queries for different databases.
Version control for your database:- Maintaining definitions in XML, JSON, and YAML-neutral languages are easier than maintaining different SQL files for different databases. Order changes and standardized development.
Built for developers:- Liquibase control when, where, and how database changes are deployed.
Liquibase works with a wide range of databases:- We can use Liquibae with many databases. While migrating the database, the Changelog files can be used without rewriting SQL statements for new databases. It help’s in saving effort and increases productivity.
Liquibase is popular:- According to the official website of Liquibase. This has been downloaded 75 million times.
Easy setup:- Liquibase is very easy to set up.
Conclusion
In this blog, we learned how to manage your database objects without writing and without depending on SQL queries.
Reference Link :- https://en.wikipedia.org/wiki/Liquibase