Liquibase Overview
Liquibase It is an open source database change management tool for tracking, versioning, and managing changes in database structures (such as tables, fields, indexes, etc.). Its purpose is to make the database change process more transparent, controllable and automated, and prevent the development team from manually executing the same database change script in multiple environments.
Liquibase supports multiple databases (MySQL, PostgreSQL, Oracle, SQL Server, H2, etc.), and can define database changes through XML, YAML, JSON, or SQL files.
Main features
- Version control: Synchronize database changes with code to avoid manual changes to the database structure.
- Automated migration: Automatically apply database changes in different environments (development, testing, production, etc.).
- Rollbackability:Liquibase provides a rollback mechanism that can return to the previous database version.
- Supports multiple formats: Supports XML, YAML, JSON and other formats to describe changes.
- Convenient integration: Liquibase can be integrated into CI/CD processes or used with frameworks such as Spring Boot to easily manage database versions.
Working mechanism
Liquibase uses a file called changelog to describe all changes to the database. This file records all executed database change sets (changeSets). Each changeSet has a unique ID and author ID to track the change.
Liquibase will automatically manage database versions and changes through changelog files. It records which changes have been applied through a DATABASECHANGELOG table every time the change is applied.
Used in conjunction with SpringBoot
Since my project uses the xml method, I use the xml method to perform examples. For other methods, if you are interested, you can go to the official website to view the documents.
Portal:Liquibase Documentation
Introduce dependencies
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <url></url> <properties> <>UTF-8</> </properties> <dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>liquibase-core</artifactId> <version>4.21.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> <!-- Or use the appropriate version as needed --> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
Configuration File
spring: datasource: url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false username: root password: 123456 driver-class-name: hikari: maximum-pool-size: 10 liquibase: change-log: classpath:liquibase/platform/ enabled: true
Create a Liquibase changeset file
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="/xml/ns/dbchangelog" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/dbchangelog /xml/ns/dbchangelog/dbchangelog-3."> <include file="liquibase/platform/change/"/> </databaseChangeLog>
Specific change set files
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="/xml/ns/dbchangelog" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/dbchangelog /xml/ns/dbchangelog/dbchangelog-3."> <changeSet author="bob"> <createTable tableName="department"> <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> </databaseChangeLog>
Commonly used commands
liquibase update: Apply all unexecuted database changes.
liquibase rollback: Roll back the database to the specified changeSet or version.
liquibase status: Check the current database change status.
liquibase generateChangeLog: Generate the initial changeLog file from the existing database.
Summarize
Liquibase is a powerful database management tool that helps you simplify database migration in development by automatically managing database changes, versioning, and rollbacks. By integrating Liquibase in Spring Boot, database structures and versions can be managed more efficiently, ensuring smoother collaboration between development teams. In the project, Liquibase can be used in conjunction with version control tools such as Git to ensure transparency and traceability of database structure changes.
This is the article about Liquibase combined with SpringBoot to implement database management. For more related SpringBoot database management content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!