数据库改动版本控制工具flyway
官网地址:https://documentation.red-gate.com/fd/quickstart-how-flyway-works-184127223.html
为什么选择flyway
- 官网给的信息:
They allow you to:
Recreate a database from scratch
Make it clear at all times what state a database is in
Migrate in a deterministic way from your current version of the database to a newer one
springboot整合flyway
官网给的地址:http://www.masterspringboot.com/programming/various/flyway-tutorial-for-spring-boot-users/
核心源码阅读路径
执行的核心代码位置入口
spring-boot-autoconfigure包中:
org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer->afterPropertiesSet->migrate方法
一般会见到一些探测的问题,是在org.flywaydb.core.Flyway#doValidate类中里面做的
- 常见的问题
Caused by: org.flywaydb.core.api.FlywayException: Validate failed:
Detected resolved migration not applied to database: 001.002.001.001
具体的而校验逻辑是在:org.flywaydb.core.internal.command.DbValidate#validate 这个类进行的
校验过程中创建MigrationInfoServiceImpl,其中加载已有脚本是在org.flywaydb.core.internal.info.MigrationInfoServiceImpl#refresh这个方法执行的时候进行的
- 这块的逻辑是根据context的配置信息获取指定classpath的SQL脚本文件
Collection<ResolvedMigration> resolvedMigrations = migrationResolver.resolveMigrations(context);
- SQL脚本是否已经执行标识,后面新增的保存近pendingResolverVersioned。如果已经执行过的序号中间新增会报错
ResolvedMigration resolvedMigration = resolvedVersioned.get(Pair.of(av.getLeft().getVersion(), av.getLeft().getType().isUndo()));
if (resolvedMigration != null) {
pendingResolvedVersioned.remove(resolvedMigration);
}