Flyway migrations not persistent in H2 embedded database

99封情书 提交于 2020-01-13 10:18:27

问题


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.

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

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();

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.

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:


I have not enough data about your configuration

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

  2. Hint use a pattern like V1.0.1__name.sql 2 under scores

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

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



来源:https://stackoverflow.com/questions/32012241/flyway-migrations-not-persistent-in-h2-embedded-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!