基于springboot的項目,springboot使用flyway,使用介紹、個人總結及報錯場景如何修改

 2023-12-06 阅读 32 评论 0

摘要:文章目錄一、背景二、為什么要使用flyway三、flyway工作流程四、文件命名規范五、Flyway 的遷移類型六、sql遷移文件執行的遷移流程七、注意事項八、SpringBoot 項目使用 Flyway步驟項目目錄第1步:引入pom第2步:配置文件application.yml第3步:編寫sql遷

在這里插入圖片描述

文章目錄

  • 一、背景
  • 二、為什么要使用flyway
  • 三、flyway工作流程
  • 四、文件命名規范
  • 五、Flyway 的遷移類型
  • 六、sql遷移文件執行的遷移流程
  • 七、注意事項
  • 八、SpringBoot 項目使用 Flyway步驟
    • 項目目錄
    • 第1步:引入pom
    • 第2步:配置文件application.yml
    • 第3步:編寫sql遷移腳本文件
    • 第4步:創建啟動類FlaywayApplication
  • 九、項目啟動后的數據庫結果展示
  • 十、flyway拓展知識
  • 十一、練習使用flyway碰到的錯誤場景及解決方案
    • 錯誤場景1:Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration and org.flywaydb.core.api.configuration.FluentConfiguration
    • 錯誤場景2:SLF4J: No SLF4J providers were found.或者SLF4J: Defaulting to no-operation (NOP) logger implementation
    • 錯誤場景3:java.sql.SQLSyntaxErrorException: Unknown database 'flyway'
    • 錯誤場景4:Caused by: org.flywaydb.core.api.FlywayException: Validate failed: Detected failed migration to version 1.0 (create flyway test table)
    • 錯誤場景5:Migration checksum mismatch for migration version 1.1 -> Applied to database : 1332862643 -> Resolved locally : -826751737
    • 錯誤場景6:Caused by: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;
    • 錯誤場景7:Detected resolved migration not applied to database: 20221103.10000
  • 十二、flyway-maven-plugin使用介紹
  • 十三、補充說明

一、背景

  • 我的springboot版本為2.7.5
  • Flyway是一個款數據庫版本管理工具,它可以很方便的在命令行中使用,或者在Java應用程序中引入,用于管理我們的數據庫版本。
  • 通過集成Flyway可以實現啟動項目時自動執行項目迭代升級所需Sql語句,從而減少升級項目時人工干預成本。
  • Flyway最核心的就是用于記錄所有版本演化和狀態的MetaData表,Flyway首次啟動會創建默認名為SCHEMA_VERSION的元素局表。 表中保存了版本,描述,要執行的sql腳本等。

MetaData表就是元數據表,就是flyway_schema_history

在這里插入圖片描述

基于springboot的項目。在這里插入圖片描述

  • 在項目或產品中,很難一開始就把業務理清楚,把數據庫表設計好,因此數據表也會在迭代周期不斷迭代。在Java應用程序中使用Flyway,能快速有效地用于迭代數據庫表結構,并保證部署到測試環境或生產環境時,數據表都是保持一致的。

flyway官方文檔:https://flywaydb.org/documentation/

二、為什么要使用flyway

在多人開發的項目中,我們都習慣了使用SVN或者Git來對代碼做版本控制,主要的目的就是為了解決多人開發代碼沖突和版本回退的問題。

其實,數據庫的變更也需要版本控制,在日常開發中,我們經常會遇到下面的問題:
1. 自己寫的SQL忘了在所有環境執行。
2. 別人寫的SQL我們不能確定是否都在所有環境執行過了。
3. 有人修改了已經執行過的SQL,期望再次執行。
4. 需要新增環境做數據遷移。
5. 每次發版需要手動控制先發DB版本,再發布應用版本。
6. 其它場景。

有了flyway,這些問題都能得到很好的解決。

三、flyway工作流程

java手動回滾事務。flyway工作流程如下:

1. 項目啟動,應用程序完成數據庫連接池的建立后,Flyway自動運行。
2. 初次使用時,flyway會創建一個 flyway_schema_history 表,用于記錄sql執行記錄。
3. Flyway會掃描項目指定路徑下(默認是 classpath:db/migration )的所有sql腳本,與 flyway_schema_history 表腳本記錄進行比對。如果數據庫記錄執行過的腳本記錄,與項目中的sql腳本不一致,Flyway會報錯并停止項目執行。
4. 如果校驗通過,則根據表中的sql記錄最大版本號,忽略所有版本號不大于該版本的腳本。再按照版本號從小到大,逐個執行其余腳本。

四、文件命名規范

