Maglethong Spirr
Maglethong Spirr

Reputation: 443

Flyway db migration with Hibernate create-drop

I am starting my first springFW application and I wanted to create a series of versioned Flyway migration files for future production use. Right now I am using a local Profile and I would like to drop and recreate all tables every time I run the application, but once I do that I seem to have a problem with Flyway since it's versioned migration files will only run once.

# File: application-local.properties

spring.jpa.hibernate.ddl-auto=create-drop

# FLYWAY (FlywayProperties)
flyway.locations=classpath:db/migration
flyway.enabled=true
flyway.baseline-version= 1
flyway.sql-migration-prefix=V
flyway.sql-migration-suffix=.sql
flyway.baseline-on-migrate=true
flyway.validate-on-migrate=false

Ideally I would like to configure Flyway to run the versioned migrates every time while I am in the local profile so I could reuse the same files on production later on. Truth is, I don't know if that is the correct way of thinking since I am very new to those technologies.

// File: FlywayConfig.java

@Configuration
@Order(Ordered.LOWEST_PRECEDENCE)
@Import(FlywayAutoConfiguration.FlywayConfiguration.class)
public class FlywayConfig { }

Upvotes: 1

Views: 2915

Answers (1)

Monzurul Shimul
Monzurul Shimul

Reputation: 8396

You can override the Flyway configuration like this:

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

    return flyway;
}

Or if you are using spring boot greater than 1.3 you can use FlywayMigrationStrategy

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

    return strategy;
}

Upvotes: 4

Related Questions