Skip to content

Commit

Permalink
Cleanup Flyway Config
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Apr 29, 2023
1 parent 2c6c42b commit 6cf647c
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Map<String, Collection<Callback>> getCallbacks()
*/
private Collection<Callback> callbacksForDataSource(String dataSourceName)
throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
final Optional<List<String>> callbackConfig = flywayBuildConfig.getConfigForDataSourceName(dataSourceName).callbacks;
final Optional<List<String>> callbackConfig = flywayBuildConfig.getConfigForDataSourceName(dataSourceName).callbacks();
if (!callbackConfig.isPresent()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ MigrationStateBuildItem build(BuildProducer<FeatureBuildItem> featureProducer,
Map<String, Collection<String>> applicationMigrationsToDs = new HashMap<>();
for (var i : dataSourceNames) {
Collection<String> migrationLocations = discoverApplicationMigrations(
flywayBuildConfig.getConfigForDataSourceName(i).locations);
flywayBuildConfig.getConfigForDataSourceName(i).locations());
applicationMigrationsToDs.put(i, migrationLocations);
}
Set<String> datasourcesWithMigrations = new HashSet<>();
Expand Down
5 changes: 5 additions & 0 deletions extensions/flyway/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
package io.quarkus.flyway.runtime;

import java.util.Collections;
import java.util.Map;

import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithParentName;

@ConfigRoot(name = "flyway", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public final class FlywayBuildTimeConfig {
@ConfigMapping(prefix = "quarkus.flyway")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public interface FlywayBuildTimeConfig {

/**
* Gets the {@link FlywayDataSourceBuildTimeConfig} for the given datasource name.
*/
public FlywayDataSourceBuildTimeConfig getConfigForDataSourceName(String dataSourceName) {
default FlywayDataSourceBuildTimeConfig getConfigForDataSourceName(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return defaultDataSource;
return defaultDataSource();
}
return namedDataSources.getOrDefault(dataSourceName, FlywayDataSourceBuildTimeConfig.defaultConfig());
FlywayDataSourceBuildTimeConfig config = namedDataSources().get(dataSourceName);
if (config == null) {
config = defaultDataSource();
}
return config;
}

/**
* Flyway configuration for the default datasource.
*/
@ConfigItem(name = ConfigItem.PARENT)
public FlywayDataSourceBuildTimeConfig defaultDataSource;
@WithParentName
FlywayDataSourceBuildTimeConfig defaultDataSource();

/**
* Flyway configurations for named datasources.
*/
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, FlywayDataSourceBuildTimeConfig> namedDataSources = Collections.emptyMap();
}
@WithParentName
Map<String, FlywayDataSourceBuildTimeConfig> namedDataSources();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public FlywayContainer createFlyway(DataSource dataSource, String dataSourceName
final Flyway flyway = new FlywayCreator(matchingRuntimeConfig, matchingBuildTimeConfig, matchingConfigCustomizers(
configCustomizerInstances, dataSourceName)).withCallbacks(callbacks)
.createFlyway(dataSource);
return new FlywayContainer(flyway, matchingRuntimeConfig.cleanAtStart, matchingRuntimeConfig.migrateAtStart,
matchingRuntimeConfig.repairAtStart, matchingRuntimeConfig.validateAtStart,
return new FlywayContainer(flyway, matchingRuntimeConfig.cleanAtStart(), matchingRuntimeConfig.migrateAtStart(),
matchingRuntimeConfig.repairAtStart(), matchingRuntimeConfig.validateAtStart(),
dataSourceName, hasMigrations,
createPossible);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,85 +49,85 @@ public FlywayCreator withCallbacks(Collection<Callback> callbacks) {
public Flyway createFlyway(DataSource dataSource) {
FluentConfiguration configure = Flyway.configure();

if (flywayRuntimeConfig.jdbcUrl.isPresent()) {
if (flywayRuntimeConfig.username.isPresent() && flywayRuntimeConfig.password.isPresent()) {
configure.dataSource(flywayRuntimeConfig.jdbcUrl.get(), flywayRuntimeConfig.username.get(),
flywayRuntimeConfig.password.get());
if (flywayRuntimeConfig.jdbcUrl().isPresent()) {
if (flywayRuntimeConfig.username().isPresent() && flywayRuntimeConfig.password().isPresent()) {
configure.dataSource(flywayRuntimeConfig.jdbcUrl().get(), flywayRuntimeConfig.username().get(),
flywayRuntimeConfig.password().get());
} else {
throw new ConfigurationException(
"Username and password must be defined when a JDBC URL is provided in the Flyway configuration");
}
} else {
if (flywayRuntimeConfig.username.isPresent() && flywayRuntimeConfig.password.isPresent()) {
if (flywayRuntimeConfig.username().isPresent() && flywayRuntimeConfig.password().isPresent()) {
AgroalDataSource agroalDataSource = (AgroalDataSource) dataSource;
String jdbcUrl = agroalDataSource.getConfiguration().connectionPoolConfiguration()
.connectionFactoryConfiguration().jdbcUrl();

configure.dataSource(jdbcUrl, flywayRuntimeConfig.username.get(),
flywayRuntimeConfig.password.get());
configure.dataSource(jdbcUrl, flywayRuntimeConfig.username().get(),
flywayRuntimeConfig.password().get());
} else {

configure.dataSource(dataSource);
}
}
if (flywayRuntimeConfig.initSql.isPresent()) {
configure.initSql(flywayRuntimeConfig.initSql.get());
if (flywayRuntimeConfig.initSql().isPresent()) {
configure.initSql(flywayRuntimeConfig.initSql().get());
}
if (flywayRuntimeConfig.connectRetries.isPresent()) {
configure.connectRetries(flywayRuntimeConfig.connectRetries.getAsInt());
if (flywayRuntimeConfig.connectRetries().isPresent()) {
configure.connectRetries(flywayRuntimeConfig.connectRetries().getAsInt());
}
if (flywayRuntimeConfig.defaultSchema.isPresent()) {
configure.defaultSchema(flywayRuntimeConfig.defaultSchema.get());
if (flywayRuntimeConfig.defaultSchema().isPresent()) {
configure.defaultSchema(flywayRuntimeConfig.defaultSchema().get());
}
if (flywayRuntimeConfig.schemas.isPresent()) {
configure.schemas(flywayRuntimeConfig.schemas.get().toArray(EMPTY_ARRAY));
if (flywayRuntimeConfig.schemas().isPresent()) {
configure.schemas(flywayRuntimeConfig.schemas().get().toArray(EMPTY_ARRAY));
}
if (flywayRuntimeConfig.table.isPresent()) {
configure.table(flywayRuntimeConfig.table.get());
if (flywayRuntimeConfig.table().isPresent()) {
configure.table(flywayRuntimeConfig.table().get());
}
configure.locations(flywayBuildTimeConfig.locations.toArray(EMPTY_ARRAY));
if (flywayRuntimeConfig.sqlMigrationPrefix.isPresent()) {
configure.sqlMigrationPrefix(flywayRuntimeConfig.sqlMigrationPrefix.get());
configure.locations(flywayBuildTimeConfig.locations().toArray(EMPTY_ARRAY));
if (flywayRuntimeConfig.sqlMigrationPrefix().isPresent()) {
configure.sqlMigrationPrefix(flywayRuntimeConfig.sqlMigrationPrefix().get());
}
if (flywayRuntimeConfig.repeatableSqlMigrationPrefix.isPresent()) {
configure.repeatableSqlMigrationPrefix(flywayRuntimeConfig.repeatableSqlMigrationPrefix.get());
if (flywayRuntimeConfig.repeatableSqlMigrationPrefix().isPresent()) {
configure.repeatableSqlMigrationPrefix(flywayRuntimeConfig.repeatableSqlMigrationPrefix().get());
}
configure.cleanDisabled(flywayRuntimeConfig.cleanDisabled);
configure.baselineOnMigrate(flywayRuntimeConfig.baselineOnMigrate);
configure.validateOnMigrate(flywayRuntimeConfig.validateOnMigrate);
configure.validateMigrationNaming(flywayRuntimeConfig.validateMigrationNaming);
configure.cleanDisabled(flywayRuntimeConfig.cleanDisabled());
configure.baselineOnMigrate(flywayRuntimeConfig.baselineOnMigrate());
configure.validateOnMigrate(flywayRuntimeConfig.validateOnMigrate());
configure.validateMigrationNaming(flywayRuntimeConfig.validateMigrationNaming());

final String[] ignoreMigrationPatterns;
if (flywayRuntimeConfig.ignoreMigrationPatterns.isPresent()) {
ignoreMigrationPatterns = flywayRuntimeConfig.ignoreMigrationPatterns.get();
if (flywayRuntimeConfig.ignoreMigrationPatterns().isPresent()) {
ignoreMigrationPatterns = flywayRuntimeConfig.ignoreMigrationPatterns().get();
} else {
List<String> patterns = new ArrayList<>(2);
if (flywayRuntimeConfig.ignoreMissingMigrations) {
if (flywayRuntimeConfig.ignoreMissingMigrations()) {
patterns.add("*:Missing");
}
if (flywayRuntimeConfig.ignoreFutureMigrations) {
if (flywayRuntimeConfig.ignoreFutureMigrations()) {
patterns.add("*:Future");
}
// Default is *:Future
ignoreMigrationPatterns = patterns.toArray(new String[0]);
}

configure.ignoreMigrationPatterns(ignoreMigrationPatterns);
configure.cleanOnValidationError(flywayRuntimeConfig.cleanOnValidationError);
configure.outOfOrder(flywayRuntimeConfig.outOfOrder);
if (flywayRuntimeConfig.baselineVersion.isPresent()) {
configure.baselineVersion(flywayRuntimeConfig.baselineVersion.get());
configure.cleanOnValidationError(flywayRuntimeConfig.cleanOnValidationError());
configure.outOfOrder(flywayRuntimeConfig.outOfOrder());
if (flywayRuntimeConfig.baselineVersion().isPresent()) {
configure.baselineVersion(flywayRuntimeConfig.baselineVersion().get());
}
if (flywayRuntimeConfig.baselineDescription.isPresent()) {
configure.baselineDescription(flywayRuntimeConfig.baselineDescription.get());
if (flywayRuntimeConfig.baselineDescription().isPresent()) {
configure.baselineDescription(flywayRuntimeConfig.baselineDescription().get());
}
configure.placeholders(flywayRuntimeConfig.placeholders);
configure.createSchemas(flywayRuntimeConfig.createSchemas);
if (flywayRuntimeConfig.placeholderPrefix.isPresent()) {
configure.placeholderPrefix(flywayRuntimeConfig.placeholderPrefix.get());
configure.placeholders(flywayRuntimeConfig.placeholders());
configure.createSchemas(flywayRuntimeConfig.createSchemas());
if (flywayRuntimeConfig.placeholderPrefix().isPresent()) {
configure.placeholderPrefix(flywayRuntimeConfig.placeholderPrefix().get());
}
if (flywayRuntimeConfig.placeholderSuffix.isPresent()) {
configure.placeholderSuffix(flywayRuntimeConfig.placeholderSuffix.get());
if (flywayRuntimeConfig.placeholderSuffix().isPresent()) {
configure.placeholderSuffix(flywayRuntimeConfig.placeholderSuffix().get());
}
if (!callbacks.isEmpty()) {
configure.callbacks(callbacks.toArray(new Callback[0]));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.quarkus.flyway.runtime;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConvertWith;
import io.quarkus.runtime.configuration.TrimmedStringConverter;
import io.smallrye.config.WithConverter;
import io.smallrye.config.WithDefault;

@ConfigGroup
public final class FlywayDataSourceBuildTimeConfig {
public interface FlywayDataSourceBuildTimeConfig {

private static final String DEFAULT_LOCATION = "db/migration";
String DEFAULT_LOCATION = "db/migration";

/**
* Comma-separated list of locations to scan recursively for migrations. The location type is determined by its prefix.
Expand All @@ -23,27 +22,15 @@ public final class FlywayDataSourceBuildTimeConfig {
* Locations starting with filesystem: point to a directory on the filesystem, may only contain SQL migrations and are only
* scanned recursively down non-hidden directories.
*/
@ConfigItem(defaultValue = DEFAULT_LOCATION)
@ConvertWith(TrimmedStringConverter.class)
public List<String> locations;
@WithDefault(DEFAULT_LOCATION)
@WithConverter(TrimmedStringConverter.class)
List<String> locations();

/**
* Comma-separated list of fully qualified class names of Callback implementations
* to use to hook into the Flyway lifecycle.
* The {@link org.flywaydb.core.api.callback.Callback} subclass must have a no-args constructor and must not be abstract.
* These classes must also not have any fields that hold state (unless that state is initialized in the constructor).
*/
@ConfigItem
public Optional<List<String>> callbacks = Optional.empty();

/**
* Creates a {@link FlywayDataSourceBuildTimeConfig} with default settings.
*
* @return {@link FlywayDataSourceBuildTimeConfig}
*/
public static FlywayDataSourceBuildTimeConfig defaultConfig() {
FlywayDataSourceBuildTimeConfig defaultConfig = new FlywayDataSourceBuildTimeConfig();
defaultConfig.locations = Collections.singletonList(DEFAULT_LOCATION);
return defaultConfig;
}
Optional<List<String>> callbacks();
}
Loading

0 comments on commit 6cf647c

Please sign in to comment.