Spring 引导 1.5 到 2 迁移 - Flyway 迁移校验和不匹配
Spring Boot 1.5 to 2 migration - Flyway migration checksum mismatch
从 1.5
升级到 Spring Boot 2
时,我收到以下错误,尽管 SQL 脚本没有更改:
Migration checksum mismatch for migration version 1
-> Applied to database : 1395032327
-> Resolved locally : -175919814
Spring开机recommends
To make sure that the schema upgrade goes smoothly, please follow the
following instructions:
First upgrade your 1.5.x Spring Boot application to Flyway 4 (4.2.0 at
the time of writing), see the instructions for Maven and Gradle.
Once your schema has been upgraded to Flyway 4, upgrade to Spring Boot
2 and run the migration again to port your application to Flyway 5.
如果您不控制部署并且不能部署应用程序两次(例如,用户下载最新版本的应用程序),这就不容易实现。
问题的原因是什么,解决方案是什么?
校验和算法似乎在版本之间发生了变化。
在 Flyway 4
、
的(某些)版本中
all checksums are automatically recalculated and updated with the new algorithm on first run (Flyway #253)
我不确定这是否意味着校验和是用两个版本计算的,如果它匹配旧版本用新版本更新,或者这是否意味着它被新版本盲目覆盖。
Flyway 校验和算法:
版本 3 - 超过字节的 crc32:
bytes = resource.loadAsBytes()
...
crc32.update(bytes);
版本 5(不是逐字复制)- crc32 over lines(忽略 CR/LF,并使用 UTF-8 编码):
BufferedReader bufferedReader = new BufferedReader(new StringReader(resource.loadAsString(configuration.getEncoding())));
[...]
while ((line = bufferedReader.readLine()) != null) {
crc32.update(line.getBytes("UTF-8"));
}
解决方案
在 的答案中提供了多种解决方案。
因为必须避免手动干预,所以我使用 FlywayMigrationStrategy
和 jdbcTemplate
在启动时将校验和从固定已知值更新为 Flyway 5 所需的新固定已知值.
可能最简单的方法是在真正迁移之前执行 flyway.repair()
:
@Configuration
public class FlywayRepair {
@Bean
public FlywayMigrationStrategy repair() {
return flyway -> {
// repair each script checksum
flyway.repair();
// before migration is executed
flyway.migrate();
};
}
}
缺点是它还删除了失败的迁移。来自 Flyway.repair() Javadoc:
Repairs the Flyway schema history table. This will perform the
following actions:
- Remove any failed migrations on databases without DDL transactions (User objects left behind must still be cleaned up manually)
- Realign the checksums, descriptions and types of the applied migrations with the ones of the available migrations
从 1.5
升级到 Spring Boot 2
时,我收到以下错误,尽管 SQL 脚本没有更改:
Migration checksum mismatch for migration version 1
-> Applied to database : 1395032327
-> Resolved locally : -175919814
Spring开机recommends
To make sure that the schema upgrade goes smoothly, please follow the following instructions:
First upgrade your 1.5.x Spring Boot application to Flyway 4 (4.2.0 at the time of writing), see the instructions for Maven and Gradle.
Once your schema has been upgraded to Flyway 4, upgrade to Spring Boot 2 and run the migration again to port your application to Flyway 5.
如果您不控制部署并且不能部署应用程序两次(例如,用户下载最新版本的应用程序),这就不容易实现。
问题的原因是什么,解决方案是什么?
校验和算法似乎在版本之间发生了变化。
在 Flyway 4
、
all checksums are automatically recalculated and updated with the new algorithm on first run (Flyway #253)
我不确定这是否意味着校验和是用两个版本计算的,如果它匹配旧版本用新版本更新,或者这是否意味着它被新版本盲目覆盖。
Flyway 校验和算法:
版本 3 - 超过字节的 crc32:
bytes = resource.loadAsBytes() ... crc32.update(bytes);
版本 5(不是逐字复制)- crc32 over lines(忽略 CR/LF,并使用 UTF-8 编码):
BufferedReader bufferedReader = new BufferedReader(new StringReader(resource.loadAsString(configuration.getEncoding()))); [...] while ((line = bufferedReader.readLine()) != null) { crc32.update(line.getBytes("UTF-8")); }
解决方案
在
因为必须避免手动干预,所以我使用 FlywayMigrationStrategy
和 jdbcTemplate
在启动时将校验和从固定已知值更新为 Flyway 5 所需的新固定已知值.
可能最简单的方法是在真正迁移之前执行 flyway.repair()
:
@Configuration
public class FlywayRepair {
@Bean
public FlywayMigrationStrategy repair() {
return flyway -> {
// repair each script checksum
flyway.repair();
// before migration is executed
flyway.migrate();
};
}
}
缺点是它还删除了失败的迁移。来自 Flyway.repair() Javadoc:
Repairs the Flyway schema history table. This will perform the following actions:
- Remove any failed migrations on databases without DDL transactions (User objects left behind must still be cleaned up manually)
- Realign the checksums, descriptions and types of the applied migrations with the ones of the available migrations