格式: V + 版本號 + __ (雙下劃線) + 描述.sql
在這里插入圖片描述

  • Eg:
    • 版本升級: V1_2_0__upgrade.sql(不推薦,因為小版本改動“1_2_0”這個數值不好一直疊加)
    • BUG修復: V1_2_0_20220816__hotfix.sql
    • V20221104_10001.sql推薦這種命名方式,其中20221102代表日期,10001代表SVN或者Git的提交版本號,便于迭代)
    • V20221104_10001__add.sql也推薦使用這種,雙下劃線后面加點描述詞)
  • 規則說明:
    • 前綴分隔符
      • V: 版本化遷移 (使用)
        • 默認V開頭 (可配置)
        • V(大些),小些不執行
      • R: 可重復遷移 (不使用)
        • 新增或追加腳本時再次執行該文件所有腳本(包括歷史sql)
        • 歷史sql腳本不可更改
        • 不需要指定腳步版本
      • U: 撤銷遷移 (不使用)
        • 編寫各版本對應撤銷版本腳步
        • 需要通過Flyway客戶端執行撤銷命令,執行最近一次版本撤銷
        • 撤銷場景較復雜,建議版本升級前進行DB備份,實現撤銷
    • SQL腳本版本
      • 2_1為sql版本號, ‘_’ 翻譯為小數點,即為2.1版本
      • 新建腳本,版本只可遞增
      • 不可使用V1作為版本號
    • 分隔符
      • ‘__’ 雙下劃線 (可配置)
    • SQL腳本名描述(待定)
      • 多個單詞用下劃線或空格將單詞分開
      • 版本化升級: upgrade
      • BUG修復: hotfix
    • .sql為固定后綴

五、Flyway 的遷移類型

版本遷移:以V開頭的文件

最常見的遷移就是就是版本化遷移,每次遷移都會對應的遷移版本,遷移的版本必須全局唯一,版本遷移最大的特點就是依次只被執行依次。

@SpringBootApplication?撤銷遷移:以U開頭的文件(這個U開頭的撤銷文件我沒玩明白如何使用,后續研究明白了再補上)

每個撤銷遷移都對應的一個版本遷移,也就是說撤銷遷移是針對版本遷移所存在的,每一個撤銷遷移與版本遷移都是一一對應的,而且對應的版本號必須一致。

可重復遷移:以R開頭的文件

可重復遷移有描述和校驗碼,但是沒有版本號,程序在每次啟動的時候,如果發現腳本文件有變化就會執行。

基于 SQL 的遷移

java.lang.reflect.undeclared,上面提到的幾種類型都是基于 SQL 文件來執行的,只不過每種類型的命名格式不一樣,下圖是從官網上截下來的,大家看下每種類型的文件應該按照如下的格式去命令,其中的 Separator 是兩個下劃線。

在這里插入圖片描述
主要分為下面幾個部分:

prefix:前綴,不同的類型采用不同的前綴,版本遷移使用 V,撤銷遷移使用 U,可重復遷移使用 R,當然這些都是可配置的;

Version:版本號,可以使用點符號或者單下劃線鏈接;

Separator:分隔符,兩個下劃線,也是可以配置的;

Springboot教程、Description:版本描述可以用下劃線和空格分隔;

Suffix:后綴,一般都是 .sql

六、sql遷移文件執行的遷移流程

1)Flyway 會掃描配置的腳本目錄下的腳本文件;
2)如果歷史記錄表不存在,則新建歷史記錄表;
3)如果是一次性執行腳本(V),按版本號從小到大執行遷移腳本,與當前歷史表中的版本號做對比,大于當前版本號的腳本才會被執行遷移;
4)如果是可重復執行腳本(R),檢查腳本是否有變動,有變動的可重復腳本才會被執行遷移;

七、注意事項

1. Fayway在每次應用啟動時檢測是否有需要執行的升級腳本;

2. 文件名以V作為前綴的,后跟版本號,版本號格式可以為為大版本號(1、2),也可以包含小版本號(1.1或1_1),但是需統一,不能有些有小版本號,有些沒有;

java springboot。3. 前綴為V的腳本不可重復執行,每個腳本只會被執行一次。已經執行過的歷史版本腳本不能再修改,除非清除flayway的歷史記錄,重新執行升級腳本,這在生產環境不允許。

前綴為R的腳本,后面無版本號,如R__update_version.sql,可以重復執行,每次內容發生變化時重啟后就會執行。

4. Flyway需要創建存儲腳本升級記錄用的表,因此需要建表權限,也可以事先建好。默認表名為flyway_schema_history;

八、SpringBoot 項目使用 Flyway步驟

項目目錄

在這里插入圖片描述

第1步:引入pom

第1步:引入pom

