Issue
We have a spring boot service that does a flyway migration and everything works properly. Now for a new deployment use case the question came up if there is an java option (parameter or something comparable) to start the flyway migration only without starting up the actual service.
Solution
Basically I implemented it myself by utilizing a flyway callback and simply shutting down the application after migration is finished successfully. The whole process is steered by a separate config parameter.
This is the callback
public class PostMigrationCallback implements Callback {
private final FlywayCustomProperties flywayCustomProperties;
private final ApplicationContext applicationContext;
public PostMigrationCallback(FlywayCustomProperties flywayCustomProperties, ApplicationContext applicationContext) {
this.flywayCustomProperties = flywayCustomProperties;
this.applicationContext = applicationContext;
}
@Override
public boolean supports(Event event, Context context) {
if (event.getId().equalsIgnoreCase("afterMigrate") && flywayCustomProperties.isMigrationOnly()) {
log.info("Service is going to shutdown as configuration was set to spring.flyway.migrate-only=true and schema was migrated successfully");
int exitCode = SpringApplication.exit(applicationContext, () -> 0);
System.exit(exitCode);
}
return false;
}
@Override
public boolean canHandleInTransaction(Event event, Context context) {
return false;
}
@Override
public void handle(Event event, Context context) {
}
}
And here we have the config parameter
@Data
@Validated
@ConfigurationProperties(prefix = "spring.flyway")
public class FlywayCustomProperties {
@NotNull
private boolean migrationOnly;
}
Answered By - hecko84
Answer Checked By - Robin (JavaFixing Admin)