SpringBoot学习笔记(十二:Liquibase )




一、Liquibase 简介


在这里插入图片描述

LiquiBase(从 2006 年开始投入使用)是一种免费开源的工具,可以实现不同数据库版本之间的迁移。本质上是一个执行sql脚本的工具。

1、主要特点:

  • 支持几乎所有主流的数据库,如MySQL、PostgreSQL、Oracle、Sql Server、DB2等
    • 支持多开发者的协作维护;
    • 日志文件支持多种格式;如XML、YAML、SON、SQL等
    • 支持多种运行方式;如命令行、Spring 集成、Maven 插件、Gradle 插件等

2、使用方式

要开始使用 LiquiBase,需要以下四个步骤:

  • 创建一个数据库 变更日志(change log)文件。
  • 在变更日志文件内部创建一个 变更集(change set)。
  • 通过命令行或构建脚本对数据库运行变更集。
  • 检验数据库中的变更。

3、变更集合

随着新特性添加到了应用程序中,经常需要变更数据库的结构或修改表约束。LiquiBase 提了超过 30 种数据库重构支持。

例如:

  • 添加列
<changeSet id="4" author="joe"> 
 <addColumn tableName="distributor"> 
   <column name="phonenumber" type="varchar(255)"/> 
 </addColumn> 
</changeSet>
  • 创建表
<changeSet id="3" author="betsey"> 
 <createTable tableName="distributor"> 
   <column name="id" type="int"> 
     <constraints primaryKey="true" nullable="false"/> 
   </column> 
   <column name="name" type="varchar(255)"> 
     <constraints nullable="false"/> 
   </column> 
   <column name="address" type="varchar(255)"> 
     <constraints nullable="true"/> 
   </column> 
   <column name="active" type="boolean" defaultValue="1"/> 
 </createTable> 
</changeSet>

二、SpringBoot整合Liquibase


1、依赖

springboot内置了对liquibase整合的支持,只需要在项目中引入liquibase的依赖,进行配置即可:

            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

依赖 spring-boot-starter-jdbc 目的是为了让 liquibase 能够获得 datasource。


2、配置

appllication.properties:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/liquibase_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
# spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:/liquibase/master.xml
#  spring.liquibase.change-log=classpath:/liquibase/changelog-master.yaml

更多配置:

  • spring.liquibase.change-log 配置文件的路径,默认值为 classpath:/db/changelog/db.changelog-master.yaml
  • spring.liquibase.check-change-log-location 检查 change log的位置是否存在,默认为true.
  • spring.liquibase.contexts 用逗号分隔的运行环境列表。
  • spring.liquibase.default-schema 默认数据库 schema
  • spring.liquibase.drop-first 是否先 drop schema(默认 false)
  • spring.liquibase.enabled 是否开启 liquibase(默认为 true)
  • spring.liquibase.password 数据库密码
  • spring.liquibase.url 要迁移的JDBC URL,如果没有指定的话,将使用配置的主数据源.
  • spring.liquibase.user 数据用户名
  • spring.liquibase.rollback-file 执行更新时写入回滚的 SQL文件

2、master.xml

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <include file="classpath:liquibase/change_log/test.xml" relativeToChangelogFile="false"/>

</databaseChangeLog>

官方是支持 xml,yaml,json 三种格,这里也可以写成yaml格式或者json格式,配置文件里修改 spring.liquibase.change-log 即可。官方对比: https://www.liquibase.org/documentation/changes/sql_file.html

databaseChangeLog:
  # 支持 yaml 格式的 SQL 语法
  - changeSet:
      id: 1
      author: Levin
      changes:
        - createTable:
            tableName: person
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: first_name
                  type: varchar(255)
                  constraints:
                    nullable: false
              - column:
                  name: last_name
                  type: varchar(255)
                  constraints:
                    nullable: false

  - changeSet:
      id: 2
      author: Levin
      changes:
        - insert:
            tableName: person
            columns:
              - column:
                  name: first_name
                  value: Marcel
              - column:
                  name: last_name
                  value: Overdijk


3、test.xml

在test.xml中写了建表的集合:

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
    <property name="autoIncrement" value="true" dbms="mysql"/>
    <changeSet id="init-schema" author="tianshouzhi" >
        <comment>init schema</comment>
        <createTable tableName="user">
            <column name="id" type="bigint" autoIncrement="${autoIncrement}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="nick_name" type="varchar(255)">
                <constraints  nullable="false"/>
            </column>
            <column name="email" type="varchar(255)">
                <constraints  nullable="false"/>
            </column>
            <column name="register_time" type="timestamp"  defaultValueComputed="CURRENT_TIMESTAMP">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci"/>
        </modifySql>
    </changeSet>
</databaseChangeLog>

4、运行效果

运行主类,从日志中可以看到Liquibase 在帮我们执行定义好的SQL,如果是第一次启动,那么数据库会存在databasechangelog 和 databasechangeloglock两个表

在这里插入图片描述


test.xml中的sql是创建了一个user表:

在这里插入图片描述




本文为学习笔记类博客,学习资料来源见参考!



参考:

【1】:数据库版本管理工具Liquibase
【2】:让开发自动化实现自动化数据库迁移
【3】:3.5 springboot与liquibase整合
【4】:Liquibase官方文档
【5】:一起来学SpringBoot | 第二十四篇:数据库管理与迁移(Liquibase)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三分恶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值