Flyway迁移在H2嵌入式数据库中不是持久的 [英] Flyway migrations not persistent in H2 embedded database

查看:82
本文介绍了Flyway迁移在H2嵌入式数据库中不是持久的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm actually writing a small web application with spring boot and wanted to use a (embedded) H2 database together with Spring Data JPA and Flyway for database migration.

这是我的application.properties:

This is my 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()方法中,执行以下操作:

In the main() method of my @SpringBootApplication class I do the following:

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表.

I added a script, which creates a table USER in the database, Flyway says it is correctly migrated, but if I connect to the database, in schema PUBLIC theres only the schema_versions table of Flyway listed.

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

If I am adding another script, which inserts base data into the USER table, the migration failes, because the table is not present after a restart of my spring boot application.

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

Can anyone tell me if there is missing in my configuration? Or if there is any wrong assumption in my setup...

推荐答案

我的配置数据不足

  1. 提示: 查看迁移文件必须是dicrectory/db/migration

  1. Hint: See migration file must be part of dicrectory /db/migration

提示 在分数

提示 根据Flyway版本的不同,您应该从大于1.0的SQL文件版本(例如1.0.1)开始.

Hint depending on Flyway version you should start with a sql file version greater than 1.0 example 1.0.1.

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

Hint per default spring boot jpa drops your database content if you using a in memory database. See http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html section 28.3.3.

这篇关于Flyway迁移在H2嵌入式数据库中不是持久的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