What ?
Liquibase is an open-source database migration tool that manages and can automate changes to a database schema, ensuring consistency across different environments. They enable version control, scripting, and execution of schema updates, making it easier to track, apply, and roll back changes in a structured and repeatable way.
Why ?
Just as Git serves as a version control tool for code, Liquibase acts as a versioning tool for databases, tracking who made changes, what was modified, and when it happened.
Schema Versioning – Tracks and applies database changes systematically, just like Git does for code.
Multiple Change Formats – Supports XML, YAML, JSON, and SQL, providing flexibility in defining migrations.
Automated Rollbacks – Enables undoing changes easily, reducing risks in deployments.
Change History Tracking – Maintains a record of who changed what and when, improving auditability.
Integration & CI/CD Support – Seamlessly integrates with DevOps pipelines and tools like Jenkins, GitHub Actions, and Kubernetes.
How ?
(for this example I used maven as a dependency manager and YAML for the scripts).
- Add the maven dependency in the pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
- Setup the database configuration so the application can connect to the database.
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=root
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- Now the Liquibase specific files can be created.
The main file is db.changelog-master.yaml
, which is the entry point that Liquibase will use applying database changes, all the migration scripts have to be added here. After this is created, one can create all the databases migration scripts need.
The file structure:
TODO: cannot paste pictures
db.changelog-master.yaml
example:
databaseChangeLog:
- include:
file: db/changelog/01-initial-table-structure.yaml
- include:
file: db/changelog/02-add-new-table.yaml
Here is an example of a database migration scripts, that will create a new table called post
with 3 columns:
databaseChangeLog:
- changeSet:
id: initial-database-structure
author: developer
changes:
- createTable:
tableName: post
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: text
type: varchar(255)
constraints:
nullable: false
- column:
name: created_at
type: timestamp
constraints:
nullable: false
defaultValueComputed: CURRENT_TIMESTAMP
When the Java Application will start, every script that has not been executed will be by Liquibase. In order to ensure the database integrity it will create 2 tables, DATABASECHANGELOG
and DATABASECHANGELOGLOCK
.
The main table is DATABASECHANGELOG, which records all the changes applied to the database. Each entry includes a field called MD5SUM — a unique signature for every file. If any file is modified in the future, Liquibase will detect the change by comparing the MD5SUM values. To maintain database integrity, it will throw an error if a discrepancy is found.
As mentioned above, Liquibase can also be used with other languages and structures. For more information, visit the official documentation: https://docs.liquibase.com/home.html