第三篇:SpringBoot - 數據庫結構版本管理與遷移


SpringBoot支持了兩種數據庫結構版本管理與遷移,一個是flyway,一個是liquibase。其本身也支持sql script,在初始化數據源之后執行指定的腳本,本章是基於 Liquibase 開展…

- Liquibase

開發人員將本地開發機器上的基於文本的文件中的數據庫更改存儲在本地數據庫中。Changelog文件可以任意嵌套,以便更好地管理,每個變更集通常包含描述應用於數據庫的更改/重構的更改。Liquibase支持對支持的數據庫和原始SQL生成SQL的描述性更改。通常,每個變更集應該只有一個更改,以避免可能導致數據庫處於意外狀態的自動提交語句失敗。

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

- 使用

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

- pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <groupId>com.battcn</groupId>
    <artifactId>battcn-boot-liquibase</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <mybatis-plugin.version>1.1.1</mybatis-plugin.version>
        <mybatis-spring-boot.version>1.3.0</mybatis-spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 數據庫連接池,類似阿里 druid  -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot.version}</version>
        </dependency>
        <!-- liquibase  -->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
            <!--<scope>runtime</scope>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

- application.yml

spring:
  application:
    name: battcn-boot-liquibase
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/liquibase?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
#我這里使用的是默認路徑,如果默認路徑其實可以不配置,有興趣可以看 LiquibaseProperties 的源代碼
liquibase:
  change-log: classpath:db/changelog/db.changelog-master.yaml
  • liquibase.change-log 配置文件的路徑,默認值為classpath:/db/changelog/db.changelog-master.yaml
  • liquibase.check-change-log-location 是否堅持change log的位置是否存在,默認為true.
  • liquibase.contexts 逗號分隔的運行時context列表.
  • liquibase.default-schema 默認的schema.
  • liquibase.drop-first 是否首先drop schema,默認為false
  • liquibase.enabled 是否開啟liquibase,默認為true.
  • liquibase.password 目標數據庫密碼
  • liquibase.url 要遷移的JDBC URL,如果沒有指定的話,將使用配置的主數據源.
  • liquibase.user 目標數據用戶名

- db.changelog-master.yaml

databaseChangeLog:
  - 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

  - changeSet:
      id: 3
      author: Levin
      changes:
        - sqlFile:
            encoding: utf8
            path: classpath:db/changelog/sqlfile/test1.sql

- test1.sql

INSERT INTO `person` (`id`, `first_name`, `last_name`) VALUES ('2', '嘻嘻', '諤諤');

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

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

<?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>

- Application.java

package com.battcn;

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

/**
 * @author Levin
 * @date 2017-08-19.
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

- 測試

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

Liquibase啟動日志

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

日志表

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

SQL執行記錄

 


免責聲明!

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



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