从 Flyway 6.0(将于 2019 年第一季度发布)开始,Spring 用户可以使用它自动将所有JavaMigration
Spring beans 与 Flyway 结合使用:
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.springframework.context.ApplicationContext;
...
ApplicationContext applicationContext = ...; // obtain a reference to Spring's ApplicationContext.
Flyway flyway = Flyway.configure()
.dataSource(url, user, password)
// Add all Spring-instantiated JavaMigration beans
.javaMigrations(applicationContext.getBeansOfType(JavaMigration.class).values().toArray(new JavaMigration[0]))
.load();
flyway.migrate();
Spring Boot 无疑能够使其对用户完全透明。
参考:
- https://github.com/flyway/flyway/issues/1062#issuecomment-446279678
- https://twitter.com/snicoll/status/1072755676923998208