Skip to content

Instantly share code, notes, and snippets.

@DArmstrong87
Last active April 7, 2025 19:27
Show Gist options
  • Save DArmstrong87/923f795790eb305803646d941a7e2c51 to your computer and use it in GitHub Desktop.
Save DArmstrong87/923f795790eb305803646d941a7e2c51 to your computer and use it in GitHub Desktop.
This class runs Flyway migrations upon application startup
package com.myproject.mypackage.config;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FlywayMigrationConfig {
// These values come from application.properties
@Value("${spring.flyway.url}")
private String flywayUrl;
@Value("${spring.flyway.user}")
private String flywayUser;
@Value("${spring.flyway.password}")
private String flywayPassword;
@Value("${spring.flyway.enabled}")
private Boolean flywayEnabled;
@Bean
public CommandLineRunner runFlywayMigration() {
if (flywayEnabled) {
return args -> {
Flyway flyway = Flyway.configure()
.dataSource(flywayUrl, flywayUser, flywayPassword)
.baselineOnMigrate(true)
.load();
flyway.migrate(); // Run the migrations
System.out.println("Flyway migrations completed for <This Service>.");
};
} else {
return args -> {
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment