【问题标题】:Flyway - Flyway Schema migration failedFlyway - Flyway 架构迁移失败
【发布时间】:2019-01-16 12:11:13
【问题描述】:
  • 我已经成功配置了spring boot,有一个新项目可以工作 带飞路
  • 使用 Postgres 数据库从版本 0001.0 迁移到 0008.0
  • 我已在本地手动更改脚本,但 flyway 迁移失败。

示例错误消息:

org.springframework.beans.factory.BeanCreationException: 错误 创建在类路径中定义的名称为“flywayInitializer”的bean 资源 [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: 调用 init 方法失败;嵌套异常是 org.flywaydb.core.api.FlywayException:验证失败:迁移 迁移版本 0006.0 的校验和不匹配

如何在不影响 flyway_schema_history 中的 flyway 脚本的情况下更改数据库表?

例如,我需要使用alter命令更改表名,但执行flyway迁移脚本没有失败。

任何建议,欢迎。

注意:- 我不想从表 flyway_schema_history 中删除脚本条目。

【问题讨论】:

    标签: postgresql spring-boot flyway


    【解决方案1】:

    有几种方法可以做到这一点:-

    1) 创建一个具有递增版本的新脚本文件。将您的 DDL 命令用于更改此文件中的表。然后运行迁移。

    2) 如果您不想从 schema_version 表中删除条目,您可以更改该表中的校验和值。要计算校验和,请使用从org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver 复制的以下方法。您可以将null 传递给资源参数:-

    /**
     * Calculates the checksum of this string.
     *
     * @param str The string to calculate the checksum for.
     * @return The crc-32 checksum of the bytes.
     */
    /* private -> for testing */
    static int calculateChecksum(Resource resource, String str) {
        final CRC32 crc32 = new CRC32();
    
        BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
        try {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                crc32.update(line.getBytes("UTF-8"));
            }
        } catch (IOException e) {
            String message = "Unable to calculate checksum";
            if (resource != null) {
                message += " for " + resource.getLocation() + " (" + resource.getLocationOnDisk() + ")";
            }
            throw new FlywayException(message, e);
        }
    
        return (int) crc32.getValue();
    }
    

    3) 如果您使用的是 Flyway Pro 版本 5+,则可以回滚迁移 https://flywaydb.org/getstarted/undo

    The answers here 已过时,但仍可帮助您。

    【讨论】:

      【解决方案2】:

      听起来您可能处于以下两种情况之一:

      1. 您想重新运行版本化迁移。正如 Kartik 建议的那样,flyway 的工作原理并非如此,而是创建一个新的版本化迁移来更改表。
      2. 迁移文件已被修改,您希望保留该文件并运行新文件(例如 0009.0)。在这种情况下,您可以尝试:
        1. 运行repair。这将重新计算校验和(除其他外)。
        2. 关闭validateOnMigrate 选项,如果有修改的迁移文件,该选项不会导致迁移失败。

      【讨论】:

        猜你喜欢
        • 2016-03-17
        • 2012-08-09
        • 2014-08-02
        • 2020-05-24
        • 2020-02-25
        • 2017-10-08
        • 2017-12-11
        • 2012-07-21
        • 2019-04-28
        相关资源
        最近更新 更多