gpt4 book ai didi

postgresql - Flyway - Flyway 架构迁移失败

转载 作者:行者123 更新时间:2023-11-29 14:31:07 26 4
gpt4 key购买 nike

  • 我已经成功配置了 spring boot 和一个可以工作的新项目带飞路
  • 使用 Postgres 数据库从 0001.0 版本迁移到 0008.0
  • 我已经在本地手动更改了脚本但是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 version 0006.0

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

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

任何建议,不胜感激。

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

最佳答案

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

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已经过时,但仍然可以为您提供帮助。

关于postgresql - Flyway - Flyway 架构迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51760052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com