<dependencies><!--解決@RestController注解爆紅--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>${spring.boot.starter.web.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><!--使用flyway--><dependency><groupId>org.flywaydb</groupId><artifactId>flyway-core</artifactId><version>${flayway.test.version}</version></dependency><!--mysql相關--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope><version>${mysql.connector.java.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><version>${spring.boot.starter.jdbc.version}</version></dependency><!--必須配置相關slf4j的依賴,否則啟動報錯:說缺少日志框架的相關實現。注意:必須設置1.7.x及之前的版本,如果輸入2.0.x版本會報錯:SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.api.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.log4j12.version}</version></dependency>
</dependencies><build><plugins><plugin><groupId>org.flywaydb</groupId><artifactId>flyway-maven-plugin</artifactId><version>${flayway.test.version}</version><configuration><user>root</user><password>123456</password><driver>com.mysql.jdbc.Driver</driver><url>jdbc:mysql://10.110.13.86:3306/flyway?characterEncoding=utf8</url><baselineOnMigrate>true</baselineOnMigrate><!--sql腳本位置,flyway會自動去找到這個目錄并且執行里面的sql腳本--><locations>classpath:db/migration/</locations></configuration></plugin></plugins>
</build>

springboot注解大全、我的項目是父模塊模式,所以版本號在父pom中指定

<properties><spring.boot.starter.web.version>2.6.3</spring.boot.starter.web.version><lombok.version>1.18.22</lombok.version><flayway.test.version>6.5.7</flayway.test.version><mysql.connector.java.version>8.0.31</mysql.connector.java.version><spring.boot.starter.jdbc.version>2.7.5</spring.boot.starter.jdbc.version><slf4j.api.version>1.7.33</slf4j.api.version><slf4j.log4j12.version>1.7.33</slf4j.log4j12.version>
</properties>

第2步:配置文件application.yml

# 端口號
server:port: 8015spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://10.110.13.86:3306/flyway?characterEncoding=utf8username: rootpassword: 123456hikari:minimum-idle: 5 #最小空閑連接數idle-timeout: 180000 #空閑連接存活最大時間,默認600000maximum-pool-size: 10 #連接池最大連接數auto-commit: true #自動提交 默認truepool-name: HikariCP #連接池名稱max-lifetime: 1800000 #連接池最長生命周期,0表示無限生命周期,默認1800000(30分鐘)connection-timeout: 30000 #連接超時時間,默認30000connection-test-query: SELECT 1# flayway相關配置flyway:locations: classpath:db/migration   #這個路徑指的是fly版本控制的sql語句存放的路徑,可以多個,可以給每個環境使用不同位置,比如classpath:db/migration,classpath:test/db/migrationbaseline-on-migrate: true   #開啟自動創建flyway元數據表標識 默認: falseenabled: true   #是否啟用flyway(默認true)clean-disabled: true    #禁止flyway執行清理table: flyway_schema_history  #用于記錄所有的版本變化記錄

第3步:編寫sql遷移腳本文件

就是寫了1個簡單的create創建語句、以及insert添加、update修改語句

V1.0__create_flyway_test_table.sql

CREATE TABLE flyway_test_table (engine_name VARCHAR(64) NOT NULL COLLATE 'utf8_general_ci',device_type INT(11) NOT NULL,cost_name VARCHAR(64) NOT NULL,cost_value FLOAT NULL DEFAULT NULL,last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,comment VARCHAR(1024) NULL DEFAULT NULL,PRIMARY KEY (engine_name) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

V1.1__add_flyway_test_table.sql

INSERT INTO flyway_test_table (engine_name, device_type, cost_name, cost_value, last_update, comment) VALUES ('34000', 0, 'io_block_read_cost', NULL, '2022-10-21 03:32:08', NULL);
INSERT INTO flyway_test_table (engine_name, device_type, cost_name, cost_value, last_update, comment) VALUES ('34003', 0, 'io_block_read_cost', NULL, '2022-10-21 03:32:08', NULL);

V20221101__10381.sql

INSERT INTO flyway_test_table (engine_name, device_type, cost_name, cost_value, last_update, comment) VALUES ('34001', 0, 'io_block_read_cost', NULL, '2022-10-21 03:32:08', NULL);

R__10382.sql

UPDATE flyway_test_table set engine_name='34005' where engine_name = '34000';

第4步:創建啟動類FlaywayApplication

package com.flyway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Spring Boot使用flayway自動執行數據庫升級腳本* @Author 211145187* @Date 2022/9/20 14:33**/
@SpringBootApplication
public class FlaywayApplication {public static void main(String[] args) {SpringApplication.run(FlaywayApplication.class, args);}
}

九、項目啟動后的數據庫結果展示

項目啟動后控制臺日志打印

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-01 13:34:01.873  INFO 21004 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 21004 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-01 13:34:01.876  INFO 21004 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-01 13:34:02.861  INFO 21004 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-01 13:34:02.898  INFO 21004 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-01 13:34:02.899  INFO 21004 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-01 13:34:03.005  INFO 21004 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-01 13:34:03.005  INFO 21004 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1086 ms
2022-11-01 13:34:03.400  INFO 21004 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.5.7 by Redgate
2022-11-01 13:34:03.405  INFO 21004 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Starting...
2022-11-01 13:34:13.747  INFO 21004 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Start completed.
2022-11-01 13:34:13.766  INFO 21004 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://10.110.13.86:3306/mysql (MySQL 5.7)
2022-11-01 13:34:13.822  INFO 21004 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 2 migrations (execution time 00:00.029s)
2022-11-01 13:34:13.832  INFO 21004 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema `mysql`: << Empty Schema >>
2022-11-01 13:34:13.847  INFO 21004 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema `mysql` to version 1.0 - create flyway test table
2022-11-01 13:34:13.884  INFO 21004 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema `mysql` to version 1.1 - add flyway test table
2022-11-01 13:34:13.901  INFO 21004 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 2 migrations to schema `mysql` (execution time 00:00.075s)
2022-11-01 13:34:14.004  INFO 21004 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8015 (http) with context path ''
2022-11-01 13:34:14.013  INFO 21004 --- [           main] com.flyway.FlaywayApplication            : Started FlaywayApplication in 12.628 seconds (JVM running for 13.458)Process finished with exit code -1

flyway庫下的表結構

在這里插入圖片描述

flyway_schema_history

在這里插入圖片描述

flyway_test_table

在這里插入圖片描述

十、flyway拓展知識

flyway.baseline-description對執行遷移時基準版本的描述.
flyway.baseline-on-migrate當遷移時發現目標schema非空,而且帶有沒有元數據的表時,是否自動執行基準遷移,默認false.
flyway.baseline-version開始執行基準遷移時對現有的schema的版本打標簽,默認值為1.
flyway.check-location檢查遷移腳本的位置是否存在,默認false.
flyway.clean-on-validation-error當發現校驗錯誤時是否自動調用clean,默認false.
flyway.enabled是否開啟flywary,默認true.
flyway.encoding設置遷移時的編碼,默認UTF-8.
flyway.ignore-failed-future-migration當讀取元數據表時是否忽略錯誤的遷移,默認false.
flyway.init-sqls當初始化好連接時要執行的SQL.
flyway.locations遷移腳本的位置,默認db/migration.
flyway.out-of-order是否允許無序的遷移,默認false.
flyway.password目標數據庫的密碼.
flyway.placeholder-prefix設置每個placeholder的前綴,默認${.
flyway.placeholder-replacementplaceholders是否要被替換,默認true.
flyway.placeholder-suffix設置每個placeholder的后綴,默認}.
flyway.placeholders.[placeholder name]設置placeholder的value
flyway.schemas設定需要flywary遷移的schema,大小寫敏感,默認為連接默認的schema.
flyway.sql-migration-prefix遷移文件的前綴,默認為V.
flyway.sql-migration-separator遷移腳本的文件名分隔符,默認__
flyway.sql-migration-suffix遷移腳本的后綴,默認為.sql
flyway.tableflyway使用的元數據表名,默認為schema_version
flyway.target遷移時使用的目標版本,默認為latest version
flyway.url遷移時使用的JDBC URL,如果沒有指定的話,將使用配置的主數據源
flyway.user遷移數據庫的用戶名
flyway.validate-on-migrate遷移時是否校驗,默認為true.

十一、練習使用flyway碰到的錯誤場景及解決方案

錯誤場景1:Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration and org.flywaydb.core.api.configuration.FluentConfiguration

詳情錯誤日志

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-01 10:52:01.616  INFO 26424 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 11.0.12 on HYT211145187-01 with PID 26424 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-01 10:52:01.619  INFO 26424 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-01 10:52:02.677  INFO 26424 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-01 10:52:02.691  INFO 26424 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-01 10:52:02.691  INFO 26424 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-01 10:52:02.778  INFO 26424 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-01 10:52:02.779  INFO 26424 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1070 ms
2022-11-01 10:52:03.148  WARN 26424 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;
2022-11-01 10:52:03.151  INFO 26424 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-11-01 10:52:03.161  INFO 26424 --- [           main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-01 10:52:03.177 ERROR 26424 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:An attempt was made to call a method that does not exist. The attempt was made from the following location:org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.configureIgnoredMigrations(FlywayAutoConfiguration.java:273)The following method did not exist:org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;The calling method's class, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration, was loaded from the following location:jar:file:/F:/apache-maven-3.6.3/repository/org/springframework/boot/spring-boot-autoconfigure/2.6.3/spring-boot-autoconfigure-2.6.3.jar!/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.classThe called method's class, org.flywaydb.core.api.configuration.FluentConfiguration, is available from the following locations:jar:file:/F:/apache-maven-3.6.3/repository/org/flywaydb/flyway-core/9.6.0/flyway-core-9.6.0.jar!/org/flywaydb/core/api/configuration/FluentConfiguration.classThe called method's class hierarchy was loaded from the following locations:org.flywaydb.core.api.configuration.FluentConfiguration: file:/F:/apache-maven-3.6.3/repository/org/flywaydb/flyway-core/9.6.0/flyway-core-9.6.0.jarAction:Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration and org.flywaydb.core.api.configuration.FluentConfiguration

錯誤原因:pom中flyway-core的依賴版本問題,當設置9.6.0時就報這個錯誤,但當設置6.5.7時就能正常啟動,感覺是flyway高版本和springboot2.x版本不匹配的問題導致的問題,具體為啥目前不清楚,只要把flyway版本設置成5.2.1或者6.5.7版本以及之間的版本那都是可以運行的。

錯誤場景2:SLF4J: No SLF4J providers were found.或者SLF4J: Defaulting to no-operation (NOP) logger implementation

詳情錯誤日志

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J: Ignoring binding found at [jar:file:/F:/apache-maven-3.6.3/repository/ch/qos/logback/logback-classic/1.2.10/logback-classic-1.2.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See https://www.slf4j.org/codes.html#ignoredBindings for an explanation..   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)

報錯原因:pom中沒有slf4j的相關框架實現,即現在項目只有框架門面,而缺少具體的框架實現。

解決方案:pom中添加如下2個依賴即可解決問題。

<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.33</version>
</dependency>
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.33</version>
</dependency>

可參考如下其他人博客:↓

SLF4J 報錯解決:No SLF4J providers were found

錯誤場景3:java.sql.SQLSyntaxErrorException: Unknown database ‘flyway’

詳情錯誤日志

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-01 13:34:54.569  INFO 7488 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 7488 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-01 13:34:54.572  INFO 7488 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-01 13:34:55.624  INFO 7488 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-01 13:34:55.634  INFO 7488 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-01 13:34:55.634  INFO 7488 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-01 13:34:55.730  INFO 7488 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-01 13:34:55.730  INFO 7488 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1111 ms
2022-11-01 13:34:56.117  INFO 7488 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.5.7 by Redgate
2022-11-01 13:34:56.122  INFO 7488 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Starting...
2022-11-01 13:35:07.441 ERROR 7488 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariCP - Exception during pool initialization.java.sql.SQLSyntaxErrorException: Unknown database 'flyway'at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-j-8.0.31.jar:8.0.31]at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.0.31.jar:8.0.31]at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-j-8.0.31.jar:8.0.31]at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-j-8.0.31.jar:8.0.31]at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.0.31.jar:8.0.31]at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.31.jar:8.0.31]at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar:na]at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar:na]at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar:na]at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) [HikariCP-4.0.3.jar:na]at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) [HikariCP-4.0.3.jar:na]at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-4.0.3.jar:na]at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-4.0.3.jar:na]at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) [flyway-core-6.5.7.jar:na]at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80) [flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.execute(Flyway.java:453) [flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.migrate(Flyway.java:158) [flyway-core-6.5.7.jar:na]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_71]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_71]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_71]at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_71]at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70) [spring-boot-autoconfigure-2.6.3.jar:2.6.3]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) [spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.6.3.jar:2.6.3]at com.flyway.FlaywayApplication.main(FlaywayApplication.java:14) ~[classes/:na]

