1、LiquiBase简介

LiquiBase是一个用于数据库重构和迁移的开源工具,通过日志文件的形式记录数据库的变更,然后执行日志文件中的修改,将数据库更新或回滚到一致的状态。LiquiBase的主要特点有:

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

2、日志文件changeLog

changelog是LiquiBase用来记录数据库的变更,一般放在CLASSPATH下,然后配置到执行路径中。

changelog支持多种格式,主要有XML/JSON/YAML/SQL,推荐使用xml格式。官网格式

一个标签对应一个变更集,由属性id、name,以及changelog的文件路径唯一标识。

changelog在执行的时候并不是按照id的顺序,而是按照changeSet在changelog中出现的顺序。

changelog中的一个changeSet对应一个事务,在changeSet执行完后commit,如果出现错误则rollback。

3、简单入门使用

1)在application.properties中配置changeLog路径

# liquibase配置
liquibase.enabled=true
#默认位置
#liquibase.change-log=classpath:/db/changelog/db.changelog-master.yaml liquibase.change-log=classpath:/db/changelog/sqlData.xml

2)xml配置sample

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-2.0.xsd"> <changeSet author="yejg" id="sql-01">
<sqlFile path="classpath:db/changelog/sqlfile/init.sql" encoding="UTF-8" />
<sqlFile path="classpath:db/changelog/sqlfile/users.sql" encoding="UTF-8" />
</changeSet> <changeSet author="yejg" id="sql-02">
<sqlFile path="classpath:db/changelog/sqlfile/users2.sql" encoding="UTF-8" />
</changeSet> </databaseChangeLog>

3)要执行的sql

--- init.sql
CREATE TABLE usersTest(
user_id varchar2(14) DEFAULT ' ' NOT NULL,
user_name varchar2(128) DEFAULT ' ' NOT NULL
) STORAGE(FREELISTS 20 FREELIST GROUPS 2) NOLOGGING TABLESPACE USER_DATA; insert into usersTest(user_id,user_name) values ('0','测试');

4)启动项目时,日志效果

