如何在Grails3/Postgres中配置Flyway? [英] How do I configure Flyway in Grails3 / Postgres?

查看:58
本文介绍了如何在Grails3/Postgres中配置Flyway?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Flyway为我的Grails 3.2.8应用程序运行迁移.根据 https://flywaydb.org/documentation/plugins/grails ,应该只需要在build.gradle中添加一个依赖项:

I am trying to use Flyway to run migrations for my Grails 3.2.8 application. According to https://flywaydb.org/documentation/plugins/grails one should just need to add a dependency to build.gradle:

dependencies {
  compile "org.flywaydb:flyway-core:4.1.2"
}

因为我希望Flyway生成我的架构,所以我还编辑了application.yml以不生成域对象.如果我没有此设置,Grails将会生成我的表格-而不是Flyway.

As I want Flyway to generate my schema I have also edited application.yml to not have domain object generated. If I do not have this setting Grails will generate my tables - not Flyway.

environments:
    development:
        dataSource:
            dbCreate: none

我还将迁移文件添加到:

I have also added a migration file to:

grails-app
  conf
    db
      migration
        V1__create_tables.sql

我也在这里阅读了( https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html )可以进行一些额外的配置,因此我将其添加到application.yml:

I also read here (https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html) that some extra configuration could done so I added this to application.yml:

flyway:
  enabled: true
  locations: classpath:grails-app/conf/db/migration
  sql-migration-prefix: V
  sql-migration-suffix: .sql

我也尝试过不添加任何配置.我似乎缺少了什么?

I have also tried without any of my added configurations. I seem to be missing something?

推荐答案

默认情况下,flyway的spring-boot自动配置依赖于在自动配置时可用的单个DataSource bean.

spring-boot auto-configuration of flyway relies by default on one single DataSource bean being available at the time of auto-configuration.

参考但是,如果gorm定义了grails DataSource,则情况并非如此-在启动自动配置后发生.

however, that is not the case if gorm defines the grails DataSource - that happens after boot autoconfig.

一种可能的解决方案是定义一个别名" DataSource bean,它充当飞行数据源,委派给定义的gorm/grails.

one possible solution is to define an "alias" DataSource bean that acts as the flyway dataSource, delegating to the gorm/grails defined one.

@Configuration
class FlywayConfig {

    @Autowired
    DataSource dataSource

    @Bean
    @FlywayDataSource
    DataSource flywayDataSource() {
        return dataSource
    }

}

示例:检查 https://github.com/zyro23/stackoverflow-43211960/commit /c4063c900b7f96bc9ba65c84684a14a1992ca2a5

访问 http://localhost:8080/dbconsole (jdbc:h2:mem:devDb),您应该看到表已创建.

visiting http://localhost:8080/dbconsole (jdbc:h2:mem:devDb) you should see that the TEST table has been created.

这篇关于如何在Grails3/Postgres中配置Flyway?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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