当前位置: 动力学知识库 > 问答 > 编程问答 >

How to use Flyway in a Grails 3 project with Spring Boot's autoconfig

问题描述:

According to the Flyway documentation it should be possible to use Flyway in a Grails 3 project out-of-the-box:

Grails 3.x is based on Spring Boot comes with out-of-the-box integration for Flyway.

All you need to do is add flyway-core to your build.gradle:

compile "org.flywaydb:flyway-core:4.1.1"

Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup.

This doesn't work for me. Flyway does not kick in at application startup. In the logs I see some suspicious lines:

FlywayAutoConfiguration did not match

- @ConditionalOnClass found required class 'org.flywaydb.core.Flyway' (OnClassCondition)

- @ConditionalOnProperty (flyway.enabled) matched (OnPropertyCondition)

- @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)

...

Exclusions:

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

And this is how Spring's FlywayAutoConfiguration class looks like:

@Configuration

@ConditionalOnClass(Flyway.class)

@ConditionalOnBean(DataSource.class)

@ConditionalOnProperty(prefix = "flyway", name = "enabled", matchIfMissing = true)

@AutoConfigureAfter({ DataSourceAutoConfiguration.class,

HibernateJpaAutoConfiguration.class })

public class FlywayAutoConfiguration {

So it looks to me that it is not working because DataSourceAutoConfiguration is excluded from auto-config.

Is this analysis correct?

Why and where is DataSourceAutoConfiguration excluded? Supposedly somewhere deep inside of Grails, because I am not aware of any place in my code that could cause this.

How can I make the flyway integration into Grails work as promised by the Flyway docs? I know I can do it manually via resources.groovy (working code from my project, heavily inspired by Grails Flyway plugin code):

if (application.config.flyway.enabled != false) {

flyway(Flyway) { bean ->

bean.initMethod = 'migrate'

dataSource = ref('dataSource')

baselineOnMigrate = application.config.flyway.baselineOnMigrate

}

BeanDefinition sessionFactoryBeanDef = getBeanDefinition('sessionFactory')

if (sessionFactoryBeanDef) {

def dependsOnList = ['flyway'] as Set

if (sessionFactoryBeanDef.dependsOn?.length > 0) {

dependsOnList.addAll(sessionFactoryBeanDef.dependsOn)

}

sessionFactoryBeanDef.dependsOn = dependsOnList as String[]

}

}

but if possible I'd prefer the auto-config approach because it supports many flyway properties out-of-the-box and I can keep my resources.groovy tidy.

分享给朋友:
您可能感兴趣的文章:
随机阅读: