Alb P
Alb P

Reputation: 51

Prevent beforeMigrate from running on up to date schema

Is it possible to prevent the beforeMigrate callback script from running when there are no migrations to run because the schema is already up to date?

Here's the code (executed on application startup):-

Flyway flyway = new Flyway();

flyway.setDataSource(url, user, password);

flyway.setLocations(scriptsLocations);
flyway.setPlaceholders(placeHolders);

flyway.setBaselineVersionAsString("7.0");
flyway.setBaselineOnMigrate(true);

flyway.migrate();

According to the log Flyway runs the beforeMigrate callback before deciding the schema is up to date and there are no migrations to run.

INFO: Flyway 4.0.3 by Boxfuse
INFO: Database: jdbc:oracle:thin:... (Oracle 11.2)
INFO: Successfully validated 8 migrations (execution time 00:00.023s)
INFO: Executing SQL callback: beforeMigrate
INFO: Current version of schema "...": 7.0.7
INFO: Schema "..." is up to date. No migration necessary.

Would like the beforeMigrate callback to run only when migrations are necessary.

Upvotes: 2

Views: 401

Answers (1)

Alb P
Alb P

Reputation: 51

Found a simple solution; use info to determine if there are pending migrations and make the call to migrate conditional based on the result: -

boolean pending = flyway.info().pending().length > 0;

if (pending) {
    flyway.migrate();
}

Upvotes: 3

Related Questions