代碼Demo地址:https://github.com/shileishmily/spring-boot-jooq-demo.git
Flyway是什么
Flyway是一款開源的數據庫版本管理工具,Flyway可以獨立於應用實現管理並跟蹤數據庫的變更,Flyway根據自己的約定,不需要復雜的配置就可以實現數據的Migrate。Migrations可以寫成SQL腳本,也可以寫在Java代碼中,Flyway還支持Spring Boot。
如果你和我一樣,有開發環境,測試環境,RC環境,生產環境,還有為某些渠道商戶定制搭建的環境。那么哪怕是增加一個字段,你都必須在各個環境執行一遍。如何改動較大,
比如某一個開發版本增加了10張表,修改了N表的注釋,字段長度等等等等。想想頭就大了,就算天天想,時時想。最后上生產環境還是會有遺漏。
現在有了flyway,一切變得輕松。
1、添加gradle依賴
buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("org.flywaydb:flyway-gradle-plugin:5.0.7") } } apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.flywaydb.flyway' apply plugin: 'org.springframework.boot' dependencies { compile group: 'org.flywaydb', name: 'flyway-maven-plugin', version: '5.2.4' compile group: 'org.flywaydb', name: 'flyway-core', version: '5.0.7' }
2、application.yml配置
spring: profiles: active: dev aop: auto: true proxy-target-class: true datasource: url: jdbc:mysql://localhost:3306/jooq_test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10 username: root password: 111 driver-class-name: com.mysql.jdbc.Driver jooq: sql-dialect: mysql flyway: clean-disabled: true #禁用clean操作 enabled: true #使flyway生效 baseline-on-migrate: true #初始化時如果不存在遷移記錄表,默認新建一個 out-of-order: true #防止開發環境下漏掉沒來得及apply的文件,產品環境最好設為false locations: classpath:/db/migration
3、/db/migration/V1__init_database.sql文件,新增一張表sys_log
CREATE TABLE `sys_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip_address` varchar(50) DEFAULT NULL COMMENT 'ip地址', `oper_id` int(11) DEFAULT NULL COMMENT '操作人ID', `user_name` varchar(50) DEFAULT NULL COMMENT '用戶名', `module_name` varchar(50) DEFAULT NULL COMMENT '模塊名稱', `method_name` varchar(50) DEFAULT NULL COMMENT '方法名', `method_desc` varchar(100) DEFAULT NULL COMMENT '方法描述', `oper_content` varchar(6000) DEFAULT NULL COMMENT '操作內容', `create_time` datetime DEFAULT NULL COMMENT '創建時間', `app_id` int(11) DEFAULT NULL COMMENT '應用ID', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4、首次執行需要在數據庫實例jooq_test創建表flyway_schema_history,flyway_schema_history是flyway版本控制記錄表,必須要創建。
CREATE TABLE `flyway_schema_history` ( `installed_rank` int(11) NOT NULL, `version` varchar(50) DEFAULT NULL, `description` varchar(200) NOT NULL, `type` varchar(20) NOT NULL, `script` varchar(1000) NOT NULL, `checksum` int(11) DEFAULT NULL, `installed_by` varchar(100) NOT NULL, `installed_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `execution_time` int(11) NOT NULL, `success` tinyint(1) NOT NULL, PRIMARY KEY (`installed_rank`), KEY `flyway_schema_history_s_idx` (`success`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
5、啟動SpringBoot程序JooqApplication,啟動時會自動執行數據庫腳本文件。
package com.suixingpay.jooq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @EnableTransactionManagement @SpringBootApplication(scanBasePackages = {"com.suixingpay.jooq"}) public class JooqApplication { public static void main(String[] args) { SpringApplication.run(JooqApplication.class, args); } }
6、登錄數據庫查詢發現多了兩張表
sys_log是我們在V1__init_database.sql中新增的表;
flyway_schema_history是flyway版本控制記錄表;
查詢表flyway_schema_history,可以看到剛才執行V1__init_database.sql腳本的記錄已經有了。
代碼Demo地址:https://github.com/shileishmily/spring-boot-jooq-demo.git