SpringBoot整合数据库版本管理工具flyway(入门)

 代码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;
扫描二维码关注公众号,回复: 6510457 查看本文章

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

猜你喜欢

转载自www.cnblogs.com/shileibrave/p/11031332.html