简体   繁体   中英

How to run Flyway commands in spring boot application?

I've been using flyway to handle my sql scripts migrations and am very happy with it. However, when I decided to move forward with spring boot and flyway I have stuck when I tried to run the different flyway commands from the app.

When I run my fat jar app, the migrate flyway command is automatically executed. All went well until one of the scripts failed in a linux (because of the case sensitive). So, when I wanted to execute the repair command I had no way to do it, so I had to manually remove the entry in the schema_version table.

So, my question is that if it is even possible to other flyway commands through the app without creating a custom flyway handler and just using its native integration.

I didn't find any spring boot property for command or something like this.

Any help?

There's no command property for Flyway integration. If you want you can implement your own FlywayMigrationStrategy , which is a one method interface:

public interface FlywayMigrationStrategy {

    /**
     * Trigger flyway migration.
     * @param flyway the flyway instance
     */
    void migrate(Flyway flyway);

}

You can pick up for instance a command line argument that you pass and do repair instead of migrate :

void migrate(Flyway flyway) {
    String command = env.getProperty("flyway.commamd", "migrate");
    switch (command) {
     case "migrate":
         flyway.migrate();
         break;
     case "repair":
         flyway.repair();
         break;
     default:
         throw new IllegalArgumentException("Invalid command: " + command);
 }
}

Check the docs here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM