org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
Flyway flyway = new SpringBootFlyway();
flyway.setDataSource(javax.sql.DataSource);
如果你配置就用配置的,如果没有配置则使用默认DataSource的。
当有spring.datasource 和 spring.flyway时,就创建Flyway对象,并使用spring.datasource中配置的DS。
如果spring.flyway.url &&spring.flyway.user有配置,则使用如下作为DS,代码中第一个判断:org.flywaydb.core.internal.util.jdbc.DriverDataSource
FlywayAutoConfiguration 类中:
@Configuration
@ConditionalOnMissingBean(Flyway.class)
@EnableConfigurationProperties({ DataSourceProperties.class, FlywayProperties.class })
public static class FlywayConfiguration
使用Flyway 对象操作。
// 校验
flyway.validate();
// 合并
flyway.migrate();
// 撤销
flyway.undo();
// 清空
flyway.clean();
// 基线
flyway.baseline();
// 替换
flyway.repair();
// 信息
flyway.info();
不懂看文档去:
https://flywaydb.org/documentation/usage/commandline/
defaultFlyway 为默认Flyway对象
flywayManager 中包括 Map
/**
* 构建Flyway
*/
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
// 使用默认Location,当然可以自定义,或者使用上传的文件
flyway.setLocations(defaultFlyway.getLocations());
flywayManager.getFlywayMap().put(tenantId, flyway);
@GetMapping("/test2")
@ResponseBody
public List test2(){
Flyway flyway = flywayManager.getFlywayMap().get(WebCacheManager.getTenantId());
// 校验
flyway.validate();
// 合并
flyway.migrate();
// 获取信息
MigrationInfo[] ms = flyway.info().all();
List list = new ArrayList(ms.length);
for(MigrationInfo m :ms){
FlywayMigrationInfo fm = new FlywayMigrationInfo();
fm.setType(m.getType().toString());
fm.setChecksum(m.getChecksum());
fm.setVersion(m.getVersion().toString());
fm.setExecutionTime(m.getExecutionTime());
fm.setDescription(m.getDescription());
fm.setScript(m.getScript());
fm.setSuccess(m.getState().toString());
fm.setInstalledOn(m.getInstalledOn());
fm.setInstalledBy(m.getInstalledBy());
fm.setInstalledRank(m.getInstalledRank());
list.add(fm);
}
return list;
}
返回值:
{
"message": "",
"data": [
{
"version": "1",
"type": "SQL",
"success": "SUCCESS",
"script": "V1__INIT_DATABASE.sql",
"installedRank": 1,
"installedOn": "2020-12-16T17:45:14.000+0000",
"installedBy": "root",
"executionTime": 60,
"description": "INIT DATABASE",
"checksum": -1283733132
},
{
"version": "2",
"type": "SQL",
"success": "SUCCESS",
"script": "V2__INIT_DATABASE2.sql",
"installedRank": 2,
"installedOn": "2020-12-16T17:45:14.000+0000",
"installedBy": "root",
"executionTime": 58,
"description": "INIT DATABASE2",
"checksum": 35166671
}
],
"code": 200
}
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `demo1` without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
分析:找到历史版本,在demo1中
原因:数据库中原来有未控制的表
end