5
votes

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

1 Answers

10
votes

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.