Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlywayAutoConfiguration doesn't "see" data source

Using spring boot 2.0.0.M5 with web starter and flywaydb on the classpath. The auto-configuration debug output on one hand shows there's a data source configured as expected:

DataSourceAutoConfiguration matched:
  - @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

DataSourceAutoConfiguration.PooledDataSourceConfiguration matched:
  - AnyNestedCondition 1 matched 1 did not; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailable PooledDataSource found supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (DataSourceAutoConfiguration.PooledDataSourceCondition)
  - @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)

DataSourceConfiguration.Hikari matched:
  - @ConditionalOnClass found required class 'com.zaxxer.hikari.HikariDataSource'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
  - @ConditionalOnProperty (spring.datasource.type=com.zaxxer.hikari.HikariDataSource) matched (OnPropertyCondition)

On the other hand, the auto-config fails to configure FlywayAutoConfiguration due to "missing (?!)" data source:

FlywayAutoConfiguration:
  Did not match:
     - @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans of type javax.sql.DataSource (OnBeanCondition)
  Matched:
     - @ConditionalOnClass found required class 'org.flywaydb.core.Flyway'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
     - @ConditionalOnProperty (spring.flyway.enabled) matched (OnPropertyCondition)

Any suggestions how to fix (or at least further debug) this will be greatly appreciated!

like image 336
eric239 Avatar asked Nov 05 '17 21:11

eric239


2 Answers

Same problem with Spring Boot 2.0.2.

I don't really why it don't see it, but when manually instanciating the dataSource fixed it :

@Bean
public DataSource dataSource(//
        @Value("${spring.datasource.url}") String url, //
        @Value("${spring.datasource.username}") String username, //
        @Value("${spring.datasource.password}") String password, //
        @Value("${spring.datasource.driver-class}") String driver, //
        @Value("${spring.datasource.hikari.connection-timeout:30}") Long timeout, //
        @Value("${spring.datasource.hikari.maximum-pool-size:20}") Integer maxPoolSize  ) {

    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(url);
    config.setUsername(username);
    config.setPassword(password);
    config.setDriverClassName(driver);
    config.setConnectionTimeout(timeout);
    config.setMaximumPoolSize(maxPoolSize);

    return new HikariDataSource(config);
}
like image 151
Houillon Cyril Avatar answered Nov 13 '22 17:11

Houillon Cyril


To make flyway migration work on spring boot application start you need following:

build.gradle

    implementation 'org.springframework.boot:spring-boot-starter-jooq'
    implementation 'org.flywaydb:flyway-core'
    implementation 'mysql:mysql-connector-java'

application.yml:

spring:
    datasource:
        password: mypass   
        url: jdbc:mysql://localhost:3306/database
        username: myuser
        driver-class-name: com.mysql.cj.jdbc.Driver

src/main/resources/db/migration/V1_1_0__my_first_migration.sql

CREATE TABLE IF NOT EXISTS `employee` (

    `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `name` varchar(20),
    `email` varchar(50),
    `date_of_birth` timestamp

)ENGINE=InnoDB DEFAULT CHARSET=UTF8;

Jooq brings in dependencies such spring-boot-starter-jdbc and com.zaxxer:HikariCP (connection pooling), this makes spring detect datasource and inject flyway initialisation.

like image 36
AjitChahal Avatar answered Nov 13 '22 16:11

AjitChahal