2019-02-19 15:15:22,425[INFO][main][][][] {dataSource-1} inited [com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:721)]
2019-02-19 15:15:23,065[INFO][main][][][] Could not set remarks reporting on OracleDatabase: com.alibaba.druid.pool.DruidPooledConnection.setRemarksReporting(boolean) [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:24,036[INFO][main][][][] Successfully acquired change log lock [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,469[INFO][main][][][] Creating database history table with name: DATABASECHANGELOG [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,496[INFO][main][][][] Reading from DATABASECHANGELOG [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,574[INFO][main][][][] classpath:/db/changelog/sqlData.xml: classpath:/db/changelog/sqlData.xml::sql-01::yejg: SQL in file classpath:db/changelog/sqlfile/init.sql executed [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,581[INFO][main][][][] classpath:/db/changelog/sqlData.xml: classpath:/db/changelog/sqlData.xml::sql-01::yejg: SQL in file classpath:db/changelog/sqlfile/users.sql executed [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,586[INFO][main][][][] classpath:/db/changelog/sqlData.xml: classpath:/db/changelog/sqlData.xml::sql-01::yejg: ChangeSet classpath:/db/changelog/sqlData.xml::sql-01::yejg ran successfully in 58ms [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,624[INFO][main][][][] classpath:/db/changelog/sqlData.xml: classpath:/db/changelog/sqlData.xml::sql-02::yejg: SQL in file classpath:db/changelog/sqlfile/users2.sql executed [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,627[INFO][main][][][] classpath:/db/changelog/sqlData.xml: classpath:/db/changelog/sqlData.xml::sql-02::yejg: ChangeSet classpath:/db/changelog/sqlData.xml::sql-02::yejg ran successfully in 10ms [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]
2019-02-19 15:15:25,649[INFO][main][][][] Successfully released change log lock [org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger.info(CommonsLoggingLiquibaseLogger.java:92)]

5)简单原理分析

在执行changelog的时候,liquibase会在数据库中新建2张表,写执行记录:

  • databasechangelog
  • databasechangeloglock

6)生成已有数据库的changeLog

首先,在pom文件中增加如下配置

<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
<propertyFileWillOverride>true</propertyFileWillOverride>
<!--生成文件的路径-->
<outputChangeLogFile>src/main/resources/changelog_dev.xml</outputChangeLogFile>
</configuration>
</plugin>
</plugins>
</build>

其中,liquibase.properties的配置如下

changeLogFile=src/main/resources/db/changelog/sqlData.xml
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@XXXX
username=XXX
password=XXX
verbose=true
## 生成文件的路径
outputChangeLogFile=src/main/resources/changelog_dev.xml

然后,执行【mvn liquibase:generateChangeLog】命令,就会生成changelog_dev.xml文件

4、其他应用

为减少部署实施的工作量,可以利用liquibase在项目中配置好sql和changeLog,让项目启动的时候,就自动去打sql脚本,实现自动化处理。

不过,目前发现liquibase不支持执行语句块,暂未找到解决办法。

Liquibase使用入门的更多相关文章

  1. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  2. liquibase之快速入门

    第一步: 创建一个Changelog File: 这个database  Changelog file列举了数据库中所有的改变情况,该文件是以xml为基础的,下面是一个空的xml文件: <?xm ...

  3. MySQL 菜鸟入门“秘籍”

    一.MySQL简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不 ...

  4. springboot简单入门笔记

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

  5. MySQL菜鸟入门“秘籍”

    一.MySQL简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不 ...

  6. 【转载】Spring boot学习记录(一)-入门篇

    前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 首先声明,Spring Boot不是一门新技术.从本质上来说,Spring B ...

  7. 【01】SpringBoot2核心技术-基础入门

    SpringBoot 2 1. SpringBoot2核心技术-基础入门 01 Spring与SpringBoot 1.Spring 能做什么 1.1 Spring的能力 微服务:将一个应用的所有功能 ...

  8. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  9. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

随机推荐

  1. SPI通信协议(非原创,转载他人,用于学习)

    SPI通信协议:1.SPI主从模式:2.数据信号的相位与极性:3.数据帧的格式. 一.什么是SPI? SPI是串行外设接口(Serial Peripheral Interface)的缩写.是 Moto ...

  2. Beta冲刺 (3/7)

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 熟悉并编写小程序的自定义控件 编辑文 ...

  3. 请教:WCF速度似乎比Remoting慢

    两段极为相似的代码,主要想看看过输与序列化过程两者的用时差异,结果10000次的调用,WCF用了11秒多,remoting用了5秒不到!这是测试的源代码 Remoting的服务端 public cla ...

  4. 用react + redux + router写一个todo

    概述 最近学习redux,打算用redux + router写了一个todo.记录下来,供以后开发时参考,相信对其他人也有用. 注意: 我只实现了Footer组件的router,其它组件的实现方法是类 ...

  5. 处理返回键劫持(结合vue)

    在这里记录一下近期解决的一个问题 需求,在某个页面,浏览器返回按钮点击的时候,不能走浏览器的默认返回操作,而是要走自己的逻辑, 比如跳转页面等等. 那么问题来了,如何去不走默认返回呢.经过网上搜罗和同 ...

  6. Testing - 软件测试知识梳理 - 基础概念

    测试是为了度量和提高被测试软件的质量,对测试软件进行工程设计.实施.维护的的整个生命周期过程. 仅仅发现Bug是测试的初步,而分析出根本原因推动问题的解决,却要有很深的功底. 不同的测试岗位从事不同的 ...

  7. python 打印堆栈信息方法

    第一种方法使用logging模块 import logging def test(self): try: 1 / 0 # 触发异常 except BaseException as e: logging ...

  8. windows中的常用Dos命令

    # __切换盘符目录__ E/D: # 从C盘切换到E盘或者D盘# __切换到指定文件夹下__cd folder_name(指定文件夹名--相对/绝对路径)cd .. # 返回上一级目录cd / # ...

  9. Base 64 加密、解密

    1.写一个公共类 package com.boyutec.oss.sys.utils; import java.io.UnsupportedEncodingException; import java ...

  10. 剑指offer【01】- 二维数组中的查找(Java)

    在经历了春招各大公司的笔试题和面试官的血虐之后,决定要刷一些算法题了,不然连面试机会都没有. 而应对笔试和面试,比较出名的就是剑指offer的题目和LeetCode的题目了.剑指offer应对面试中的 ...