java 使用 Spring Boot 修复 Flyway

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37462550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:32:08  来源:igfitidea点击:

Flyway repair with Spring Boot

javaspringmavenspring-bootflyway

提问by Jean-Fran?ois Beauchef

I don't quite understand what I am supposed to do when a migration fails using Flyway in a Spring Boot project.

我不太明白当在 Spring Boot 项目中使用 Flyway 迁移失败时我应该做什么。

I activated Flyway by simply adding the Flyway dependency in my pom.xml. And everything works fine. My database scripts are migrated when I launch the Spring Boot app.

我通过简单地在我的pom.xml. 一切正常。当我启动 Spring Boot 应用程序时,我的数据库脚本会被迁移。

But I had an error in one of my scripts and my last migration failed. Now when I try to migrate, there is a "Migration checksum mismatch". Normally, I would run mvn flyway:repair, but since I am using Spring Boot, I am not supposed to use the Flyway Maven plug-in. So what am I supposed to do?

但是我的一个脚本中出现错误并且上次迁移失败了。现在,当我尝试迁移时,会出现“迁移校验和不匹配”。通常,我会运行mvn flyway:repair,但由于我使用的是 Spring Boot,我不应该使用 Flyway Maven 插件。那我该怎么办?

采纳答案by Daniel K?fer

there are several ways to perform a repair on the database. I personally prefer the simple SQL statement.

有几种方法可以对数据库执行修复。我个人更喜欢简单的 SQL 语句。

SQL Statement:

SQL语句:

Just delete the row with the failed migration. After that you can run the migration again.

只需删除迁移失败的行。之后,您可以再次运行迁移。

Run flyway directly

直接运行flyway

You can install Flyway local and run flyway repairin the console

可以flyway repair在本地安装 Flyway 并在控制台中运行

Use the Flyway Maven Plugin

使用 Flyway Maven 插件

Add the Flyway Maven Pluginto your pom and run mvn flyway:repair. I don't think this contradict with the Spring Boot concept.

Flyway Maven 插件添加到你的 pom 并运行mvn flyway:repair. 我不认为这与 Spring Boot 概念相矛盾。

Extend Spring Boot

扩展 Spring Boot

Spring Boot will call Flyway.migrate()to perform the database migration. If you would like more control, provide a @Beanthat implements FlywayMigrationStrategy.

Spring Boot 将调用Flyway.migrate()执行数据库迁移。如果您想要更多控制,请提供一个@Bean实现FlywayMigrationStrategy.

In the FlywayMigrationStrategyyou can call the migrate or repair method from flyway. More Information is available in the Spring Boot Reference Guide.

FlywayMigrationStrategy您可以从 flyway 调用迁移或修复方法。更多信息可在Spring Boot 参考指南 中找到

I don't think the FlywayMigrationStrategyin the application is the right place to repair the database. A failed migration is a exception and should be handle outside the application.

我认为FlywayMigrationStrategy应用程序中的 不是修复数据库的正确位置。失败的迁移是一个例外,应该在应用程序之外处理。

回答by felipealvesgnu

Flyway Maven Plugin

Flyway Maven 插件

Just to add this info to @Daniel's answer

只是为了将此信息添加到@Daniel 的回答中

1.

1.

      ...
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>4.1.0</version>
            <configuration>
                <url>jdbc:mysql://localhost:3306</url>
                <user>root</user>
                <password>root</password>
                <schemas>
                    <schema>[your_schema]</schema>
                </schemas>
            </configuration>
        </plugin>
      ...

2.

2.

mvn flyway:clean

mvn flyway:干净

3.

3.

mvn flyway:repair

mvn flyway:修复

PS.: if the step 2 and 3 don't work change the order.

PS.:如果第 2 步和第 3 步不起作用,请更改顺序。

More info on maven goals: https://flywaydb.org/documentation/maven/

有关 maven 目标的更多信息:https: //flywaydb.org/documentation/maven/

回答by Javadroider

You can do it through code by declaring the following bean.

您可以通过声明以下 bean 来通过代码来实现。

@Bean
public FlywayMigrationStrategy cleanMigrateStrategy() {
    return flyway -> {
        flyway.repair();
        flyway.migrate();
    };
}

回答by BishalG

When database migration fails, the migration is marked as failed in the schema history table (i.e flyway_schema_history)indicating manual database cleanup may be required. But if database supports DDL transactions, the migration is rolled back automatically and nothing is recorded in the schema history table. PostgreSQL, Amazon Redshift, MS SQLare few of the databases which support DDL transactions whereas Oracle Database, MySQL, MariaDB, Amazon Auroradoes not support DDL transactions.

当数据库迁移失败时,迁移在模式历史表中被标记为失败,(i.e flyway_schema_history)表明可能需要手动清理数据库。但如果数据库支持 DDL 事务,则迁移会自动回滚,并且架构历史表中不会记录任何内容。PostgreSQLAmazon RedshiftMS SQL是少数支持DDL事务,而数据库Oracle DatabaseMySQLMariaDBAmazon Aurora不支持DDL事务。

In case of failed migration entries, there are several options to repair it (only applicable for databases that do NOT support DDL transactions) as described by @daniel-k?fer. I want to add another (may be easier way) to deal with failed migrations.

如果迁移条目失败,有几个选项可以修复它(仅适用于不支持 DDL 事务的数据库),如@daniel-k?fer 所述。我想添加另一个(可能更简单的方法)来处理失败的迁移。

There are several callbackssupported by flyway, afterMigrateErroris one of them. If we add a sql file with name afterMigrateError.sqlthen, it will be executed after each failed migrate runs. Therefore, we can simply create a file afterMigrateError.sqlon default location of database migration folder (resources/db/migration) with sql command to remove failed migrations from flyway_schema_historytable.

flyway支持的回调有几种,afterMigrateError就是其中之一。如果我们添加一个带有名称的 sql 文件afterMigrateError.sql,它将在每次失败的迁移运行后执行。因此,我们可以简单地使用 sql 命令在afterMigrateError.sql数据库迁移文件夹 ( resources/db/migration) 的默认位置创建一个文件,以从flyway_schema_history表中删除失败的迁移。

The sql command afterMigrateError.sqlcan be as mentioned below:

sql命令afterMigrateError.sql可以如下所述:

DELETE IGNORE FROM flyway_schema_history WHERE success=0;

This command looks for the table flyway_schema_historyif it exists otherwise it will do no changes. Then it simply looks for the rows which has successcolumn with 0entry (actually this happen if migration fails , all successful migration will have value 1in success column), then delete such entries. Now, we can simply change our latest migration file and correct it and run again.

此命令查找表(flyway_schema_history如果存在),否则不会进行任何更改。然后它只是查找success具有0条目的列的行(实际上,如果迁移失败会发生这种情况,所有成功的迁移都将1在成功列中具有值),然后删除这些条目。现在,我们可以简单地更改我们最新的迁移文件并更正它并再次运行。

回答by xilef

Install flyway locally as said above, change directory into the installation then run (example for H2):

如上所述在本地安装 flyway ,将目录更改为安装然后运行(例如 H2):

./flyway -url=jdbc:h2:/Users/mugo/dev/h2/das-boot -user=sa -password= repair