问题
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