飞路迁移后运行data.sql文件 [英] Run data.sql file after flyway migration
问题描述
在我的Spring启动项目中,我通过flyway
维护sql/query
版本.由于某些原因,我需要加载一些我不想添加到飞行版本中的初始数据.对于这些数据,我正在使用flyway
脚本创建相关表.因此,要加载初始数据,我必须在flyway执行该脚本后运行我的data.sql
文件. flyway运行其脚本后,如何确保运行我的data.sql
文件?
有什么建议吗?
In my Spring boot project, I maintain sql/query
version by flyway
. For some reason, I need to load some of initial data which I don't wan to add on flyway version. For those data, I am creating related tables from flyway
scripts. So to load initial data, I must run my data.sql
file after flyway executes that scrips. How can I be sure to run my data.sql
file after flyway runs its scrips?
Any suggestion please?
推荐答案
data.sql
自动针对嵌入式数据库运行.
data.sql
gets ran automatically for embedded databases.
对于MySQL,您需要将以下属性添加到您的application.properties
文件中:
For MySQL you will need to add the following property to your application.properties
file:
spring.datasource.initialization-mode=always
要在Flyway运行完迁移后申请,您可以使用Flyway的可重复迁移,因为它们总是最后一次应用. https://flywaydb.org/documentation/migrations#repeatable-migrations
To apply after Flyway has ran migrations you could use Flyway's repeatable migrations as they are always applied last. https://flywaydb.org/documentation/migrations#repeatable-migrations
或者作为另一种选择,您可以使用CommandLineRunner
并通过编程来获取和执行SQL文件.例如:
Or as another alternative you could use a CommandLineRunner
and source and execute your SQL file programmatically. For example:
import org.springframework.boot.CommandLineRunner;
@Component
public class DatabaseMigration implements CommandLineRunner {
@Value("classpath:data.sql")
private Resource dataFile;
@Override
public void run(String... strings) {
// read file and execute with JdbcTemplate
// ...
}
}
这篇关于飞路迁移后运行data.sql文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!