【问题标题】:Flyway migrations not persistent in H2 embedded databaseFlyway 迁移在 H2 嵌入式数据库中不持久
【发布时间】:2015-11-07 20:07:17
【问题描述】:

我实际上正在使用 Spring Boot 编写一个小型 Web 应用程序,并希望将(嵌入式)H2 数据库与 Spring Data JPA 和 Flyway 一起用于数据库迁移。

这是我的 application.properties:

spring.datasource.url=jdbc:h2:~/database;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=org.h2.Driver

在我的 @SpringBootApplication 类的 main() 方法中,我执行以下操作:

ResourceBundle applicationProperties = ResourceBundle.getBundle("application");
Flyway flyway = new Flyway();
flyway.setDataSource(applicationProperties.getString("spring.datasource.url"), applicationProperties.getString("spring.datasource.username"), applicationProperties.getString("spring.datasource.password"));
flyway.migrate();

我添加了一个脚本,该脚本在数据库中创建了一个表 USER,Flyway 说它已正确迁移,但如果我连接到数据库,在模式 PUBLIC 中只列出了 Flyway 的 schema_versions 表。

如果我要添加另一个脚本,将基础数据插入到 USER 表中,迁移将失败,因为在我的 spring boot 应用程序重新启动后该表不存在。

谁能告诉我我的配置是否缺少?或者如果我的设置中有任何错误的假设......

【问题讨论】:

    标签: spring-data-jpa h2 flyway


    【解决方案1】:

    我没有足够的关于你的配置的数据

    1. 提示: 请参阅迁移文件必须是目录 /db/migration 的一部分

    2. 提示 在分数下使用 V1.0.1__name.sql 2 之类的模式

    3. 提示 根据 Flyway 版本,您应该从大于 1.0 的 sql 文件版本开始,例如 1.0.1。

    4. 如果您使用内存数据库,则默认 spring boot jpa 会删除您的数据库内容。请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html 第 28.3.3 节。

    【讨论】:

    • 第四个提示解决了我的问题!将spring.jpa.hibernate.ddl-auto=none 添加到我的application.properties 后,Spring 数据和flyway 正在正确完成指定的工作。非常感谢您的回复!
    • 完美运行!现在可以使用 flyway 将数据插入到 h2 数据库中。
    猜你喜欢
    • 2016-11-02
    • 2018-10-17
    • 2018-02-26
    • 2023-03-05
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2017-07-10
    相关资源
    最近更新 更多