錯誤原因:mysql數據庫下沒有叫flyway的數據庫。

解決方案:必須先創建名為flyway的數據庫后,才能設置url: jdbc:mysql://10.110.13.86:3306/flyway?characterEncoding=utf8。

錯誤場景4:Caused by: org.flywaydb.core.api.FlywayException: Validate failed: Detected failed migration to version 1.0 (create flyway test table)

詳情錯誤日志

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-01 13:56:26.099  INFO 25648 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 25648 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-01 13:56:26.102  INFO 25648 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-01 13:56:27.276  INFO 25648 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-01 13:56:27.312  INFO 25648 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-01 13:56:27.312  INFO 25648 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-01 13:56:27.401  INFO 25648 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-01 13:56:27.401  INFO 25648 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1249 ms
2022-11-01 13:56:27.778  INFO 25648 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.5.7 by Redgate
2022-11-01 13:56:27.784  INFO 25648 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Starting...
2022-11-01 13:56:38.112  INFO 25648 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Start completed.
2022-11-01 13:56:38.130  INFO 25648 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://10.110.13.86:3306/flyway (MySQL 5.7)
2022-11-01 13:56:38.193  WARN 25648 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.reflect.InvocationTargetException
2022-11-01 13:56:38.193  INFO 25648 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Shutdown initiated...
2022-11-01 13:56:38.197  INFO 25648 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Shutdown completed.
2022-11-01 13:56:38.200  INFO 25648 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-11-01 13:56:38.209  INFO 25648 --- [           main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-01 13:56:38.238 ERROR 25648 --- [           main] o.s.boot.SpringApplication               : Application run failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.reflect.InvocationTargetExceptionat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]at com.flyway.FlaywayApplication.main(FlaywayApplication.java:14) [classes/:na]
Caused by: java.lang.reflect.InvocationTargetException: nullat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_71]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_71]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_71]at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_71]at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]... 18 common frames omitted
Caused by: org.flywaydb.core.api.FlywayException: Validate failed: 
Detected failed migration to version 1.0 (create flyway test table)at org.flywaydb.core.Flyway.doValidate(Flyway.java:292) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.access$100(Flyway.java:73) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway$1.execute(Flyway.java:166) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway$1.execute(Flyway.java:158) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.execute(Flyway.java:527) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.migrate(Flyway.java:158) ~[flyway-core-6.5.7.jar:na]... 25 common frames omitted

