我正在学习Flyway迁移工具,我没有明确校验和的概念.有人可以解释一下是什么吗?它是如何计算的,或者如何改变?
我理解修复命令重新计算校验和,我不明白它是如何不同的.
谢谢!
Flyway中的校验和字段构成验证机制的一部分,确保迁移脚本自应用于数据库后未发生更改.这将保证您的应用程序的所有实例都具有相同的数据库结构(内容).您可以关闭验证,但我不建议您这样做.回答你的问题:
什么是?
只是google什么是校验和.维基百科
它是如何计算的?
对于SQL迁移,Flyway使用CRC32类来计算校验和.具体代码见下文.
怎么改变?
一旦迁移的二进制内容被修改,迁移的校验和将被更改.如果要在迁移文件的新版本计算校验和时更改数据库中的校验和字段,然后更改数据库中的值.但是,我不建议这样做.您不应该这样做,而您想要更改它的事实可能表明您做错了什么.无论如何,校验和的计算代码非常简单(由Flyway源代码提供):
/**
* 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();
}
Run Code Online (Sandbox Code Playgroud)
为了计算任意文件的flyway校验和,我使用以下代码:
import java.util.*;
import org.flywaydb.core.internal.resource.filesystem.*;
import org.flywaydb.core.internal.resolver.*;
import java.nio.charset.*;
public class Program {
public static void main( String[] args ) throws Exception{
String filename="/path/to/migration/V8_test.sql";
FileSystemResource r = new FileSystemResource(null, filename,Charset.forName("UTF-8"));
int cs = ChecksumCalculator.calculate(r);
System.out.println(cs);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码的唯一依赖项是org.flywaydb:flyway-core:6.4.1
| 归档时间: |
|
| 查看次数: |
5421 次 |
| 最近记录: |