Does spring.flyway.init-sqls work (as expected)?

Viewed 549

I'm trying to set the spring.flyway.init-sqls property to run a little statement before Flyway does anything with the migration scripts, but I'm struggling to make it work as I expect. I have the following in my application.yml:

spring:
  datasource.url: <some url here>
  flyway.init-sqls: SET OWNER <owner of tables created via migration scripts>

Now, in FlywayAutoConfiguration.FlywayConfiguration there's a method that looks like this:

private DataSource configureDataSource(FluentConfiguration configuration, FlywayProperties properties,
        DataSourceProperties dataSourceProperties, DataSource flywayDataSource, DataSource dataSource) {
    if (properties.isCreateDataSource()) {
        String url = getProperty(properties::getUrl, dataSourceProperties::determineUrl);
        String user = getProperty(properties::getUser, dataSourceProperties::determineUsername);
        String password = getProperty(properties::getPassword, dataSourceProperties::determinePassword);
        configuration.dataSource(url, user, password);
        if (!CollectionUtils.isEmpty(properties.getInitSqls())) {
            String initSql = StringUtils.collectionToDelimitedString(properties.getInitSqls(), "\n");
            configuration.initSql(initSql);
        }
    }
    else if (flywayDataSource != null) {
        configuration.dataSource(flywayDataSource);
    }
    else {
        configuration.dataSource(dataSource);
    }
    return configuration.getDataSource();
}

This is the only place where I can find any usage of spring.flyway.init-sqls, but I'm noticing that properties.isCreateDataSource() never returns true unless spring.flyway.url or spring.flyway.user are defined. Therefore, the code that uses init-sqls is never reached.

Is this expected behavior? I would have expected that as long as spring.datasource.url is set, it's not necessary to also define spring.flyway.url, but that seems to be the case, because it appears to work when I do this:

spring:
  datasource.url: <some url here>
  flyway:
    init-sqls: SET OWNER <owner of tables created via migration scripts>
    url: ${spring.datasource.url}

So I have a workaround, but I'd like to hear if there's something I'm missing?

0 Answers
Related