錯誤原因:數據庫涉及的2張表沒全刪掉(可能有一個表還存在情況下啟動的項目),如圖,我這邊模擬的場景是第一次啟動項目,實現第一個sql創建DDL的創建表語句,第二個sql插入一條數據的場景,但是我涉及的的2個表flyway_schema_history和flyway_test_table沒刪掉。

在這里插入圖片描述

解決方案:這兩個表都刪除情況下再重新啟動項目

錯誤場景5:Migration checksum mismatch for migration version 1.1 -> Applied to database : 1332862643 -> Resolved locally : -826751737

詳情錯誤日志

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-01 14:50:33.363  INFO 26596 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 26596 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-01 14:50:33.366  INFO 26596 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-01 14:50:34.336  INFO 26596 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-01 14:50:34.372  INFO 26596 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-01 14:50:34.373  INFO 26596 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-01 14:50:34.460  INFO 26596 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-01 14:50:34.460  INFO 26596 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1052 ms
2022-11-01 14:50:34.839  INFO 26596 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.5.7 by Redgate
2022-11-01 14:50:34.844  INFO 26596 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Starting...
2022-11-01 14:50:45.205  INFO 26596 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Start completed.
2022-11-01 14:50:45.225  INFO 26596 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://10.110.13.86:3306/flyway (MySQL 5.7)
2022-11-01 14:50:45.297  WARN 26596 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.reflect.InvocationTargetException
2022-11-01 14:50:45.297  INFO 26596 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Shutdown initiated...
2022-11-01 14:50:45.301  INFO 26596 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Shutdown completed.
2022-11-01 14:50:45.305  INFO 26596 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-11-01 14:50:45.314  INFO 26596 --- [           main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-01 14:50:45.343 ERROR 26596 --- [           main] o.s.boot.SpringApplication               : Application run failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.reflect.InvocationTargetExceptionat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]at com.flyway.FlaywayApplication.main(FlaywayApplication.java:14) [classes/:na]
Caused by: java.lang.reflect.InvocationTargetException: nullat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_71]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_71]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_71]at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_71]at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]... 18 common frames omitted
Caused by: org.flywaydb.core.api.FlywayException: Validate failed: 
Migration checksum mismatch for migration version 1.1
-> Applied to database : 1332862643
-> Resolved locally    : -826751737at org.flywaydb.core.Flyway.doValidate(Flyway.java:292) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.access$100(Flyway.java:73) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway$1.execute(Flyway.java:166) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway$1.execute(Flyway.java:158) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.execute(Flyway.java:527) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.migrate(Flyway.java:158) ~[flyway-core-6.5.7.jar:na]... 25 common frames omittedProcess finished with exit code 1

