使用Spring Boot运行基于Flyway Java的回调 [英] Run Flyway Java-based callbacks with Spring Boot

查看:84
本文介绍了使用Spring Boot运行基于Flyway Java的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Spring Boot运行基于Flyway Java的回调?我正在转换一个现有项目,该项目在每次迁移后都会更新一些视图定义,这是由Java完成的,因为它需要一些额外的逻辑.我知道可以在pl/pgsql(我们使用的是Postgres)中完成此操作,但是已经在Java中完成并经过了测试.

Is there a way to run Flyway Java-based callbacks with Spring boot? I'm converting an existing project that after each migration updates some view definitions, and this is done by Java as it needs some extra logic. I know it could be done in pl/pgsql (we are using Postgres) but it is already done and tested in Java.

Spring启动文档表示可能,但已列出回调脚本应与迁移保存在同一目录中,也许这仅适用于基于SQL的回调.

Spring boot docs says it is possible, but it is listed that the callback scripts should live in same dir as migrations, maybe this works just for SQL based callbacks.

此代码在没有Spring Boot的情况下有效:

This code works without Spring Boot:

    Flyway flyway = new Flyway();
    flyway.setDataSource(this.getDataSource());
    flyway.setLocations("/db/migration");
    flyway.setCallbacks(new LogMaintenanceFlywayCallback());
    flyway.migrate();

我在/db/migration 中进行了几次迁移,每次迁移之后我都需要执行回调.它可以在我当前的项目中运行,并且我需要在Spring Boot中执行相同的操作(或通过其他方式获得相同的行为).

I have several migrations in /db/migration and after each one I need to execute my callback. It works in my current project and I need to do the same (or another way to get the same behavior) in Spring Boot.

推荐答案

您可以进行如下配置,并且可以使用:

You can have a configuration like this and it will work:

@Configuration
public class FlywayFactory {

    @Bean
    public FlywayMigrationInitializer flywayInitializer(Flyway flyway, FlywayCallback flywayCallback) {
        flyway.setCallbacks(flywayCallback);
        return new FlywayMigrationInitializer(flyway);
    }

    @Bean
    public FlywayCallback flywayCallback() {
        return new LogMaintenanceFlywayCallback();
    }
}

这篇关于使用Spring Boot运行基于Flyway Java的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