Since version 6.*, Flyway supports Spring bean injection into java migration files with JavaMigration
interface implemented. Here is my example:
@Component
public class V1_201809261821__some_migration extends BaseJavaMigration {
@Autowired
private SomeDAO someDAO;
@Override
public void migrate(Context context) throws Exception {
someDAO.doSomething();
}
}
When startup, it complains that:
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
v1_201809261821__some_migration (field private SomeDAO V1_201809261821__some_migration.someDAO)
┌─────┐
| someDAO (field private org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate someDAO.namedParameterJdbcTemplate)
↑ ↓
| flywayInitializer defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]
↑ ↓
| flyway defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]
↑ ↓
| v1_201809261821__some_migration (field private SomeDAO V1_201809261821__some_migration.someDAO)
└─────┘
It seems that I can't use JdbcTemplate
in Java migration files, Flyway's document shows that I can construct my own JdbcTemplate
using Context
like:
public void migrate(Context context) {
new JdbcTemplate(new SingleConnectionDataSource(context.getConnection(), true))
.execute("INSERT INTO test_user (name) VALUES ('Obelix')");
}
But unfortunately I have no control of SomeDAO
, it's from another module I can't touch.
Related versions:
Flyway: 6.0.6
Spring Boot: 2.2.0