Liquibase+SpringBoot的简单使用笔记!update+rollback

  • 2020 年 11 月 5 日
  • 笔记

该笔记记录了springboot整合liquibase之后,如何根据liquibase ChangeLogFile对数据库进行修改以及回滚操作

参考:
baeldung.com
JHipster

1. 利用changeLog文件对数据库进行修改

  1. 引入liquibase依赖
  2. 在resource目录下新建db.changelog-master.xml作为变更集文件
  3. 修改application加入liquibase的配置,主要配置变更集文件位置
  4. 运行项目,即可根据变更集文件的内容对数据库进行修改

master.xml简单格式:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="//www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="//www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="//www.liquibase.org/xml/ns/pro" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//www.liquibase.org/xml/ns/dbchangelog-ext //www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd //www.liquibase.org/xml/ns/pro //www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd //www.liquibase.org/xml/ns/dbchangelog //www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
    <changeSet author="chunmiaoz (generated)" id="1604474279717-1">
        <createTable tableName="user">
            <column name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="phone" type="VARCHAR(11)"/>
            <column name="username" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

2. 从现有数据库表生成changeLog文件

  1. 在数据库中建表
  2. 新建一个liquibase.properties并配置数据库地址、用户名密码、驱动
  3. 在有liquibase.properties的目录下打开cmd,输入命令
liquibase --changeLogFile=dbchangelog.xml generateChangeLog

会自动生成一个dbchangelog.xml的目标文件
Liquibase可根据目标文件后缀名生成对应类型的变更集文件,如.yaml会生成yaml格式的

liquibase.properties:

# Enter the path for your changelog file.
changeLogFile=dbchangelog.xml

#### Enter the Target database 'url' information  ####
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true

# Enter the username for your Target database.
username: root

# Enter the password for your Target database.
password: 123456

3. 从使用JPA导出的jar包生成changeLog文件

  1. 新建一个项目,引入jpa依赖
  2. 修改application,配置jpa的ddl-auto属性为create
  3. 新建实体类
  4. 将项目打包成jar文件
  5. 在jar文件目录下,新建liquibase.properties并配置相应的属性
  6. 在该目录打开cmd,输入命令
Liquibase --logLevel=INFO --defaultsFile=liquibase.properties generateChangeLog  

即可自动生成dbchangelog.xml文件

Liquibase.properties:

# jar包地址
classpath=liquibase-demo-0.0.1-SNAPSHOT.jar

# Enter the path for your changelog file.
changeLogFile=dbchangelog.xml

#### Enter the Target database 'url' information  ####
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true

# Enter the username for your Target database.
username: root

# Enter the password for your Target database.
password: 123456

4. 回滚Liquibase生成的数据库

利用liquibase的命令

liquibase update

生成的数据库表单,可以利用命令使数据库回滚

liquibase rollbackCount 1 //回滚一条记录
liquibase rollbackToDate YYYY-MM-DD HH:MM:SS //回滚到指定日期

5. Springboot集成的liquibase回滚操作

通过借鉴jhipster的liquibase配置结构完成此功能
前提:liquibase回滚时,要求databasechangelog和更改集文件一致。

  1. 在项目中,resource目录下配置master.xml主变更集文件,目的是将子更改集include进来
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="//www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="//www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="//www.liquibase.org/xml/ns/pro" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//www.liquibase.org/xml/ns/dbchangelog-ext //www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd //www.liquibase.org/xml/ns/pro //www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd //www.liquibase.org/xml/ns/dbchangelog //www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
    <include file="db/changelog/202011051610-added-person.xml" relativeToChangelogFile="false"></include>
</databaseChangeLog>
  1. 新建202011051610-added-person.xml子变更集文件,位置为src\main\resources\db\changelog\db.changelog-master.xml
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="//www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="//www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="//www.liquibase.org/xml/ns/pro" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//www.liquibase.org/xml/ns/dbchangelog-ext //www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd //www.liquibase.org/xml/ns/pro //www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd //www.liquibase.org/xml/ns/dbchangelog //www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
    <changeSet author="chunmiaoz (generated)" id="1604474279717-1">
        <createTable tableName="user">
            <column name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="phone" type="VARCHAR(11)"/>
            <column name="username" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet id="202011041617-added-person" author="Chunmiao">
        <createTable tableName="person">
            <column name="id" type="INT">
                <constraints primaryKey="true" nullable="false" unique="true" ></constraints>
            </column>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="false"></constraints>
            </column>
            <column name="address" type="VARCHAR(255)"></column>
            <column name="city" type="VARCHAR(255)"></column>
        </createTable>
    </changeSet>

    <changeSet id="202011041621-added-company" author="Chunmiao">
        <createTable tableName="company">
            <column name="id" type="INT">
                <constraints primaryKey="true" nullable="false" unique="true"></constraints>
            </column>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="false"></constraints>
            </column>
            <column name="address" type="VARCHAR(255)"></column>
            <column name="city" type="VARCHAR(255)"></column>
        </createTable>
    </changeSet>

    <changeSet id="202011041621-alert-person" author="Chunmiao">
        <addColumn tableName="person">
            <column name="country" type="VARCHAR(2)">
            </column>
        </addColumn>

        <rollback>
            <dropColumn tableName="person" columnName="country"></dropColumn>
        </rollback>
    </changeSet>
</databaseChangeLog>
  1. 修改pom.xml,为maven增加liquibase插件
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <configuration>
                    <changeLogFile>${basedir}/src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
                    <url>jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8</url>
                    <username>root</username>
                    <password>123456</password>
                </configuration>
            </plugin>
        </plugins>
  1. 运行项目,可以发现liquibase会自动创建数据库表
  2. 执行回滚操作,在命令行中利用maven命令
    mvn liquibase:rollback -Dliquibase.rollbackCount=1
  3. 利用maven执行变更集任务可以使用命令
    mvn liquibase:update

已知问题:
在springBoot中,由于liquibase update是由springBoot完成的,手动在命令行中对该changeLog执行回滚操作,虽然会提示成功,但是数据库中的数据实际没有发生回滚。

原因:
springBoot中changelogFile位置为classpath:db/changelog/db.changelog-master.xml
而在命令行中,无法将文件位置表示为classpath:的形式,文件名字不同会被liquibase认为是不同的版本管理

解决方法:
将changeSet定义在子变更集中,在主变更集中引入子变更集,这样不管在什么环境里,changeSet的路径都被表示为在主变更集中配置的一样,从而避免了路径不同的冲突