數據庫管理與遷移(Liquibase)


SpringBoot 是為了簡化 Spring 應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規范,引入相關的依賴就可以輕易的搭建出一個 WEB 工程

目前 Spring Boot 支持較好的兩款工具分別是 flywayliquibase,支持 sql script,在初始化數據源之后執行指定的腳本代碼或者腳本文件,本章基於 Liquibase

Liquibase

LiquiBase 是一個用於數據庫重構和遷移的開源工具,通過 changelog文件 的形式記錄數據庫的變更,然后執行 changelog文件 中的修改,將數據庫更新或回滾到一致的狀態。

主要特點

  • 支持幾乎所有主流的數據庫,如MySQL、PostgreSQL、Oracle、Sql Server、DB2等
  • 支持多開發者的協作維護;
  • 日志文件支持多種格式;如XML、YAML、SON、SQL等
  • 支持多種運行方式;如命令行、Spring 集成、Maven 插件、Gradle 插件等

在平時開發中,無可避免測試庫增加字段或者修改字段以及創建表之類的,環境切換的時候如果忘記修改數據庫那么肯定會出現 不可描述的事情 ,這個時候不妨考慮考慮Liquibase

官方文檔:http://www.liquibase.org/documentation/index.html

本章目標

利用 Spring Boot 集成 Liquibase,避免因粗心大意導致環境遷移時缺少字段….

導入依賴

依賴 spring-boot-starter-jdbc 目的是為了讓 liquibase 能夠獲得 datasource ,這里換成 mybatishibernate 等也是一樣,主要偷懶不想寫配置….

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencies>
<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>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
</dependencies>

屬性配置

只要依賴了 liquibase-core 默認可以不用做任何配置,但還是需要知道默認配置值是什么,這樣方便定位和解決問題

1
2
3
4
5
6
7
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/chapter23?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
# 只要依賴了 liquibase-core 默認可以不用做任何配置,但還是需要知道默認配置值是什么
# spring.liquibase.enabled=true
# spring.liquibase.change-log=classpath:/db/changelog/db.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文件

db.changelog-master.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
# 同時也支持依賴外部SQL文件(TODO 個人比較喜歡這種)
- changeSet:
id: 3
author: Levin
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/sqlfile/test1.sql

test1.sql

1
INSERT INTO `person` (`id`, `first_name`, `last_name`) VALUES ('2', '哈哈', '呵呵');

上面的yaml文件其實就是從下面的 XML 演變而來的,官方是支持 xmlyamljson 三種格式,寫法也比較簡單

傳送門(官方給出了三種寫法格式,依樣畫葫蘆就可以了):http://www.liquibase.org/documentation/changes/sql_file.html

1
2
3
4
5
6
7
8
<?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 id="1" author="Levin">
<sqlFile path="classpath:db/changelog/changelog/test1.sql"/>
</changeSet>
</databaseChangeLog>

主函數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.battcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
* @author Levin
*/
@SpringBootApplication
public class Chapter23Application {

public static void main(String[] args) {

SpringApplication.run(Chapter23Application.class, args);

}
}

測試

1.啟動Chapter23Application.java中的main方法

Liquibase啟動日志Liquibase啟動日志

從日志中可以看到Liquibase 在幫我們執行定義好的SQL,如果是第一次啟動,那么數據庫會存在databasechangelog 和 databasechangeloglock兩種表,從名字就可以看出,故而不作過多解釋

日志表日志表

2.SQL中的語法是創建一張person表和 兩次 INSERT 操作

SQL執行記錄SQL執行記錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM