Skip to content

Commit

Permalink
fix: onCreated should not return unwrapped datasource (#109) (#113)
Browse files Browse the repository at this point in the history
* fix: onCreated should not return unwrapped datasource

close: #96

* Update DataSourceMigrationRunnerSpec.groovy
  • Loading branch information
sdelamo authored Sep 9, 2020
1 parent e9ba2d0 commit 0fe00bc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ public DataSourceMigrationRunner(ApplicationContext applicationContext,

@Override
public DataSource onCreated(BeanCreatedEvent<DataSource> event) {
DataSource dataSource = dataSourceResolver.resolve(event.getBean());

DataSource dataSource = event.getBean();
if (event.getBeanDefinition() instanceof NameResolver) {
((NameResolver) event.getBeanDefinition())
.resolveName()
.ifPresent(name -> {
applicationContext
.findBean(FlywayConfigurationProperties.class, Qualifiers.byName(name))
.ifPresent(flywayConfig -> run(flywayConfig, dataSource));
.ifPresent(flywayConfig -> {
DataSource unwrappedDataSource = dataSourceResolver.resolve(dataSource);
run(flywayConfig, unwrappedDataSource);
});
});
}

return dataSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.micronaut.flyway

import io.micronaut.context.ApplicationContext
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

abstract class ApplicationContextSpecification extends Specification implements ConfigurationFixture {

@Shared
@AutoCleanup
ApplicationContext applicationContext = ApplicationContext.run(configuration)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.micronaut.flyway

trait ConfigurationFixture {

Map<String, Object> getConfiguration() {
Map<String, Object> m = [:]
if (specName) {
m['spec.name'] = specName
}
m
}

Map<String, Object> getJpaConfiguration(List<String> packages = ['example.micronaut']) {
[
'jpa.default.packages-to-scan' : packages,
'jpa.default.properties.hibernate.hbm2ddl.auto': 'none',
'jpa.default.properties.hibernate.show_sql' : true,
]
}

Map<String, Object> getDataSourceConfiguration(String database,
String dataSourceName = 'default',
String driverClassName = 'org.h2.Driver',
String username = 'sa',
String password = '') {
Map<String, Object> m = [:]
m['datasources.' + dataSourceName + '.url'] = driverClassName == 'org.h2.Driver' ? h2JdbcUrl(database) : ''
m['datasources.' + dataSourceName + '.username'] = username
m['datasources.' + dataSourceName + '.password'] = password
m['datasources.' + dataSourceName + '.driverClassName'] = driverClassName
m
}

String h2JdbcUrl(String dbName) {
'jdbc:h2:mem:' + dbName + ';DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE'
}

String getSpecName() {
null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.micronaut.flyway

import io.micronaut.context.annotation.Primary
import io.micronaut.context.annotation.Requires
import io.micronaut.jdbc.DataSourceResolver

import javax.inject.Singleton
import javax.sql.DataSource

class DataSourceMigrationRunnerSpec extends ApplicationContextSpecification {

@Override
String getSpecName() {
'DataSourceMigrationRunnerSpec'
}
@Override
Map<String, Object> getConfiguration() {
super.configuration +
getDataSourceConfiguration('datasourcemigrationrunner') +
getJpaConfiguration(['example.micronaut'])
}
void "DataSourceMigrationRunner::onCreated returns wrapped Datasource"() {
when:
DataSource dataSource = applicationContext.getBean(DataSource)

then:
!(dataSource instanceof ReturnedByDataSourceResolver)
}

@Primary
@Requires(property = 'spec.name', value = 'DataSourceMigrationRunnerSpec')
@Singleton
static class MockDataSourceResolver implements DataSourceResolver {
@Override
DataSource resolve(DataSource dataSource) {
new ReturnedByDataSourceResolver(dataSource)
}
}

static class ReturnedByDataSourceResolver implements DataSource {

@Delegate
private final DataSource dataSource

ReturnedByDataSourceResolver(DataSource dataSource) {
this.dataSource = dataSource
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.micronaut.runtime.event.annotation.EventListener
import org.flywaydb.core.Flyway
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

import javax.inject.Singleton
import javax.sql.DataSource

Expand Down Expand Up @@ -45,7 +44,6 @@ class EventListenerSpec extends Specification {
applicationContext.getBean(TestEventListener).migrationFinishedEvents.size() == 1
applicationContext.getBean(TestEventListener).schemaCleanedEvents.size() == 1
}

}

@Singleton
Expand Down

0 comments on commit 0fe00bc

Please sign in to comment.