【发布时间】:2021-04-20 13:24:06
【问题描述】:
正如标题所示,我正在寻找可以帮助我在加载 Springs 应用程序上下文(准确地说是持久性上下文)之前运行 Flyway 迁移的任何方法。原因是我在应用程序启动时运行的查询很少。这导致我的测试失败,因为正在对尚不存在的数据库表执行查询。我使用 H2 作为我的测试数据库。现在我只使用flyway核心依赖:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.5.0</version>
<scope>test</scope>
</dependency>
我有一个如下的 Flyway 配置类:
@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;
}
}
并且属性定义在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
我想要实现的是:1. flyway 进行迁移 2. Spring 上下文加载(按特定顺序)
【问题讨论】:
标签: spring h2 flyway spring-test