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...
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.
Flyway runs each migration in a separate transaction. In case of failure this transaction is rolled back.
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.
I have not enough data about your configuration
Hint: See migration file must be part of dicrectory /db/migration
Hint use a pattern like V1.0.1__name.sql 2 under scores
Hint depending on Flyway version you should start with a sql file version greater than 1.0 example 1.0.1.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With