在我的例子中,我将 spring-boot 与 gradle 一起使用,并通过简单地将 compile 'org.flywaydb:flyway-core'
放入 build.gradle
来添加 flyway >。
对于 test
中的模拟器运行,我想在每次运行前清除数据库。我放了一个 reset script在 /src/test/resources/db/migration/V1.0__Reset.sql
中(在 /src/main/resources/db/migration/V1.1__Init 中有真正的初始化 sql 脚本.sql
),但由于重置脚本而收到 SyntaxException,当我从 MySQL Workbench 运行它时不会发生这种情况。
如何在启动时重置或清除数据库?
-- 更新--
我尝试使用 Spring DataSourceInitializer
,但似乎 Flyway 脚本在 DS 初始化之前执行,因此它导致 Hibernate 语法错误,因为找不到表。
@Resource
DataSource ds;
@Bean
public DataSourceInitializer dbInit() throws FileNotFoundException, URISyntaxException {
public DataSourceInitializer dbInit() throws FileNotFoundException, URISyntaxException {
DataSourceInitializer re = new DataSourceInitializer();
re.setDataSource(ds);
re.setEnabled(true);
String str = "classpath:sql/V1.0__Reset.sql";
URL url = ResourceUtils.getURL(str);
org.springframework.core.io.Resource resi = new PathResource(url.toURI());
// new org.springframework.core.io.ClassPathResource(str)
re.setDatabasePopulator(new ResourceDatabasePopulator(resi));
return re;
}
最佳答案
使用 Flyway.clean()。它正是你想要的。无需编写您自己的重置脚本。
关于mysql - 使用 flyway 时在启动时清除数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744122/