Guilherme Melo
Guilherme Melo

Reputation: 199

How to load initial data in H2 when using flyway on spring boot application?

I'm creating a spring boot application using Flyway to migration and want to use a memory database for development profile, but the problem is that data is lost every time I restart application. So I need to insert some data when my application start in development profile. I tried to put a file called data.sql on src/main/resource to spring load it when application starts but it doesnt work (It didnt run the script). I tried to put INIT=runscript from 'classpath:data.sql' in the h2 url but it tries to run it before Flyway migration execution so the tables doesnt exist yet. Can anyone give me an other way to do it?

My application.yml:

spring:
  datasource:
    url: jdbc:h2:mem:testdb;IFEXISTS=FALSE
    username: sa
    password:
    driver-class-name: org.h2.Driver
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
  flyway:
    enabled: true

Upvotes: 2

Views: 4028

Answers (2)

Zakariae BEN ALLAL
Zakariae BEN ALLAL

Reputation: 92

i have the same probleme to fix it, i added this configuration : enter image description here

For me the configuraton tyou need to add to fix INSERT by flyway is : ddl-auto: none

because jpa try to update database after flyway it's weird but it's work for me

Thank you for your help

Upvotes: -1

R.G
R.G

Reputation: 7131

Per documentation , a profile-specific customised flyway spring.flyway.locations can be configured. The profile-specific scripts runs when that profile is active . So a dev profile configured will work on this requirement.

The initialisation script can be placed as part of migration folder which will run and populate the db.

An example can be found here

Upvotes: 6

Related Questions