web-dev-qa-db-ja.com

原因:org.flywaydb.core.api.FlywayException:検証に失敗しました。移行2の移行チェックサムの不一致

私は以下の問題の解決策を見つけようとしましたが、どれもうまくいきませんでした。私はAngular + Spring BootアプリケーションをMySQL + flywayを使用して開発しています。ここで何が間違っているのかを教えてください。

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

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

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>

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`));

enter image description here

39
user5707585

Flywayは、SQLスクリプトのチェックサムと以前に実行したチェックサムのチェックサムを比較しています。この例外は通常、Flywayによって既に適用されているSQLスクリプトを変更した場合に発生し、チェックサムの不一致を引き起こします。

これが開発の場合、データベースを削除して、移行を最初から開始できます。

実稼働環境にいる場合は、すでに適用されているSQLスクリプトを編集しないでください。今後は新しいSQLスクリプトのみを作成してください。

49
Kyle Anderson

最善の解決策は、次の手順を実行することです。

  1. V2__create_shipwreck.sqlというファイルを削除し、クリーンアップしてプロジェクトを再度ビルドします。
  2. プロジェクトを再度実行し、h2にログインして、「schema_version」というテーブルを削除します。

    drop schema_version;

  3. ここで、V2__create_shipwreck.sqlファイルをddlで作成し、プロジェクトを再実行します。

  4. これを覚えておいてください、pom.xmlにflyway-coreのバージョン4.1.2を追加してください

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

これで動作するはずです。これが役立つことを願っています。

13
Indrajeet Gour

同じ問題が発生し、データベースから完全なスキーマを削除しましたが、問題は残りました。

Flywayのrepair()コマンドを実行してこれを解決しました。

flyway.repair();

または、Flyway Mavenプラグインの場合:

mvn flyway:repair

Pom.xmlへのMavenプラグインの追加:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>5.2.4</version>
</plugin>

ところで:私は正確に何が悪かったのか見つけられませんでした。

8
dave

適用する移行とは異なる移行を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

適用される移行

V_005_five.sql
* V_004_addUserTable.sql *
V_003_three.sql
V_002_two.sql
V_001_one.sql

ここでの解決策はschema_versionから削除することです

V_005_five.sql
V_004_four.sql

また、発生したデータベースの変更を元に戻します。たとえば、スキーマが新しいテーブルを作成した場合、移行を実行する前にそのテーブルを削除する必要があります。

フライウェイを実行すると、再適用のみが行われます

V_005_five.sql
* V_004_addUserTable.sql *

新しいschema_versionは

V_005_five.sql
* V_004_addUserTable.sql *
V_003_three.sql
V_002_two.sql
V_001_one.sql

それが役に立てば幸い

7
Djalas

生産中でない場合は、データベースのflywayTableを調べて、適用されたスクリプトの名前を含む行を削除できます。

flywayTableは、このデータベースのバージョンに関する情報、既に適用されているスクリプトを含むflywayによって使用されるデータベース内のテーブルの名前を定義するプロジェクトオプションです...

1
Moebius

SQLファイルの変更が既存のスキーマに影響しないことが確実な場合は、既存のスキーマのチェックサムも更新できます。

これは、sqlファイルのわずかな変更に続いて行いました。

チェックサムの更新方法は次のとおりです。

update flyway_schema_history set checksum = '-1934991199' where installed_rank = '1';
1
Stephane

簡単な解決策は、application.propertiesのspring.datasource.url = jdbc:h2:file:〜/ dasbootを次のような新しいファイル名に変更することです:spring.datasource.url = jdbc:h2:file:〜/ dasboots

1
Eliran_Mesika

実際には別のソリューションがありますが、これは回避策であり、適切に管理されたプロジェクトでは実行しないでください。しかし、私はより良い道を下ることができない状況に遭遇しました:)

Schame_versionテーブルを更新し、実際にチェックサムを新しいものに変更できます。これにより、移行が行われますが、他の副作用が生じる可能性があります。

異なる環境(test、uat、prodなど)にデプロイする場合、より多くの環境で同じチェックサムを更新する必要が生じることがあります。また、gitflowやリリースブランチに関しては、全体を簡単に混在させることができます。

1
Márton Szabó

1-移行ファイルを削除します。データベースに2接続し、移行によって作成されたテーブルを削除します。 3-適切なSQLを使用して移行のファイルを再作成します。

0
Ahmed Chioua

Schema_versionレコードを更新して、「ローカルで解決」の値に一致するようにします。この値は、-1729781252です。

0
Bucher Arnold

私は同じ問題を抱えていましたが、これはLinuxとWindows(Macも)間のチェックサムが原因で発生したと考えています。 flywayでuse repair()コマンドを実行できます。

flyway.repair();

実稼働環境にいる場合は、移行SQLファイルを変更していないことを確認してください。 flyway.repair();を実行するとき、移行バージョンとそのチェックサムについて確信していることをフライウェイに伝え、フライウェイがそれらを修復することを意味するためです!

0
agent74

ここで、ローカルシステムでこの問題が発生したときに有効なソリューションを示します。

  1. DBのflyway_schema_historyに移動します
  2. SQL移行スクリプトを含む行を削除します
0
Deepak Nair

Flywayは、チェックサムの計算方法をバージョン3からバージョン5に変更しました。チェックサムを再計算できます。 FlywayプラグインはSpringデータソースプロパティを適切に読み取らないため、コマンドラインで手動で指定する必要があります( またはFlywayが受け入れる他のさまざまな方法の1つ )。

mvn flyway:repair -Dflyway.user=root -Dflyway.password= -Dflyway.url=jdbc:mysql://localhost:3306/mydatabase -Dflyway.table=schema_version

Flywayはチェックサムを格納するテーブルも変更したため、flyway-table=schema_versionを指定して古いテーブルを使用する必要があります。そうしないと、警告(およびバージョン6のエラー)が発生します。

[INFO] Repairing Schema History table for version 2 (Description: create sources, Type: SQL, Checksum: 2125962141)  ...
[INFO] Repairing Schema History table for version 3 (Description: create stats, Type: SQL, Checksum: 389912194)  ...
[INFO] Repairing Schema History table for version 4 (Description: add user encrypted, Type: SQL, Checksum: 182607572)  ...
0
Chloe