Last active
April 7, 2025 19:27
-
-
Save DArmstrong87/923f795790eb305803646d941a7e2c51 to your computer and use it in GitHub Desktop.
This class runs Flyway migrations upon application startup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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