Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the Flyway configuration experience #533

Merged
merged 8 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void forceRun(FlywayConfigurationProperties config, DataSource dataSource) {
fluentConfiguration.cleanDisabled(!config.isCleanSchema());
fluentConfiguration.dataSource(dataSource);
fluentConfiguration.configuration(config.getProperties());
customizeConfiguration(config.getNameQualifier(), fluentConfiguration);

Flyway flyway = fluentConfiguration.load();
applicationContext.registerSingleton(Flyway.class, flyway, Qualifiers.byName(config.getNameQualifier()), false);
Expand All @@ -93,6 +94,12 @@ void forceRun(FlywayConfigurationProperties config, DataSource dataSource) {
}
}

private void customizeConfiguration(String nameQualifier, FluentConfiguration fluentConfiguration) {
applicationContext.findBean(FlywayConfigurationCustomizer.class, Qualifiers.byName(nameQualifier))
.orElse(new DefaultFlywayConfigurationCustomizer(applicationContext, nameQualifier))
.customizeFluentConfiguration(fluentConfiguration);
}

private void runFlyway(FlywayConfigurationProperties config, Flyway flyway) {
if (config.isCleanSchema()) {
LOG.info("Cleaning schema for database with qualifier [{}]", config.getNameQualifier());
Expand All @@ -114,4 +121,5 @@ private void runFlyway(FlywayConfigurationProperties config, Flyway flyway) {
void runAsync(FlywayConfigurationProperties config, Flyway flyway) {
runFlyway(config, flyway);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.flyway;

import io.micronaut.context.ApplicationContext;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.type.Argument;
import io.micronaut.inject.qualifiers.Qualifiers;
import org.flywaydb.core.api.ClassProvider;
import org.flywaydb.core.api.ResourceProvider;
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;

/**
* Default implementation of {@link FlywayConfigurationCustomizer}. Finds and configures all
* {@link jakarta.inject.Named} instances of the following Flyway types:
*
* <ul>
* <li>{@link JavaMigration}[]</li>
* <li>{@link Callback}[]</li>
* <li>{@link MigrationResolver}[]</li>
* <li>{@link ResourceProvider}</li>
* <li>{@link ClassProvider}</li>
* </ul>
*
* @author Jeremy Grelle
* @since 7.2.0
*/
public class DefaultFlywayConfigurationCustomizer implements FlywayConfigurationCustomizer {

private final ApplicationContext applicationContext;
private final String name;

DefaultFlywayConfigurationCustomizer(ApplicationContext applicationContext, String name) {
this.applicationContext = applicationContext;
this.name = name;
}

@Override
public void customizeFluentConfiguration(FluentConfiguration fluentConfiguration) {
applicationContext.findBean(JavaMigration[].class, Qualifiers.byName(name))
.ifPresent(fluentConfiguration::javaMigrations);

applicationContext.findBean(Callback[].class, Qualifiers.byName(name))
.ifPresent(fluentConfiguration::callbacks);

applicationContext.findBean(MigrationResolver[].class, Qualifiers.byName(name))
.ifPresent(fluentConfiguration::resolvers);

applicationContext.findBean(ResourceProvider.class, Qualifiers.byName(name))
.ifPresent(fluentConfiguration::resourceProvider);

applicationContext.findBean(Argument.of(ClassProvider.class, Argument.of(JavaMigration.class)), Qualifiers.byName(name))
.ifPresent(fluentConfiguration::javaMigrationClassProvider);
}

@Override
public @NonNull String getName() {
return this.name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.flyway;

import io.micronaut.core.naming.Named;
import org.flywaydb.core.api.configuration.FluentConfiguration;

/**
* Interface for customizing Flyway configuration. Allows for injection of custom implementations
* of Flyway-specific types, and for setting general configuration properties that might not yet
* be explicitly supported in {@link FlywayConfigurationProperties}.
*
* @author Jeremy Grelle
* @since 7.2.0
*/
public interface FlywayConfigurationCustomizer extends Named {

/**
* A callback for customizing Flyway configuration by setting properties on the
* {@link FluentConfiguration} builder prior to execution of migrations.
*
* @param fluentConfiguration The configuration to be customized
*/
void customizeFluentConfiguration(FluentConfiguration fluentConfiguration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ public class FlywayConfigurationProperties implements Toggleable {
@SuppressWarnings("WeakerAccess")
public static final boolean DEFAULT_CLEAN_SCHEMA = false;

@ConfigurationBuilder(prefixes = "", excludes = {"jdbcProperties", "configuration"})
// NOTE - Some of the ignored properties (javaMigrations, callbacks, resolvers, resourceProvider, javaMigrationClassProvider)
// are custom Flyway types and implementations are meant to be provided by the user through an implementation of
// FlywayConfigurationCustomizer if needed.
// Most of the other ignored properties have overloaded methods in FluentConfiguration, making it non-deterministic as to which
// of the methods would be selected for setting the builder property, thus explicit property setters are provided here instead
// that pass the value through to the builder.
@ConfigurationBuilder(prefixes = "", excludes = {"jdbcProperties", "configuration", "dryRunOutput",
"ignoreMigrationPatterns", "locations", "encoding", "target", "javaMigrations", "dataSource",
"baselineVersion", "callbacks", "resolvers", "resourceProvider", "javaMigrationClassProvider"})
FluentConfiguration fluentConfiguration = new FluentConfiguration();

private final String nameQualifier;
Expand Down Expand Up @@ -213,4 +221,70 @@ public void setProperties(@MapFormat(transformation = MapFormat.MapTransformatio
public Map<String, String> getProperties() {
return properties;
}
}

//Pass-through properties for overloaded FluentConfiguration methods

/**
* Sets the dry run output filename.
*
* @see FluentConfiguration#dryRunOutput(String)
*
* @param dryRunOutputFileName The dry run output filename
*/
public void setDryRunOutput(String dryRunOutputFileName) {
fluentConfiguration.dryRunOutput(dryRunOutputFileName);
}

/**
* Sets the migration patterns to ignore.
*
* @see FluentConfiguration#ignoreMigrationPatterns(String...)
*
* @param ignoreMigrationPatterns The migration patterns to ignore
*/
public void setIgnoreMigrationPatterns(String... ignoreMigrationPatterns) {
fluentConfiguration.ignoreMigrationPatterns(ignoreMigrationPatterns);
}

/**
* Sets the locations to scan recursively for migrations.
*
* @see FluentConfiguration#locations(String...)
*
* @param locations The locations to scan for migrations
*/
public void setLocations(String... locations) {
fluentConfiguration.locations(locations);
}

/**
* Sets the encoding of SQL migrations.
*
* @see FluentConfiguration#encoding(String)
*
* @param encoding The encoding of SQL migrations
*/
public void setEncoding(String encoding) {
fluentConfiguration.encoding(encoding);
}

/**
* Sets the target version up to which Flyway should consider migrations.
*
* @see FluentConfiguration#target(String)
*
* @param target The target version
*/
public void setTarget(String target) {
fluentConfiguration.target(target);
}

/**
* The version to tag an existing schema with when executing baseline. Passes through to {@link FluentConfiguration#baselineVersion(String)}
* @param baselineVersion The version to tag an existing schema with when executing baseline.
*/
public void setBaselineVersion(String baselineVersion) {
fluentConfiguration.baselineVersion(baselineVersion);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.TypeConverter;
import jakarta.inject.Singleton;
import org.flywaydb.core.api.pattern.ValidatePattern;

import java.util.Optional;
Expand All @@ -28,8 +27,11 @@
* @author Nenad Vico
* @see org.flywaydb.core.api.pattern.ValidatePattern
* @since 5.4.1
* @deprecated This converter is no longer necessary in conjunction with enhancements to
* {@link FlywayConfigurationProperties} that ensure that {@link org.flywaydb.core.api.configuration.FluentConfiguration#ignoreMigrationPatterns(String...)}
* will always be used to set ignore-migration-patterns, letting Flyway perform its own type conversion.
*/
@Singleton
jeremyg484 marked this conversation as resolved.
Show resolved Hide resolved
@Deprecated(since = "7.2.0")
public class ValidatePatternTypeConverter implements TypeConverter<String, ValidatePattern> {

@Override
Expand Down
Loading
Loading