Flyway.API操作&源码分析

 对象创建

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/

多数据源操作实例

在数据源代理处创建Flyway

defaultFlyway 为默认Flyway对象

flywayManager 中包括 Map<Long,Flyway> flywayMap 属性

/**

 * 构建Flyway

 */

Flyway flyway = new Flyway();

flyway.setDataSource(dataSource);

// 使用默认Location,当然可以自定义,或者使用上传的文件

flyway.setLocations(defaultFlyway.getLocations());

flywayManager.getFlywayMap().put(tenantId, flyway);

 给指定租户migrate并返回版本信息

@GetMapping("/test2")
@ResponseBody
public List<FlywayMigrationInfo> test2(){
	Flyway flyway = flywayManager.getFlywayMap().get(WebCacheManager.getTenantId());
	// 校验
	flyway.validate();
	// 合并
	flyway.migrate();
	// 获取信息
	MigrationInfo[] ms =  flyway.info().all();
	
	List<FlywayMigrationInfo> list = new ArrayList<FlywayMigrationInfo>(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
}

错误:Found non-empty schema(s) `demo1` without schema history table

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
*************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.configureProperties(FlywayAutoConfiguration.java:255) The following method did not exist: &#39;org.flywaydb.core.api.configuration.FluentConfiguration org.flywaydb.core.api.configuration.FluentConfiguration.oracleSqlplus(boolean)&#39; The calling method&#39;s class, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration, was loaded from the following location: jar:file:/D:/repository/org/springframework/boot/spring-boot-autoconfigure/3.1.0/spring-boot-autoconfigure-3.1.0.jar!/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class The called method&#39;s class, org.flywaydb.core.api.configuration.FluentConfiguration, is available from the following locations: jar:file:/D:/repository/org/flywaydb/flyway-core/9.20.1/flyway-core-9.20.1.jar!/org/flywaydb/core/api/configuration/FluentConfiguration.class The called method&#39;s class hierarchy was loaded from the following locations: org.flywaydb.core.api.configuration.FluentConfiguration: file:/D:/repository/org/flywaydb/flyway-core/9.20.1/flyway-core-9.20.1.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration and org.flywaydb.core.api.configuration.FluentConfiguration Disconnected from the target VM, address: &#39;127.0.0.1:52541&#39;, transport: &#39;socket&#39; Process finished with exit code 1 怎么回事
07-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闲猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值