跳转至

Liquibase框架支持

Liquibase框架支持

本文档描述了 SeaboxSQL 数据库对于Liquibase工具的支持情况和配置说明,面向所有使用SeaboxSQL数据库的用户,主要是数据库管理员和应用程序开发人员。

有关 Liquibase 的更多信息,请参阅以下资源:Liquibase Docs

Liquibase概述

Liquibase 是一个用于跟踪、管理和应用数据库变化的开源的数据库重构工具。它将数据库的所有变化(包括结构和数据)都保存在日志文件中,便于版本控制。

Liquibase 使参与应用程序发布过程的任何人都可以轻松的完成:

• 消除发布数据库时的错误和延迟。

• 部署和回滚特定版本的更改,而无需知道自己部署的内容。

• 将数据库和应用程序更改一起部署,以便它们始终保持同步。

日志文件支持多种格式,如 XML,YAML,JSON,SQL 等。

支持多种运行方式,如命令行、Spring 集成、Maven 插件、Gradle 插件等。

Liquibase 配置说明

以 Spring Boot 集成 Liquibase 为例:

pom.xm文件引入Liquibase

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

配置changelog

在 classpath:/db/changelog 中声明一个 db.changelog-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.8.xsd">
<include file="classpath:db/changelog/init-table.xml" relativeToChangelogFile="false" />
<include file="classpath:db/changelog/init-data.xml" relativeToChangelogFile="false" />
</databaseChangeLog>

在 classpath:/db/changelog 中声明一个 init-table.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.8.xsd">
<changeSet id="2" author="test">
<insert tableName="person">
<column name="name" value="init"></column>
<column name="date" value="2010-01-01"></column>
<column name="sex" value="false"></column>
<column name="blob" value="blob"></column>
<column name="clob" value="clob"></column>
</insert>
</changeSet>
</databaseChangeLog>

在 classpath:/db/changelog 中声明一个 init-data.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.8.xsd">
<changeSet id="2" author="test">
<insert tableName="person">
<column name="name" value="init"></column>
<column name="date" value="2010-01-01"></column>
<column name="sex" value="false"></column>
<column name="blob" value="blob"></column>
<column name="clob" value="clob"></column>
</insert>
</changeSet>
</databaseChangeLog>

在 application.properties 中指定要加载的 changelog 文件:

spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml

指定pg驱动(liquibase需要指定postgresql驱动包)

spring.datasource.driver-class-name:org.postgresql.Driver
spring.datasource.url:jdbc:postgresql://ip:端口/库名
spring.datasource.username:用户名
spring.datasource.password:密码

在启动项目时,会进行自动加载

自动加载

在对应数据库中查询databasechangelog表会看到xml文件中sql和数据的变更信息

数据查询

Liquibase 注意点

  1. 适配 Liquibase 需使用 pg 形态的驱动,即直接使用 pg 的驱动类(org.postgresql.Driver)和连接串(jdbc:postgresql://localhost:54321/test);

  2. Liquibase 创建包含 blob 和 clob 类型的表时,需要使用 标签定义 sql 语句,不能使用 liquibase提供的列属性 type 指定列类型,否则 blob 会被映射为 oid 类型,clob 会被映射为 text 类型。