報錯原因:修改了已經執行過的遷移文件,比如V開頭的叫V1.1__add_flyway_test_table.sql這個名字的文件,重新執行這個更改的遷移文件時就會產生新的校驗和“checkSum"與原來校驗和不符合,導致報該錯誤,原則上不可以直接修改已經執行的遷移文件,要修改需要新建遷移文件進行修改。

舉例說明:比如最開始我有個文件V1.1__add_flyway_test_table.sql,里面有個insert插入一條數據的sql,然后我啟動整個項目,執行sql插入一條數據,且V開頭的文件按常理已經執行過一次了,后面壓根不會再次執行,但我手欠兒,再次對V開頭的文件V1.1__add_flyway_test_table.sql進行修改或者新增幾條sql,然后再次啟動項目就會報錯誤5的錯誤了。

解決方案

  • 1)方案1(不推薦):涉及的2張表flyway_schema_history和flyway_test_table都刪除,這會造成已有的數據丟失、混亂。
  • 2)方案2(推薦):可以使用Flyway Maven 插件,雙擊運行:flyway:repair ,修復該錯誤,再重新執行:flyway:migrate。這樣項目就能再次啟動。
  • 3)方案3(不推薦):刪除2張表flyway_schema_history和flyway_test_table,然后重新啟動項目把所有sql腳本從頭到尾執行一遍。缺點:萬一數據量太大太耗時,甚至可能出錯。

