引起:org.flywaydb.core.api.FlywayException:验证失败.迁移校验和不匹配迁移2

39 java flyway spring-boot

我试图找到以下问题的解决方案,但它们都没有为我工作.我正在使用MySQL + flyway开发Angular + Spring Boot应用程序.请指导这里出了什么问题.

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 org.flywaydb.core.api.FlywayException: Validate failed. Migration Checksum mismatch for migration 2
-> Applied to database : 1499248173
-> Resolved locally    : -1729781252
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:829) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at com.boot.App.main(App.java:9) [classes/:na]
Caused by: org.flywaydb.core.api.FlywayException: Validate failed. Migration Checksum mismatch for migration 2
-> Applied to database : 1499248173
-> Resolved locally    : -1729781252
    at org.flywaydb.core.Flyway.doValidate(Flyway.java:1108) ~[flyway-core-3.2.1.jar:na]
    at org.flywaydb.core.Flyway.access$300(Flyway.java:62) ~[flyway-core-3.2.1.jar:na]
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:1012) ~[flyway-core-3.2.1.jar:na]
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:1006) ~[flyway-core-3.2.1.jar:na]
    at org.flywaydb.core.Flyway.execute(Flyway.java:1418) ~[flyway-core-3.2.1.jar:na]
    at org.flywaydb.core.Flyway.migrate(Flyway.java:1006) ~[flyway-core-3.2.1.jar:na]
    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 18 common frames omitted
Run Code Online (Sandbox Code Playgroud)

application.properties

logging.level.org.springframework.web=DEBUG

server.port=8080

spring.h2.console.enabled=true
spring.h2.console.path=/h2

## For H2 DB
#spring.datasource.url=jdbc:h2:file:~/dasboot
#spring.datasource.username=sa
#spring.datasource.password=
#spring.datasource.driver-class-name=org.h2.Driver

## For MYSQL DB
spring.datasource.url=jdbc:mysql://localhost:3306/dasboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.max-wait=10000
spring.datasource.min-evictable-idle-time-millis=1000
spring.datasource.min-idle=8
spring.datasource.time-between-eviction-runs-millis=1

flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=false;

#datasource.flyway.url=jdbc:h2:file:~/dasboot
#datasource.flyway.username=sa
#datasource.flyway.password=
#datasource.flyway.driver-class-name=org.h2.Driver


datasource.flyway.url=jdbc:mysql://localhost:3306/dasboot
datasource.flyway.username=root
datasource.flyway.password=root
datasource.flyway.driver-class-name=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

的pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>

    <name>das-boot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)

V2__create_shipwreck.sql

-- For H2 DB
--CREATE TABLE SHIPWRECK(
--  ID INT AUTO_INCREMENT,
--  NAME VARCHAR(255),
--  DESCRIPTION VARCHAR(2000),
--  CONDITION VARCHAR(255),
--  DEPTH INT,
--  LATITUDE DOUBLE,
--  LONGITUDE DOUBLE,
--  YEAR_DISCOVERED INT
--);

CREATE TABLE `dasboot`.`shipwreck` (
  `ID` INT NOT NULL AUTO_INCREMENT,
  `NAME` VARCHAR(255) NULL,
  `DESCRIPTION` VARCHAR(2000) NULL,
  `CONDITION` VARCHAR(255) NULL,
  `DEPTH` INT NULL,
  `LATITUDE` DOUBLE NULL,
  `LONGITUDE` DOUBLE NULL,
  `YEAR_DISCOVERED` INT NULL,
  PRIMARY KEY (`ID`));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Kyl*_*son 53

Flyway将SQL脚本的校验和与先前运行的校验和的校验和进行比较.如果更改已由Flyway应用的SQL脚本,则通常会发生此异常,从而导致校验和不匹配.

如果这是开发,您可以删除数据库并从头开始迁移.

如果您正在制作中,请不要编辑已应用的SQL脚本.只创建新的SQL脚本.

  • 删除数据库?如果您有大量测试数据?丢弃不是解决方案。它可以工作,但不能解决问题。 (3认同)

小智 27

