Updated April 4, 2023
Definition of Spring Boot Liquibase
It is an open-source tool that was used for version control for databases and also used in database schema migration. This tool is supported in many RDBMS databases like MySQL, PostgreSQL, Oracle, and MSSQL, also it will supporting in different types of formats like SQL, XML, JSON, and YAML. It is also a vendor-independent database which means it will not depend on any database-specific syntax, it will also generate the change documentation of the database. Using this tool we can create a table and also do all the operations on the database server.
What is Spring Boot Liquibase?
- It is used to keep track of script revision of database schema. It is working on many databases and accepting multiple file-formats to define the structure of the database.
- Using this we can roll back our changes in the database also we can forward it to the given point. Also, we can remove the changes from the database to remember which script we have executed on the database instance.
- It is used when it was necessary to revert or update the changes from the database server.
- Also, we can use to migrate the database schema changes from one version to another version. It is similar to spring boot flyway both tools are used in database schema migration.
- Using this tool it is possible to manage the change in a variety of files and use the monitoring and report the same it on the dashboard.
Usage
- We can use to develop the java application which was started by using a jar file.
- We can do a migration of schema changed from the database server.
- Also, spring boot features are include logging, profiles, security, integration, testing, and many more.
- Below are the prerequisites which were we required at the time of developing the project.
Pre-requisites
1) Project – Maven Project
2) Spring boot version – 2.6.0
3) Packaging – Jar
4) Java – 11
5) Dependencies – PostgreSQL driver, Spring data JPA, liquibase migration
6) Spring tool suite
It has some important properties which were we have defined in the application.properties file.
1) Spring.liquibase.change-log –
This is the change log configuration path.
2) Spring.liquibase.url –
This is the database migration URL.
3) Spring.liquibase.user –
This is the database migration user.
4) Spring.liquibase.password –
This is the database migration password.
5) Spring.liquibase.liquibase-tablespace –
This is liquibase object tablespace.
6) Spring.liquibase.default-schema –
It is a database default schema.
7) Spring.liquibase.database-change-log-table –
This is the table name of change history.
8) Spring.liquibase.context –
It is the runtime context of comma-separated list.
9) Spring.liquibase.database-change-log-lock-table –
This is table name that was used to track the liquibase activities.
10) Spring.liquibase.labels –
It is runtime labels of the comma-separated list.
Create the project
Below is the example of creating the spring boot liquibase project are as follows.
1) Create a project template using spring initializer and give the following name to the project.
In the below step we have provided project group name as com. example, artifact name as spring-boot-liquibase, project name as spring-boot-liquibase, package as a jar file, and selecting java version as 11.
Group – com. example
Artifact name – spring-boot-liquibase
Name – spring-boot-liquibase
Description – Project for spring-boot-liquibase
Package name – com.example.spring-boot-liquibase
Packaging – Jar
Java – 11
Dependencies – spring web, PostgreSQL driver, spring data JPA and liquibase migration
2) After generating project extract files and open this project by using spring tool suite –
3) After opening the project using the spring tool suite check the project and its files –
Examples
Below is the example of spring boot liquibase are as follows.
1) Add dependency packages –
Code:
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-web</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.liquibase</groupId> -- Start and end of groupId tag.
<artifactId>liquibase-core</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.postgresql</groupId> -- Start and end of groupId tag.
<artifactId>postgresql </artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
2) Configure the application.properties file –
Code:
spring.datasource.url = jdbc:postgresql://localhost/spring_boot_liquibase
spring.datasource.username = postgresql
spring.datasource.password = postgresql
spring.datasource.platform = postgresql
spring.jpa.hibernate.ddl-auto=update
3) Create liquibase change log file –
Code:
databaseChangeLog:
- include:
file: db/changelog/stud-schema.xml
- include:
file: db/changelog/stud-data.xml
4) Create stud-schema.xml file –
Code:
<createTable tableName="stud" --- Create table name as stud.
Remarks ="stud Data">
<column name="stud_id" type="int" autoIncrement="true">
</column> -- End of column tag
<column name="stud_name" type="varchar (10)"> -- Start of column name tag.
</column> -- End of column tag
</createTable>
5) Create stud-data.xml file –
Code:
<comment> Inserting stud records</comment> -- Start and end of comment tag.
<insert tableName="stud">
<column name="stud_id" valueNumeric="1"/>
<column name="stud_name" value="ABC"/>
</insert> -- End of insert tag.
<insert tableName="stud">
<column name="stud_id" valueNumeric="2"/>
<column name="stud_name" value="PQR"/>
</insert> -- End of insert tag.
<insert tableName="company">
<column name="stud_id" valueNumeric="3"/>
<column name="stud_name" value="XYZ"/>
</insert> -- End of insert tag.
6) Run the application –
7) Check the table is created and data is inserted into stud table –
Code:
Select * from stud;
Importance
- It is a very important migration tool that was used and helps us to create the database schema, after creating schema it will run deployment of database server also it will do some sanity check to ensure that all changes which we have done are properly working or not.
- It provides a good starting point to address the team which came to manage the schema changes of the database.
- It has the ability to roll back and forward our database changes to a specific point. To use it in our project we need to add the liquibase dependency.
Conclusion
Spring boot liquibase has the ability to manage and revision of our database schema scripts. It is an open-source tool that was used for version control for databases. We can run liquibase on a spring boot environment by using the liquibase spring bean.
Recommended Articles
This is a guide to Spring Boot Liquibase. Here we discuss the Definition, What is Spring Boot Liquibase, Prerequisites, examples. You may also have a look at the following articles to learn more –