碰到個問題:原來的V1.1__add_flyway_test_table.sql里面有個內容,然后我對它修改后,執行flyway:repair,在執行flyway:migrate后,再次啟動項目發現V1.1__add_flyway_test_table.sql新增的內容沒有執行?數據庫沒有產生新數據?

答案:應該是不會執行的,除非新增個V開頭的遷移文件才會執行,即:對已經執行過的V開頭的遷移文件修改后也不會執行,需要新建遷移文件進行修改才會生效。

錯誤場景6:Caused by: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;

詳情錯誤日志

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-01 16:38:17.063  INFO 18260 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 18260 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-01 16:38:17.064  INFO 18260 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-01 16:38:18.093  INFO 18260 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-01 16:38:18.126  INFO 18260 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-01 16:38:18.126  INFO 18260 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-01 16:38:18.215  INFO 18260 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-01 16:38:18.215  INFO 18260 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1101 ms
2022-11-01 16:38:18.597  WARN 18260 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;
2022-11-01 16:38:18.602  INFO 18260 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-11-01 16:38:18.609  INFO 18260 --- [           main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-01 16:38:18.634 ERROR 18260 --- [           main] o.s.boot.SpringApplication               : Application run failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]at com.flyway.FlaywayApplication.main(FlaywayApplication.java:14) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.15.jar:5.3.15]... 21 common frames omitted
Caused by: java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.configureIgnoredMigrations(FlywayAutoConfiguration.java:273) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.configureProperties(FlywayAutoConfiguration.java:213) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.flyway(FlywayAutoConfiguration.java:119) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_71]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_71]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_71]at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_71]at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.15.jar:5.3.15]... 22 common frames omitted
Caused by: java.lang.NoSuchMethodError: org.flywaydb.core.api.configuration.FluentConfiguration.ignoreMissingMigrations(Z)Lorg/flywaydb/core/api/configuration/FluentConfiguration;at java.lang.invoke.MethodHandleNatives.resolve(Native Method) ~[na:1.8.0_71]at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:962) ~[na:1.8.0_71]at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:987) ~[na:1.8.0_71]at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1390) ~[na:1.8.0_71]at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1746) ~[na:1.8.0_71]at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:477) ~[na:1.8.0_71]... 30 common frames omittedProcess finished with exit code 1

錯誤原因:之前測試flyway-core的版本設置6.5.7就可以正常啟動,當使用高版本號比如設置9.6.0時就報如上的錯誤,感覺就是版本設置高了導致的問題,具體為啥目前不清楚,只要把flyway版本設置成5.2.1或者6.5.7版本以及之間的版本那都是可以運行的。

錯誤場景7:Detected resolved migration not applied to database: 20221103.10000

