java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot Liquibase数据库管理和迁移

SpringBoot整合Liquibase实现对数据库管理和迁移

作者:码农小伙

Liquibase是一个用于用于跟踪、管理和应用数据库变化的开源工具,通过日志文件(changelog)的形式记录数据库的变更(changeset),然后执行日志文件中的修改,将数据库更新或回滚(rollback)到一致的状态,本文主要介绍SpringBoot与Liquibase的集成,需要的朋友可以参考下

简介

Liquibase是一个用于用于跟踪、管理和应用数据库变化的开源工具,通过日志文件(changelog)的形式记录数据库的变更(changeset),然后执行日志文件中的修改,将数据库更新或回滚(rollback)到一致的状态。它的目标是提供一种数据库类型无关的解决方案,通过执行schema类型的文件来达到迁移。本文主要介绍SpringBoot与Liquibase的集成。

优点

简单示例

这里主要介绍基于SpringBoot集成liquibase来管理数据库的变更。

POM依赖

Maven 包的依赖,主要包含mysql驱动, JDBC(这里spring-boot-starter-data-jpa包含了jdbc包,当然直接引入jdbc包也行),以及liquibase包。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>
<dependency>
    <groupId>com.github.wenhao</groupId>
    <artifactId>jpa-spec</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>4.9.1</version>
</dependency>

yml配置

SpringBoot AutoConfig默认已经包含了对liquibase的配置,在spring.liquibase配置下。
基础的配置,可以直接使用如下(主要是指定change-log的位置,默认的位置是classpath:/db/changelog/db.changelog-master.yaml):

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test_db_liquibase?useSSL=false&autoReconnect=true&characterEncoding=utf8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: bfXa4Pt2lUUScy8jakXf
  liquibase:
    enabled: true
    # 如下配置是被spring.datasource赋值的,所以可以不配置
#    url: jdbc:mysql://localhost:3306/test_db_liquibase?useSSL=false&autoReconnect=true&characterEncoding=utf8
#    user: root
#    password: bfXa4Pt2lUUScy8jakXf
    change-log: classpath:/db/changelog/db.changelog-master.yaml

新增changelog

XML方式固然OK,不过依然推荐使用yml格式。

databaseChangeLog:
  - changeSet:
      id: 20220412-01
      author: pdai
      changes:
        - createTable:
            tableName: person
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: firstname
                  type: varchar(50)
              - column:
                  name: lastname
                  type: varchar(50)
                  constraints:
                    nullable: false
              - column:
                  name: state
                  type: char(2)

  - changeSet:
      id: 20220412-02
      author: pdai
      changes:
        - addColumn:
            tableName: person
            columns:
              - column:
                  name: username
                  type: varchar(8)

  - changeSet:
      id: 20220412-03
      author: pdai
      changes:
        - addLookupTable:
            existingTableName: person
            existingColumnName: state
            newTableName: state
            newColumnName: id
            newColumnDataType: char(2)

测试

启动springBootApplication, 我们可以看到如下的几个changeSet被依次执行

2022-04-12 20:41:20.591  INFO 8476 --- [           main] liquibase.lockservice                    : Successfully acquired change log lock
2022-04-12 20:41:20.737  INFO 8476 --- [           main] liquibase.changelog                      : Creating database history table with name: test_db_liquibase.DATABASECHANGELOG
2022-04-12 20:41:20.783  INFO 8476 --- [           main] liquibase.changelog                      : Reading from test_db_liquibase.DATABASECHANGELOG
Running Changeset: classpath:/db/changelog/db.changelog-master.yaml::20220412-01::pdai
2022-04-12 20:41:20.914  INFO 8476 --- [           main] liquibase.changelog                      : Table person created
2022-04-12 20:41:20.914  INFO 8476 --- [           main] liquibase.changelog                      : ChangeSet classpath:/db/changelog/db.changelog-master.yaml::20220412-01::pdai ran successfully in 53ms
Running Changeset: classpath:/db/changelog/db.changelog-master.yaml::20220412-02::pdai
2022-04-12 20:41:20.952  INFO 8476 --- [           main] liquibase.changelog                      : Columns username(varchar(8)) added to person
2022-04-12 20:41:20.952  INFO 8476 --- [           main] liquibase.changelog                      : ChangeSet classpath:/db/changelog/db.changelog-master.yaml::20220412-02::pdai ran successfully in 31ms
Running Changeset: classpath:/db/changelog/db.changelog-master.yaml::20220412-03::pdai
2022-04-12 20:41:21.351  INFO 8476 --- [           main] liquibase.changelog                      : Lookup table added for person.state
2022-04-12 20:41:21.351  INFO 8476 --- [           main] liquibase.changelog                      : ChangeSet classpath:/db/changelog/db.changelog-master.yaml::20220412-03::pdai ran successfully in 389ms
2022-04-12 20:41:21.382  INFO 8476 --- [           main] liquibase.lockservice                    : Successfully released change log lock

查看数据库,你会发现数据已经变更

那我们如果重新启动这个SpringBootApplication,会怎么呢?

很显然,因为databasechangelog表中已经有相关执行记录了,所以将不再执行变更

2022-04-12 20:49:01.566  INFO 9144 --- [           main] liquibase.lockservice                    : Successfully acquired change log lock
2022-04-12 20:49:01.761  INFO 9144 --- [           main] liquibase.changelog                      : Reading from test_db_liquibase.DATABASECHANGELOG
2022-04-12 20:49:01.812  INFO 9144 --- [           main] liquibase.lockservice                    : Successfully released change log lock

## 进一步理解

###  比较好的changelog的实践
简单而言:yml格式 + sql-file方式

执行sqlFile格式的changeSet,如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/857a01879d394aa2a4e920c25cc069e2.png)
执行的日志如下

```log
2022-04-12 21:00:28.198  INFO 17540 --- [           main] liquibase.lockservice                    : Successfully acquired change log lock
2022-04-12 21:00:28.398  INFO 17540 --- [           main] liquibase.changelog                      : Reading from test_db_liquibase.DATABASECHANGELOG
Running Changeset: classpath:/db/changelog/db.changelog-master.yaml::20220412-04::pdai
2022-04-12 21:00:28.516  INFO 17540 --- [           main] liquibase.changelog                      : SQL in file classpath:/db/changelog/db.changelog-20220412-04.sql executed
2022-04-12 21:00:28.516  INFO 17540 --- [           main] liquibase.changelog                      : ChangeSet classpath:/db/changelog/db.changelog-master.yaml::20220412-04::pdai ran successfully in 83ms
2022-04-12 21:00:28.532  INFO 17540 --- [           main] liquibase.lockservice                    : Successfully released change log lock

执行后,查看变更记录

数据表user表已经创建并插入一条数据

以上就是SpringBoot整合Liquibase实现对数据库管理和迁移的详细内容,更多关于SpringBoot Liquibase数据库管理和迁移的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文