3
votes
  • I have successfully configured spring boot with a new project to work with flyway
  • Migrated with the Postgres database from the version 0001.0 to 0008.0
  • I have made manually alter the script in local but flyway migration getting failed.

Sample 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 script 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.

Any suggestions, Kindly appreciated.

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

2

2 Answers

4
votes

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.

2
votes

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.