詳情錯誤日志

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.3)2022-11-02 09:54:21.609  INFO 11444 --- [           main] com.flyway.FlaywayApplication            : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 11444 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)
2022-11-02 09:54:21.613  INFO 11444 --- [           main] com.flyway.FlaywayApplication            : No active profile set, falling back to default profiles: default
2022-11-02 09:54:22.684  INFO 11444 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8015 (http)
2022-11-02 09:54:22.694  INFO 11444 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-02 09:54:22.694  INFO 11444 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-11-02 09:54:22.781  INFO 11444 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-02 09:54:22.781  INFO 11444 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1124 ms
2022-11-02 09:54:23.171  INFO 11444 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.5.7 by Redgate
2022-11-02 09:54:23.176  INFO 11444 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Starting...
2022-11-02 09:54:33.502  INFO 11444 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Start completed.
2022-11-02 09:54:33.519  INFO 11444 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://10.110.13.86:3306/flyway (MySQL 5.7)
2022-11-02 09:54:33.585  WARN 11444 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.reflect.InvocationTargetException
2022-11-02 09:54:33.585  INFO 11444 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Shutdown initiated...
2022-11-02 09:54:33.589  INFO 11444 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariCP - Shutdown completed.
2022-11-02 09:54:33.592  INFO 11444 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-11-02 09:54:33.602  INFO 11444 --- [           main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-02 09:54:33.632 ERROR 11444 --- [           main] o.s.boot.SpringApplication               : Application run failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.reflect.InvocationTargetExceptionat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]at com.flyway.FlaywayApplication.main(FlaywayApplication.java:14) [classes/:na]
Caused by: java.lang.reflect.InvocationTargetException: nullat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_71]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_71]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_71]at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_71]at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]... 18 common frames omitted
Caused by: org.flywaydb.core.api.FlywayException: Validate failed: 
Detected resolved migration not applied to database: 20221103.10000at org.flywaydb.core.Flyway.doValidate(Flyway.java:292) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.access$100(Flyway.java:73) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway$1.execute(Flyway.java:166) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway$1.execute(Flyway.java:158) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.execute(Flyway.java:527) ~[flyway-core-6.5.7.jar:na]at org.flywaydb.core.Flyway.migrate(Flyway.java:158) ~[flyway-core-6.5.7.jar:na]... 25 common frames omittedProcess finished with exit code 1

錯誤原因:sql版本校驗沒過,正常情況一般是版本命名不規范,修改sql版本即可。

場景復現:我先創建個V20221103_10001__add_flyway_test_table.sql的文件,執行版本遷移成功,然后我創建個V20221103_10000__add_flyway_test_table.sql發下失敗,明顯是雙下劃線前面的版本不規范造成的。

十二、flyway-maven-plugin使用介紹

該插件主要針對錯誤場景5

pom引入插件,切記插件中一定要標注<user>、<password>、<driver>、 <url>、<locations>等屬性,否則插件可能無生效。

<build><plugins><plugin><groupId>org.flywaydb</groupId><artifactId>flyway-maven-plugin</artifactId><version>${flayway.test.version}</version><configuration><user>root</user><password>123456</password><driver>com.mysql.jdbc.Driver</driver><url>jdbc:mysql://10.110.13.86:3306/flyway?characterEncoding=utf8</url><baselineOnMigrate>true</baselineOnMigrate><!--sql腳本位置,flyway會自動去找到這個目錄并且執行里面的sql腳本--><locations>classpath:db/migration/</locations></configuration></plugin></plugins>
</build>

maven刷新后插件的位置

在這里插入圖片描述

以上步驟中,每次想要migration都需要運行整個springboot項目,并且只能執行migrate一種命令,其實flyway還是有很多其它命令的。maven插件給了我們不需要啟動項目就能執行flyway各種命令的機會。
插件命令說明

其它命令的作用如下:

  • baseline
    對已經存在數據庫Schema結構的數據庫一種解決方案。實現在非空數據庫新建MetaData表,并把Migrations應用到該數據庫;也可以在已有表結構的數據庫中實現添加Metadata表。

  • clean
    清除掉對應數據庫Schema中所有的對象,包括表結構,視圖,存儲過程等,clean操作在dev 和 test階段很好用,但在生產環境務必禁用。

  • info
    用于打印所有的Migrations的詳細和狀態信息,也是通過MetaData和Migrations完成的,可以快速定位當前的數據庫版本。

  • repair
    repair操作能夠修復metaData表,該操作在metadata出現錯誤時很有用。

  • undo
    撤銷操作,社區版不支持。

  • validate
    驗證已經apply的Migrations是否有變更,默認開啟的,原理是對比MetaData表與本地Migrations的checkNum值,如果值相同則驗證通過,否則失敗。

  • migrate
    注意:點擊flyway:migrate和啟動整個項目是不等價的,只點擊flyway:migrate并沒有執行遷移,而重新啟動項目就會執行新的的遷移文件。
    migrate是指把數據Schema遷移到最新版本,在Migrate時會檢查MetaData元數據表,如果不存在就創建MetaData表,MetaData用于記錄數據庫歷史變更等信息;
    migrate會掃描指定文件系統或者classpath下的Migrations。會與MetaData中的記錄進行對比,進行版本升級。

十三、補充說明

這個撤銷遷移腳本,也就是U開頭的文件沒玩明白,沒查到完整的博客不明白如何使用,后續如果鼓弄明白了會補充進來的。如果有知道的博友也可以評論,一起學習實踐下如何使用。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/189616.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息