SpringBoot 整合flyway

前言:本文章专用于因版本问题导致springboot整合flyway不成功无法自动迁移的情况

【pom.xml】

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>3.0</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.0.M4</version>
</dependency>

【启动类】

@SpringBootApplication(exclude = {FlywayAutoConfiguration.class})
public class App {
    public static void main(String[] args) {
        ThreadUtil.execAsync(new FlywayRunner());
        SpringApplication.run(App.class, args);
    }
}

【配置属性类】

/**
 * @author JHL
 * @version 1.0
 * @date 2022/8/19 12:20
 * @since : JDK 11
 */
public class FlywayProperties {

    public static final String PREFIX = "flyway";

    private Boolean enable = false;
    /**
     * DataSource
     */
    private String url;
    private String username;
    private String password;
    /**
     * sql脚本文件路径
     */
    private String flywayScriptLocation = "classpath:db/migration/";
    /**
     * 自动迁移
     */
    private Boolean flywayAutoMigration = true;
    /**
     * 发生错误的时候清理表
     */
    private Boolean cleanOnValidationError = true;
    /**
     * 结束时间自动清理表
     */
    private Boolean autoCleanTable = false;


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getCleanOnValidationError() {
        return cleanOnValidationError;
    }

    public void setCleanOnValidationError(Boolean cleanOnValidationError) {
        this.cleanOnValidationError = cleanOnValidationError;
    }

    public Boolean getAutoCleanTable() {
        return autoCleanTable;
    }

    public void setAutoCleanTable(Boolean autoCleanTable) {
        this.autoCleanTable = autoCleanTable;
    }

    public String getFlywayScriptLocation() {
        return flywayScriptLocation;
    }

    public void setFlywayScriptLocation(String flywayScriptLocation) {
        this.flywayScriptLocation = flywayScriptLocation;
    }

    public Boolean getFlywayAutoMigration() {
        return flywayAutoMigration;
    }

    public void setFlywayAutoMigration(Boolean flywayAutoMigration) {
        this.flywayAutoMigration = flywayAutoMigration;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }
}

【配置类】

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.setting.yaml.YamlUtil;
import org.flywaydb.core.Flyway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author JHL
 * @version 1.0
 * @date 2022/8/19 14:58
 * @since : JDK 11
 */
public class FlywayRunner implements Runnable {

    private static final Logger log = LoggerFactory.getLogger(FlywayRunner.class);

    private static final Dict CONF = YamlUtil.loadByPath("application.yml");

    private static FlywayProperties flywayProperties;

    private static Flyway flyway = new Flyway();

    static {
        Map map = (Map) CONF.get(FlywayProperties.PREFIX);
        flywayProperties = BeanUtil.mapToBean(map, FlywayProperties.class, true);
        flyway.setDataSource(flywayProperties.getUrl(), flywayProperties.getUsername(), flywayProperties.getPassword());
        flyway.setLocations(flywayProperties.getFlywayScriptLocation());
        if (flywayProperties.getCleanOnValidationError()) {
            flyway.setCleanOnValidationError(true);
        }
        if (flywayProperties.getFlywayAutoMigration()) {
            flyway.setInitOnMigrate(true);
        }
    }

    @Override
    public void run() {
        if (flywayProperties.getEnable()) {
            if (flywayProperties.getFlywayAutoMigration()) {
                flyway.migrate();
                log.info("########### [ flyway 数据库初始化成功! ]  ###########");
            }
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                if (flywayProperties.getAutoCleanTable() && flywayProperties.getFlywayAutoMigration()) {
                    flyway.clean();
                    log.info("###########  [ flyway 数据库表清空成功! ]  ###########");
                }
            }));
        }
    }
}

【application.yml】

# 数据库脚本版本控制
flyway:
  enable: true
  url: jdbc:mysql://182.140.146.132:3303/znpg?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
  username: root
  password: imkjdsadwqeadxz
  flywayScriptLocation: classpath:db/migration/
  flywayAutoMigration: true
  cleanOnValidationError: true
  autoCleanTable: false

【IDEA插件支持】

Flyway Migration Creation用于按照flyway的规则自动生成数据库脚本的文件名称的插件

SQL 脚本命名规范如下

Prefix+Version+Separator+Description+Suffix

Prefix 前缀:V 代表版本迁移,U 代表撤销迁移,R 代表可重复迁移

Version 版本号:版本号通常 . 和整数组成

Separator 分隔符:固定由两个下划线 __ 组成

Description 描述:由下划线分隔的单词组成,用于描述本次迁移的目的

Suffix 后缀:如果是 SQL 文件那么固定由 .sql 组成,如果是基于 Java 类则默认不需要后缀

【参考文章】

推荐阅读——flyway的快速入门教程

Flyway组件的简介、工作流程和使用

springboot flyway 整合(不生效原因)

posted @ 2022-08-19 12:53  黄河大道东  阅读(205)  评论(0编辑  收藏  举报