Run data.sql file after flyway migration

帅比萌擦擦* 提交于 2020-03-23 08:07:09

问题


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?


回答1:


data.sql gets ran automatically for embedded databases.

For MySQL you will need to add the following property to your application.properties file:

spring.datasource.initialization-mode=always

Edit:

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

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


来源:https://stackoverflow.com/questions/57708913/run-data-sql-file-after-flyway-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!