mysql - 使用 flyway 时在启动时清除数据库

标签 mysql spring spring-boot flyway

在我的例子中,我将 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/

相关文章:

spring - JNDI 不适用于 HornetQ 和 tomcat

spring-mvc - 非空对象变为空json

php - 从 mysql 数据库中选择最长的天数?

python - 从 python 和 MySQLdb 中的 Json 对象获取字符串

spring - 在spring + hibernate中保存creationTimestamp和updatedTime

spring - 无法将参数从Gradle命令行传递到Spring项目(不是Boot)

java - 参数为空时 Spring Cloud 假装行为

java - Spring项目中的org.springframework.beans.NotWritablePropertyException

mysql 分区由 varchar - 随机行为?

php - Mysql 保存/加载但没有帖子和名称字段