如何在Grails3 / Postgres中配置Flyway?

Hen*_*rik 2 grails flyway

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

dependencies {
  compile "org.flywaydb:flyway-core:4.1.2"
}
Run Code Online (Sandbox Code Playgroud)

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

environments:
    development:
        dataSource:
            dbCreate: none
Run Code Online (Sandbox Code Playgroud)

我还向以下位置添加了迁移文件:

grails-app
  conf
    db
      migration
        V1__create_tables.sql
Run Code Online (Sandbox Code Playgroud)

我还在这里阅读(https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html),可以做一些额外的配置,所以我将其添加到application.yml中:

flyway:
  enabled: true
  locations: classpath:grails-app/conf/db/migration
  sql-migration-prefix: V
  sql-migration-suffix: .sql
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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

参考 https://github.com/spring-projects/spring-boot/blob/v1.5.2.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java# L130

但是,如果gorm定义了grails DataSource,则情况并非如此-发生在启动autoconfig之后。

一种可能的解决方案是定义一个“别名” DataSourcebean,用作飞行数据源,委派给定义的gorm / grails。

@Configuration
class FlywayConfig {

    @Autowired
    DataSource dataSource

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

}
Run Code Online (Sandbox Code Playgroud)

样本:检查https://github.com/zyro23/stackoverflow-43211960/commit/c4063c900b7f96bc9ba65c84684a14a1992ca2a5

访问http:// localhost:8080 / dbconsolejdbc:h2:mem:devDb),您应该看到该TEST表已创建。