SpringBoot使用Flyway管理数据库版本

SpringBoot使用Flyway管理数据库版本

介绍

Flyway 是一个简单开源数据库版本控制器(约定大于配置),主要提供 migrate、clean、info、validate、baseline、repair 等命令。它支持 SQL(PL/SQL、T-SQL)方式和 Java 方式,支持命令行客户端等,还提供一系列的插件支持(Maven、Gradle、SBT、ANT 等)。java

官网地址:https://flywaydb.org/ 当前版本:5.2.1 maven:git

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.2.1</version>
</dependency>

Flyway集成Springboot

Flyway与Springboot集成很方便。github

pom加入引用

pom.xml 加入引用:spring

<!-- flyway -->
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>

加入sql脚本

在resources目录下建立db/migration目录,里面能够放入sql脚本,格式以下: "V1__init_database.sql"sql

  • 其中V表示版本关键字,其余的还有U和R,分别用户撤销和迁移。
  • 后面的1表示版本号
  • 后面根两条下划线,这里是个坑,刚开始没注意,只写了一个,结果一直提示以下错误。
Caused by: org.flywaydb.core.api.FlywayException: Wrong migration name format: V1_init_database.sql(It should look like this: V1.2__Description.sql)
  • 再后面跟脚本说明,能够带下划线

目录结构以下截图:数据库

flyway脚本

配置

加入配置:api

enter description here

不过这配置加不加都无所谓,都能执行bash

运行

项目启动后,数据库新建完毕,见下图:maven

enter description here

其中flyway_schema_history是flyway内部使用的表,维护了数据库的变动记录,以下:spring-boot

enter description here

整个集成过程仍是很方便的。

示例代码: github:https://github.com/treeyh/java-demo/tree/master/spring-boot/spring-boot-server gitee:https://gitee.com/treeyh/java-demo/tree/master/spring-boot/spring-boot-server

相关文章
相关标签/搜索