Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run flyway:clean before migrations in a SpringBoot app?

I am using Springboot and Flyway. The migrations work just fine but I wanted to be able perform a clean flyway command when the application context gets loaded with test profile.

Is it possible to configure SpringBoot to do clean and then migrate if active profile is test?

like image 205
Barbadoss Avatar asked Jun 14 '15 14:06

Barbadoss


People also ask

How do you skip Flyway migration?

The hotfix migration can be deployed with Flyway with skipExecutingMigrations=true . The schema history table will be updated with the new migration, but the script itself won't be executed again. skipExecutingMigrations can be used with with cherryPick to skip specific migrations.

How does Flyway work with spring boot?

Spring Boot comes with out-of-the-box integration for Flyway. Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup. You can then configure a good number of Flyway properties directly from your application. properties or application.


2 Answers

You can overwrite the Flyway autoconfiguration like this:

@Bean
@Profile("test")
public Flyway flyway(DataSource theDataSource) {
    Flyway flyway = new Flyway();
    flyway.setDataSource(theDataSource);
    flyway.setLocations("classpath:db/migration");
    flyway.clean();
    flyway.migrate();

    return flyway;
}

In Spring Boot 1.3 (current version is 1.3.0.M1, GA release is planned for September), you can use a FlywayMigrationStrategy bean to define the actions you want to run:

@Bean
@Profile("test")
public FlywayMigrationStrategy cleanMigrateStrategy() {
    FlywayMigrationStrategy strategy = new FlywayMigrationStrategy() {
        @Override
        public void migrate(Flyway flyway) {
            flyway.clean();
            flyway.migrate();
        }
    };

    return strategy;
}
like image 78
dunni Avatar answered Sep 18 '22 08:09

dunni


in more recent versions of spring boot (eg 2.0.2) you can use the property spring.flyway.clean-on-validation-error if you want to use clean because of a change in the sql files

like image 31
stelios.anastasakis Avatar answered Sep 20 '22 08:09

stelios.anastasakis