Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flyway - The meaning of the concept of checksums

Tags:

flyway

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!

like image 808
AleGallagher Avatar asked Apr 06 '17 23:04

AleGallagher


People also ask

How does Flyway generate the checksum?

Flyway uses the CRC32 algorithm for checksum generation.

What is checksum in migration?

What is a checksum calculation? When you migrate data, you need to ensure whether the whole process was done correctly. The method performing that is called a checksum calculation. Checksum is calculated by using SQL statements, fired at both ends - source and target.

How are checksums calculated?

To calculate the checksum of an API frame: Add all bytes of the packet, except the start delimiter 0x7E and the length (the second and third bytes). Keep only the lowest 8 bits from the result. Subtract this quantity from 0xFF.


1 Answers

Checksum field in Flyway forming a part of verification mechanism ensuring that migration scripts haven't been changed since they applied to the database. This will guaranty that all instances of your application will have same database structure (content). You can switch off verification, but I will not recommend you to do so. Answering you questions:

What is?

Just google what is checksum. Wikipedia

How is it calculated?

For SQL migrations Flyway uses CRC32 class to calculate the checksum. For exact code see below.

How can it be changed?

The checksum of the migration will be changed once the binary content of you migration modified. If you want to change checksum field in the DB when 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 doing something wrong. Anyway, the calculation code for the checksum is quite simple (with courtesy of 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();
        }

like image 67
Mikhail Chibel Avatar answered Sep 26 '22 10:09

Mikhail Chibel