veilupearl
veilupearl

Reputation: 929

Flyway schema migration failed

Error message:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Validate failed: Migration checksum mismatch for migration version 0006.0

How to alter the database tables without affecting Flyway scripts from the flyway_schema_history?

For example, I need to change the table name using alter command, but executing the Flyway migration script without failed.

Note: I don't want to remove the script entries from the table flyway_schema_history.

Upvotes: 3

Views: 8418

Answers (3)

Kartik
Kartik

Reputation: 7907

There are a few ways to do this:-

  1. Create a new script file with incremented version. Put your DDL commands to alter the table in this file. Then run migration.

  2. If you don't want to delete the entry from the schema_version table, you can change the checksum value in that table. To calculate checksum, use the following method copied from org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver. You can pass null for resource parameter:-

    /**
     * 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();
    }
    
  3. If you are using Flyway Pro version 5+, you can rollback the migration https://flywaydb.org/getstarted/undo.

    The answers here are outdated but can still help you.

Upvotes: 4

jmonloop
jmonloop

Reputation: 178

To solve this error locally without dropping your whole db:

  1. Fix the migration error which caused the root problem
  2. Disconnect your db server
  3. Open the table "flyway_schema_history" which would be created automatically
  4. Delete the rows with the versions that are causing the mismatch problem
  5. Open the tables that have columns depending on the conflict migrations and drop those columns (if needed)
  6. Run again your db server with the new migrations

Upvotes: 0

Hamish Carpenter
Hamish Carpenter

Reputation: 851

It sounds like you might be in one of two situations:

  1. You want to re-run a versioned migration. This isn't really how flyway works, as Kartik has suggested, create a new versioned migration to alter the table.
  2. A migration file has been modified and you want to leave it that way and run new ones (eg 0009.0). In this situation you can try:
    1. Run repair. Which will recalculate the checksums (among other things).
    2. Turn off the validateOnMigrate option which will not fail a migration if there are modified migration files.

Upvotes: 2

Related Questions