SpringBoot集成Flyway進行數據庫版本遷移管理的步驟
Flyway簡介
Flyway中的遷移(migrations)模式Flyway對數據庫的所有更改都稱為 migrations(遷移) 。 migrations(遷移) 分為版本控制(Versioned)遷移與可重復(Repeatable)的遷移兩種,
而版本控制又分為regular(常規)和undo(撤銷)兩種形式。
常規版本控制例子:
CREATE TABLE car ( id INT NOT NULL PRIMARY KEY, license_plate VARCHAR NOT NULL, color VARCHAR NOT NULL);ALTER TABLE owner ADD driver_license_id VARCHAR;INSERT INTO brand (name) VALUES (’DeLorean’);
撤消(undo)遷移與常規版本遷移相反,但形式上看也是版本遷移的一種,具有相同版本的版本遷移影響。針對上例中的撤銷遷移例子如下:
DELETE FROM brand WHERE name=’DeLorean’;ALTER TABLE owner DROP driver_license_id;DROP TABLE car;
可重復遷移:只有描述、校驗和(checksum),沒有版本號??芍貜瓦w移不止執行一次,每次校驗和(checksum)發生變更時就會被執行。
可重復遷移主要有以下兩種用途:
在一次遷移運行中,在所有版本遷移執行之后,可重復遷移在最后執行??芍貜瓦w移按照它們描述的順序執行。
Flyway遷移的命名模式如下:
Flaway的版本命名十分靈活,以下版本都是有效的迭代版本:
1 001 5.2 1.2.3.4.5.6.7.8.9 205.68 20130115113556 2013.1.15.11.35.56 2013.01.15.11.35.56Flyway歷史記錄表flyway_history_schema
Flyway通過創建表flyway_history_schema來記錄遷移,表中的主要字段如下:
Spring Boot集成示例
0.添加所需依賴<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring-boot-dependencies已包含了flyway的版本設置 --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>1.application.yml配置數據庫連接與sql腳本所在目錄(不配則默認為classpath:db/migration)
spring: datasource: url: jdbc:mysql://localhost:3306/spring_boot_series?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT username: root password: tiger driver-class-name: com.mysql.cj.jdbc.Driver # 自動讀取spring.datasource配置進行遷移操作 flyway: locations: classpath:db2.添加初始化sql文件 V1.0__Base_DDL.sql 到配置目錄下(前綴須以大寫V為前綴,可通過sql-migration-prefix配置更改)
CREATE SCHEMA IF NOT EXISTS `spring_boot_series` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `spring_boot_series`;-- ------------------------------------------------------- Table `spring_boot_series`.`user_base`-- -----------------------------------------------------CREATE TABLE IF NOT EXISTS `spring_boot_series`.`user_base`( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `nickname` VARCHAR(45) NOT NULL DEFAULT ’’ COMMENT ’昵稱’, `status` VARCHAR(45) NOT NULL, `sex` TINYINT NULL COMMENT ’性別(MALE:1-男性,FEMALE:0-女性)’, `mobile` VARCHAR(13) NOT NULL DEFAULT ’’ COMMENT ’手機號碼’, `email` VARCHAR(100) NOT NULL DEFAULT ’’ COMMENT ’郵箱’, `birthday` BIGINT NOT NULL DEFAULT 0 COMMENT ’生日’, `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE = InnoDB;CREATE TABLE IF NOT EXISTS `spring_boot_series`.`user_auth`( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uid` BIGINT NOT NULL, `identity_type` TINYINT NOT NULL, `identifier` VARCHAR(50) NOT NULL DEFAULT ’’, `certificate` VARCHAR(30) NOT NULL DEFAULT ’’, `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), INDEX `idx_uid_certificate` (`uid` ASC, `certificate` ASC), INDEX `idx_type_identity` (`identity_type` ASC, `identifier` ASC)) ENGINE = InnoDB COMMENT = ’用戶授權表’;3.添加版本文件
V1.0.1__Modify_user_birthday.sql:
alter table user_base modify birthday datetime null comment ’生日’;
V1.0.2__Modify_user_birthday.sql:
alter table user_base modify birthday timestamp null comment ’生日’;alter table user_base add login_time timestamp null;4.啟動SpringBoot應用,以下為數據庫為空SpringBoot應用運行時的執行輸出
由于配置連接的數據庫里沒有任何表,所以SpringBoot應用運行時flyway會根據腳本進行數據庫的初始化,執行的順序會根據腳本版本號由低到高按順序執行(1.0 -> 1.0.1 -> 1.0.2),每執行完一個版本腳本都會向記錄表flyway_history_schema插入一條數據。
若添加了新的版本腳本,應用啟動時flyway會根據腳本文件版本到記錄表flyway_history_schema查詢是否有對應的版本腳本被執行了,如果沒有相應的版本腳本記錄才會執行腳本。
參考
官網文檔
示例地址
以上就是SpringBoot集成Flyway進行數據庫版本遷移管理的步驟的詳細內容,更多關于SpringBoot集成Flyway的資料請關注軟科小院其它相關文章!