Spring Boot 集成 Flyway 数据库版本管理

Spring Boot Flyway About 1,792 words

添加依赖

Spring Boot2.6.7父工程中默认使用的Flyway版本为8.0.5

需要用到jdbc依赖(当然配置了MyBatis等,可以正常加载datasource的依赖都可以)

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

SQL 脚本存放路径

默认是在classth:db/migration(可通过spring.flyway.locations配置修改)

resources目录下新建db文件夹,db文件夹下新建migration文件夹。

└─src
    └─main
        ├─java
        │  └─com
        │      └─example
        │          └─flyway
        │                  FlywayApplication.java
        │
        └─resources
            │  application.yml
            │
            └─db
                └─migration

SQL 脚本命令规则

规则:V<VERSION>__<NAME>.sql

示例:

  • V0.1__INIT_TABLE.sql
  • V1_0_0__INIT_TABLE.sql

注意:

  • V字母要大写。
  • <VERSION>后面的下划线是两个下划线。
  • 已经运行过后的SQL脚本不应该再被修改,不然启动程序的时候会报错。

可能错误

没有以__双下划线分割,Flyway验证失败,不加载脚本。

o.f.core.internal.command.DbValidate     : Validating migrations ...
o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/V0.1_INIT_TABLE.sql (filename: V0.1_INIT_TABLE.sql)
o.f.core.internal.command.DbValidate     : Successfully validated 0 migrations (execution time 00:00.031s)
o.f.core.internal.command.DbValidate     : No migrations found. Are your locations set up correctly?

手动修改了已经执行过的脚本,导致校验文件码失败。

Caused by: org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation
Migration checksum mismatch for migration version 0.1
-> Applied to database : -73817940
-> Resolved locally    : -1919657487. Either revert the changes to the migration, or run repair to update the schema history.

官网

https://flywaydb.org

Views: 1,219 · Posted: 2022-10-05

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh