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

jus*_*tus 5 h2 flyway spring-data-jpa

我实际上是在编写一个带有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
Run Code Online (Sandbox Code Playgroud)

在我的@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();
Run Code Online (Sandbox Code Playgroud)

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

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

任何人都可以告诉我我的配置是否丢失了吗?或者如果我的设置中有任何错误的假设......

Flo*_* E. 10

我没有足够的有关您的配置的数据

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

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

  3. 提示取决于Flyway版本,您应该从大于1.0示例1.0.1的sql文件版本开始.

  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`后,春天的数据和飞行路线都做正确的指定工作.非常感谢您的回复! (6认同)