这是当我在本地系统中遇到此问题时对我有用的解决方案。

  1. 转到数据库中的 flyway_schema_history
  2. 删除包含sql迁移脚本的行

  • 就我而言,表的名称是 schema_version。 (4认同)
  • 我可以在H2做什么? (2认同)

Ind*_*our 14

最好的解决方案是执行以下步骤:

  1. 删除名为 - V2__create_shipwreck.sql的文件,再次清理并构建项目.
  2. 再次运行该项目,登录到h2并删除名为"schema_version"的表.

    drop table schema_version;

  3. 现在使用ddl创建V2__create_shipwreck.sql文件并再次重新运行该项目.

  4. 请记住这一点,在pom.xml中添加版本4.1.2 for flyway-core

    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>4.1.2</version>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)

它现在应该工作.希望这会有所帮助.

  • 你可以只删除你遇到麻烦的行,例如:从schema_version中删除version = 2; (3认同)

Moe*_*ius 8

如果您不在生产中,您可以在数据库中查看flywayTable并删除包含已应用脚本名称的行。

flywayTable 是一个项目选项,它定义了 flyway 使用的数据库中表的名称,其中包含有关此数据库版本的信息,已应用的脚本...


dav*_*ave 8

我遇到了同样的问题并从数据库中删除了完整的架构,但问题仍然存在.

我通过运行repair()flyway命令解决了这个问题:

flyway.repair();
Run Code Online (Sandbox Code Playgroud)

我无法找出究竟出了什么问题.

  • 如果你使用 gradle 那么 `./gradlew FlywayRepair` 就可以了 (5认同)
  • 那是在初始化Flyway之后,例如Flyway Flyway = new Flyway(); Flyway.setDataSource(dataSource); 飞路.修复(); 迁移(); (2认同)

Dja*_*las 7

我只是从schema_version中删除偏离要应用的迁移的迁移.这样您就不会丢弃任何可能的测试数据.

例如:

SELECT * from schema_version order by installed_on desc

V_005_five.sql
V_004_four.sql
V_003_three.sql
V_002_two.sql
V_001_one.sql
Run Code Online (Sandbox Code Playgroud)

要应用的迁移

V_005_five.sql
* V_004_addUserTable.sql *
V_003_three.sql
V_002_two.sql
V_001_one.sql
Run Code Online (Sandbox Code Playgroud)

这里的解决方案是从schema_version中删除

V_005_five.sql
V_004_four.sql
Run Code Online (Sandbox Code Playgroud)

并恢复由此引起的任何数据库更改.例如,如果架构创建了新表,则必须在运行迁移之前删除该表.

当你运行flyway时它只会重新申请

V_005_five.sql
* V_004_addUserTable.sql *
Run Code Online (Sandbox Code Playgroud)

新的schema_version将是

V_005_five.sql
* V_004_addUserTable.sql *
V_003_three.sql
V_002_two.sql
V_001_one.sql
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你

  • @ user5707585 这应该是公认的答案,因为它实际上有助于为您的问题提供解决方案,而不是在您开始时不应该做的事情上居高临下地居高临下。 (2认同)

Ste*_*ane 7

如果您确定 SQL 文件中的修改对您现有的架构没有影响,您还可以更新现有架构的校验和。

我在 sql 文件中稍作更改后执行了此操作。

这是我更新校验和的方法:

update flyway_schema_history set checksum = '-1934991199' where installed_rank = '1';
Run Code Online (Sandbox Code Playgroud)


小智 5

遇到这个问题时,我所做的就是连接到数据库并更新与错误版本相对应的校验和字段,将由 FlyWay 本地解析的值放在那里。

对于以下错误:

nested exception is org.flywaydb.core.api.FlywayException: Validate failed.
Migration Checksum mismatch for migration 1.12
    -> Applied to database : 1029320280
    -> Resolved locally    : -236187247
Run Code Online (Sandbox Code Playgroud)

我只是这样做了:

UPDATE schema_version SET checksum = -236187247 WHERE version_rank = 12 AND checksum = 1029320280;
Run Code Online (Sandbox Code Playgroud)

并且问题解决了..

注意:您必须确保您的架构实际上是正确的,检查您的表及其结构,如果一切正常,那么您可以应用此解决方案;否则,您应该首先使用普通的本机 SQL 手动修复架构。