I'm learning Flyway migration tool, and I don't have clear the concept of checksums. Could someone explain me what is? how is it calculated, or how can it be changed?
I understand the repair command re-calculate the checksum, I do not understand how it differs.
Thanks!
The checksum field in Flyway is a verification mechanism ensuring that migration scripts haven't been changed since they were applied to the database. They guarantee that all instances of your application will have the same database structure. You can switch off verification, but I would not recommend you to do so. To answer your questions:
What is?
Just google what is a checksum. Wikipedia
How is it calculated?
For SQL migrations, Flyway uses CRC32 class to calculate the checksum. For the exact code, see below.
How can it be changed?
The checksum of the migration will be changed once the binary content of your migration is modified, even by one byte or bit of data. It can also change between different versions of Flyway. If you want to change the checksum field in the DB then you need to calculate the checksum for the new version of your migration file and then change the value in the DB. However, I wouldn't recommend to do that. You shouldn't need to do that and the fact that you want to change it may indicate that you are doing something wrong. Generally, if you need to fix checksums in the DB, then you can use a Repair operation via Flyway itself. Anyway, the calculation code for the checksum is quite simple (courtesy of Flyway's source code):
/**
* 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();
}