Flyway 迁移在 H2 嵌入式数据库中不持久

Flyway migrations not persistent in H2 embedded database

我实际上正在编写一个带有 spring 引导的小型 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 class 的 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();

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

如果我添加另一个脚本,将基础数据插入用户 table,则迁移失败,因为 table 在我的 spring 引导重启后不存在申请。

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

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

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

  2. 的一部分
  3. 提示 使用类似 V1.0.1__name.sql 2 under scores

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

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