Skip to content

Commit

Permalink
Revert "Cleanup Flyway Config"
Browse files Browse the repository at this point in the history
This reverts commit 6cf647c.
  • Loading branch information
gastaldi committed May 2, 2023
1 parent 9ca01af commit ec29707
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 206 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: 0 additions & 5 deletions extensions/flyway/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
<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,40 +1,35 @@
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;

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

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

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

/**
* Flyway configurations for named datasources.
*/
@WithParentName
Map<String, FlywayDataSourceBuildTimeConfig> namedDataSources();
}
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, FlywayDataSourceBuildTimeConfig> namedDataSources = Collections.emptyMap();
}
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,17 +1,18 @@
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 interface FlywayDataSourceBuildTimeConfig {
public final class FlywayDataSourceBuildTimeConfig {

String DEFAULT_LOCATION = "db/migration";
private static final 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 @@ -22,15 +23,27 @@ public interface 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.
*/
@WithDefault(DEFAULT_LOCATION)
@WithConverter(TrimmedStringConverter.class)
List<String> locations();
@ConfigItem(defaultValue = DEFAULT_LOCATION)
@ConvertWith(TrimmedStringConverter.class)
public 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).
*/
Optional<List<String>> callbacks();
@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;
}
}
Loading

0 comments on commit ec29707

Please sign in to comment.