Flyway - The Meaning of Checksums

I am researching the Flyway migration tool and I have no clear concept of checksums . Can someone explain to me what this is? how is it calculated or how can it be changed?

I understand that the restore command recalculates the checksum, I don't understand how it is different.

Thank!

+3


source to share


1 answer


The checksum field in Flyway is part of the validation mechanism, ensuring that the migration scripts have not changed since they were applied to the database. This ensures that all instances of your application will have the same database structure (content). You can turn off the check, but I won't recommend that you do that. Answering your questions:

What?

Just google what the checksum is. Wikipedia

How is it calculated?



For SQL migration, Flyway uses the CRC32 class to calculate the checksum. See below for the exact code.

How can this be changed?

The migration checksum will be changed after you change the binary content of your migration. If you want to change the checksum field in the DB, when you need to calculate the checksum for the new version of your migration file, then change the value in the DB. However, I would not recommend doing this. You don't need to do this, and the fact that you want to change it may indicate that you are doing something wrong. Anyway, the checksum calculation code is pretty simple (courtesy of the Flyway 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();
    }

      

+5


source







All Articles