As the title suggests, I am looking for any means that could help me run Flyway migrations before Springs application context (persistence context to be precise) is loaded. The reason for that is I have few queries that run at the startup of the application. This leads to my tests failing as queries are being executed on database tables that do not yet exist. I am using H2 as my test database. Right now I am using only flyway core dependency:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.5.0</version>
<scope>test</scope>
</dependency>
and I have a single Flyway configuration class as follows:
@Configuration
class FlywayConfig {
private static final String resourcePath = "classpath:flyway/migrations";
private static final String sampleDataPath = "classpath:flyway/sample_data";
@Bean
Flyway flyway(@Value("${spring.datasource.url}") String dataSource,
@Value("${spring.datasource.username}") String username,
@Value("${spring.datasource.password}") String password) {
FluentConfiguration fluentConfiguration = Flyway.configure().dataSource(dataSource, username, password);
fluentConfiguration.locations(resourcePath, sampleDataPath);
Flyway flyway = fluentConfiguration.load();
return flyway;
}
}
and the properties are defined in application.yml
spring:
datasource:
username: sa
password: sa
url: 'jdbc:h2:mem:testdb;Mode=Oracle;IGNORE_CATALOGS=TRUE;DB_CLOSE_DELAY=-1;'
platform: h2
h2:
console:
enabled: true
jpa:
show-sql: true
What I would like to achieve is that: 1. flyway does migration 2. Spring context loads up (in that particular order)