Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway migrations not persistent in H2 embedded database

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

like image 430
justus Avatar asked Aug 14 '15 14:08

justus


People also ask

How do I make my H2 database persistent?

If we want to persist the data in the H2 database, we should store data in a file. To achieve the same, we need to change the datasource URL property. In the above property, the sampledata is a file name.

Are Flyway migrations transactional?

Flyway runs each migration in a separate transaction. In case of failure this transaction is rolled back.

Does Flyway migrate data?

Flyway automatically discovers migrations on the filesystem and on the Java classpath. To keep track of which migrations have already been applied when and by whom, Flyway adds a schema history table to your schema.


1 Answers

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.

like image 69
Florian E. Avatar answered Oct 05 '22 23:10

Florian E.