在Spring Boot应用程序中使用Flyway时,如何在H2中加载初始数据? [英] How to load initial data in H2 when using flyway on spring boot application?
问题描述
我正在使用Flyway进行迁移以创建Spring Boot应用程序,并希望将内存数据库用于开发配置文件,但是问题是每次我重新启动应用程序时数据都会丢失.因此,当我的应用程序在开发配置文件中启动时,我需要插入一些数据.我试图在应用程序启动时在src/main/resource上放置一个名为data.sql
的文件,以弹簧加载它,但它不起作用(它没有运行脚本).我试图将INIT=runscript from 'classpath:data.sql'
放在h2 url中,但是它试图在Flyway迁移执行之前运行它,因此该表尚不存在.谁能给我另一种方法吗?
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?
我的application.yml:
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
推荐答案
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.
可以找到一个示例 查看全文