diff --git a/flyway/src/main/java/io/micronaut/flyway/AbstractFlywayMigration.java b/flyway/src/main/java/io/micronaut/flyway/AbstractFlywayMigration.java index 21325b80..b454c95f 100644 --- a/flyway/src/main/java/io/micronaut/flyway/AbstractFlywayMigration.java +++ b/flyway/src/main/java/io/micronaut/flyway/AbstractFlywayMigration.java @@ -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); @@ -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()); @@ -114,4 +121,5 @@ private void runFlyway(FlywayConfigurationProperties config, Flyway flyway) { void runAsync(FlywayConfigurationProperties config, Flyway flyway) { runFlyway(config, flyway); } + } diff --git a/flyway/src/main/java/io/micronaut/flyway/DefaultFlywayConfigurationCustomizer.java b/flyway/src/main/java/io/micronaut/flyway/DefaultFlywayConfigurationCustomizer.java new file mode 100644 index 00000000..6c7b8fce --- /dev/null +++ b/flyway/src/main/java/io/micronaut/flyway/DefaultFlywayConfigurationCustomizer.java @@ -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: + * + * + * + * @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; + } +} diff --git a/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationCustomizer.java b/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationCustomizer.java new file mode 100644 index 00000000..70007316 --- /dev/null +++ b/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationCustomizer.java @@ -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); +} diff --git a/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationProperties.java b/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationProperties.java index 9a9c7529..c642bd48 100644 --- a/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationProperties.java +++ b/flyway/src/main/java/io/micronaut/flyway/FlywayConfigurationProperties.java @@ -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; @@ -213,4 +221,70 @@ public void setProperties(@MapFormat(transformation = MapFormat.MapTransformatio public Map 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); + } + + } diff --git a/flyway/src/main/java/io/micronaut/flyway/ValidatePatternTypeConverter.java b/flyway/src/main/java/io/micronaut/flyway/ValidatePatternTypeConverter.java index 22fb05ba..9edbbc78 100644 --- a/flyway/src/main/java/io/micronaut/flyway/ValidatePatternTypeConverter.java +++ b/flyway/src/main/java/io/micronaut/flyway/ValidatePatternTypeConverter.java @@ -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; @@ -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 +@Deprecated(since = "7.2.0") public class ValidatePatternTypeConverter implements TypeConverter { @Override diff --git a/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationCustomizerSpec.groovy b/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationCustomizerSpec.groovy new file mode 100644 index 00000000..a3caff71 --- /dev/null +++ b/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationCustomizerSpec.groovy @@ -0,0 +1,154 @@ +package io.micronaut.flyway + +import io.micronaut.context.annotation.Factory +import io.micronaut.context.annotation.Requires +import io.micronaut.inject.qualifiers.Qualifiers +import jakarta.inject.Named +import jakarta.inject.Singleton +import org.flywaydb.core.Flyway +import org.flywaydb.core.api.callback.Callback +import org.flywaydb.core.api.callback.Event +import org.flywaydb.core.api.configuration.FluentConfiguration +import org.flywaydb.core.api.migration.BaseJavaMigration +import org.flywaydb.core.api.migration.Context +import org.flywaydb.core.api.migration.JavaMigration + +import javax.sql.DataSource + +class FlywayConfigurationCustomizerSpec extends AbstractFlywaySpec { + + void 'flyway configuration can be customized with an implementation of FlywayConfigurationCustomizer'() { + given: + run('spec.name' : FlywayConfigurationCustomizerSpec.simpleName, + 'flyway.datasources.movies.enabled' : true, + 'datasources.movies.url' : 'jdbc:h2:mem:flyway2Db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE', + 'datasources.movies.username' : DS_USERNAME, + 'datasources.movies.password' : DS_PASSWORD, + 'datasources.movies.driverClassName': DS_DRIVER, + + 'flyway.datasources.books.enabled' : true, + 'datasources.books.url' : 'jdbc:h2:mem:flywayDb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE', + 'datasources.books.username' : DS_USERNAME, + 'datasources.books.password' : DS_PASSWORD, + 'datasources.books.driverClassName' : DS_DRIVER) + + when: + applicationContext.getBean(DataSource, Qualifiers.byName('movies')) + applicationContext.getBean(FlywayConfigurationProperties, Qualifiers.byName('movies')) + Flyway moviesFlyway = applicationContext.getBean(Flyway, Qualifiers.byName('movies')) + + then: + noExceptionThrown() + moviesFlyway + + when: + applicationContext.getBean(DataSource, Qualifiers.byName('books')) + applicationContext.getBean(FlywayConfigurationProperties, Qualifiers.byName('books')) + Flyway booksFlyway = applicationContext.getBean(Flyway, Qualifiers.byName('books')) + + then: + noExceptionThrown() + moviesFlyway.configuration.javaMigrations.size() == 1 + moviesFlyway.configuration.callbacks.size() == 1 + booksFlyway.configuration.javaMigrations.size() == 1 + booksFlyway.configuration.callbacks.size() == 0 + } +} + +@Requires(property = "spec.name", value = "FlywayConfigurationCustomizerSpec") +//tag::typesfactorystart[] +@Factory +public class CustomFlywayTypesFactory { +//end::typesfactorystart[] + + //tag::migrations[] + @Named("books") //<1> + @Singleton + public JavaMigration[] booksMigrations() { + return [ new V3__Migrate_books() ]; //<2> + } + //end::migrations[] + + @Named("movies") + @Singleton + JavaMigration[] moviesMigrations() { + return [ new V3__Migrate_movies() ] + } + + @Named("movies") + @Singleton + Callback[] moviesCallbacks() { + return [ new Callback() { + + @Override + boolean supports(Event event, org.flywaydb.core.api.callback.Context context) { + return false + } + + @Override + boolean canHandleInTransaction(Event event, org.flywaydb.core.api.callback.Context context) { + return false + } + + @Override + void handle(Event event, org.flywaydb.core.api.callback.Context context) { + + } + + @Override + String getCallbackName() { + return null + } + }] + } + + //tag::javamigration[] + + static class V3__Migrate_books extends BaseJavaMigration //<3> + { + + @Override + void migrate(Context context) throws Exception { + //Execute migration + } + } + //end::javamigration[] + + static class V3__Migrate_movies extends BaseJavaMigration + { + + @Override + void migrate(Context context) throws Exception { + + } + } + +//tag::typesfactoryend[] +} +//end::typesfactoryend[] + +@Requires(property = "spec.name", value = "FlywayConfigurationCustomizerSpec") +//tag::customizer[] +@Singleton +public class BooksFlywayConfigurationCustomizer implements FlywayConfigurationCustomizer { //<1> + + private final static String NAME = "books"; + private final JavaMigration[] javaMigrations; + + public BooksFlywayConfigurationCustomizer(@Named(NAME) JavaMigration[] javaMigrations) { //<2> + this.javaMigrations = javaMigrations; + } + + @Override + public void customizeFluentConfiguration(FluentConfiguration fluentConfiguration) { + if(javaMigrations != null) { + fluentConfiguration.javaMigrations(javaMigrations); //<3> + } + } + + @Override + public String getName() { + return NAME; + } +} +//end::customizer[] diff --git a/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationOverloadedPropertiesSpec.groovy b/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationOverloadedPropertiesSpec.groovy new file mode 100644 index 00000000..a2dfaf0e --- /dev/null +++ b/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationOverloadedPropertiesSpec.groovy @@ -0,0 +1,62 @@ +package io.micronaut.flyway + +import io.micronaut.context.exceptions.BeanInstantiationException +import io.micronaut.inject.qualifiers.Qualifiers +import org.flywaydb.core.Flyway +import org.flywaydb.core.api.configuration.Configuration +import org.flywaydb.core.api.pattern.ValidatePattern + +import javax.sql.DataSource + +class FlywayConfigurationOverloadedPropertiesSpec extends AbstractFlywaySpec { + + void 'overloaded properties in FluentConfiguration can be set'() { + given: + run('spec.name': FlywayConfigurationPropertiesEnabledSpec.simpleName, + 'flyway.datasources.movies.enabled': true, + 'datasources.movies.url': 'jdbc:h2:mem:flyway2Db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE', + 'datasources.movies.username': DS_USERNAME, + 'datasources.movies.password': DS_PASSWORD, + 'datasources.movies.driverClassName': DS_DRIVER, + 'flyway.datasources.movies.ignore-migration-patterns': '*:*', + 'flyway.datasources.movies.locations': 'classpath:db/migration,classpath:othermigrations', + 'flyway.datasources.movies.encoding': 'utf-8', + 'flyway.datasources.movies.target': '1', + 'flyway.datasources.movies.baseline-version': '2' + ) + + when: + applicationContext.getBean(DataSource, Qualifiers.byName('movies')) + applicationContext.getBean(FlywayConfigurationProperties, Qualifiers.byName('movies')) + Configuration configuration = applicationContext.getBean(Flyway, Qualifiers.byName('movies')).getConfiguration() + + then: + noExceptionThrown() + configuration + configuration.ignoreMigrationPatterns == [ ValidatePattern.fromPattern('*:*') ] + configuration.locations.toString() == '[classpath:db/migration, classpath:othermigrations]' + configuration.encoding.toString() == 'UTF-8' + configuration.target.toString() == '1' + configuration.baselineVersion.toString() == '2' + } + + void 'overloaded team edition properties throw an exception'() { + when: + run([ 'spec.name': FlywayConfigurationPropertiesEnabledSpec.simpleName, + 'flyway.datasources.movies.enabled': true, + 'datasources.movies.url': 'jdbc:h2:mem:flyway2Db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE', + 'datasources.movies.username': DS_USERNAME, + 'datasources.movies.password': DS_PASSWORD, + 'datasources.movies.driverClassName': DS_DRIVER, + (flywayProperty): value + ]) + + then: + BeanInstantiationException ex = thrown() + ex.message.contains("is not supported by OSS") + + where: + flywayProperty | value + 'flyway.datasources.movies.dryRunOutput' | 'foo' + } +} diff --git a/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationPropertiesEnabledSpec.groovy b/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationPropertiesEnabledSpec.groovy index f0b4e81e..d96b9ddb 100644 --- a/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationPropertiesEnabledSpec.groovy +++ b/flyway/src/test/groovy/io/micronaut/flyway/FlywayConfigurationPropertiesEnabledSpec.groovy @@ -1,11 +1,8 @@ package io.micronaut.flyway - import io.micronaut.context.exceptions.NoSuchBeanException import io.micronaut.inject.qualifiers.Qualifiers import org.flywaydb.core.Flyway -import org.flywaydb.core.api.MigrationState -import org.flywaydb.core.api.pattern.ValidatePattern import javax.sql.DataSource @@ -62,32 +59,6 @@ class FlywayConfigurationPropertiesEnabledSpec extends AbstractFlywaySpec { noExceptionThrown() } - void 'if flyway configuration then ValidatePatternTypeConverter and Flyway beans are created'() { - given: - run('spec.name' : FlywayConfigurationPropertiesEnabledSpec.simpleName, - 'flyway.datasources.movies.enabled' : true, - 'flyway.datasources.movies.ignore-migration-patterns' : "*:missing", - 'datasources.movies.url' : 'jdbc:h2:mem:flyway2Db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE', - 'datasources.movies.username' : DS_USERNAME, - 'datasources.movies.password' : DS_PASSWORD, - 'datasources.movies.driverClassName': DS_DRIVER) - - when: - applicationContext.getBean(DataSource, Qualifiers.byName('movies')) - def configuration = applicationContext.getBean(FlywayConfigurationProperties, Qualifiers.byName('movies')) - applicationContext.getBean(Flyway, Qualifiers.byName('movies')) - - then: - noExceptionThrown() - - and: - configuration.fluentConfiguration.ignoreMigrationPatterns.length == 1 - configuration.fluentConfiguration.ignoreMigrationPatterns[0].matchesMigration(false, MigrationState.MISSING_SUCCESS) - - expect: - applicationContext.getConversionService().canConvert(String.class, ValidatePattern.class) - } - void 'if flyway configuration then camel case configuration properties in the properties map can be set successfully'() { given: run('spec.name' : FlywayConfigurationPropertiesEnabledSpec.simpleName, diff --git a/flyway/src/test/groovy/io/micronaut/flyway/ValidatePatternTypeConverterSpec.groovy b/flyway/src/test/groovy/io/micronaut/flyway/ValidatePatternTypeConverterSpec.groovy deleted file mode 100644 index 32a6a908..00000000 --- a/flyway/src/test/groovy/io/micronaut/flyway/ValidatePatternTypeConverterSpec.groovy +++ /dev/null @@ -1,83 +0,0 @@ -package io.micronaut.flyway - -import org.flywaydb.core.api.ErrorCode -import org.flywaydb.core.api.FlywayException -import org.flywaydb.core.api.MigrationState -import org.flywaydb.core.api.pattern.ValidatePattern -import org.flywaydb.core.internal.license.FlywayRedgateEditionRequiredException -import spock.lang.Specification - -/** - * @author Nenad Vico - */ -class ValidatePatternTypeConverterSpec extends Specification { - - - void "test empty string conversion"() { - given: - ValidatePatternTypeConverter validatePatternTypeConverter = new ValidatePatternTypeConverter() - - expect: - !validatePatternTypeConverter.convert("", ValidatePattern.class).isPresent() - } - - void "test wrong string conversion"() { - given: - ValidatePatternTypeConverter validatePatternTypeConverter = new ValidatePatternTypeConverter() - - when: - validatePatternTypeConverter.convert("junk", ValidatePattern.class) - - then: - def e = thrown(FlywayException) - e.errorCode == ErrorCode.ERROR - e.message == 'Invalid pattern \'junk\'. Pattern must be of the form : See https://rd.gt/37m4hXD for full details' - } - - void "test future string conversion"() { - given: - ValidatePatternTypeConverter validatePatternTypeConverter = new ValidatePatternTypeConverter() - - expect: - validatePatternTypeConverter.convert("*:future", ValidatePattern.class).get() - .matchesMigration(false, MigrationState.FUTURE_SUCCESS) - } - - void "test repeatable string conversion"() { - given: - ValidatePatternTypeConverter validatePatternTypeConverter = new ValidatePatternTypeConverter() - - when: - validatePatternTypeConverter.convert("repeatable:*", ValidatePattern.class) - - then: - def e = thrown(FlywayRedgateEditionRequiredException) - e.errorCode == ErrorCode.ERROR - e.message == 'Flyway Redgate Edition Required: ignoreMigrationPattern with type \'repeatable\' is not supported by OSS Edition\n' + - 'Download Redgate Edition for free: https://rd.gt/3GGIXhh' - } - - void "test versioned string conversion"() { - given: - ValidatePatternTypeConverter validatePatternTypeConverter = new ValidatePatternTypeConverter() - - when: - validatePatternTypeConverter.convert("versioned:missing", ValidatePattern.class) - - then: - def e = thrown(FlywayRedgateEditionRequiredException) - e.errorCode == ErrorCode.ERROR - e.message == 'Flyway Redgate Edition Required: ignoreMigrationPattern with type \'versioned\' is not supported by OSS Edition\n' + - 'Download Redgate Edition for free: https://rd.gt/3GGIXhh' - } - - void "test missing string conversion"() { - given: - ValidatePatternTypeConverter validatePatternTypeConverter = new ValidatePatternTypeConverter() - - expect: - validatePatternTypeConverter.convert("*:missing", ValidatePattern.class).get() - .matchesMigration(false, MigrationState.MISSING_SUCCESS) - } - -} diff --git a/src/main/docs/guide/configuration/additionalConfig.adoc b/src/main/docs/guide/configuration/additionalConfig.adoc index e8b9dd9f..2b74d04c 100644 --- a/src/main/docs/guide/configuration/additionalConfig.adoc +++ b/src/main/docs/guide/configuration/additionalConfig.adoc @@ -17,3 +17,53 @@ include::{flywaytests}/groovy/io/micronaut/flyway/postgresql/TransactionLockSpec ---- Note that setting configuration this way will override any matching properties set via the known configuration keys in the tables above. + +=== Configuring Custom Flyway Type Implementations + +It is possible to inject implementations of certain Flyway type interfaces as beans. The following types are currently supported: + +|=== +|Type |Description + +|link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/migration/JavaMigration.html[JavaMigration]`[]` +|Java-based migrations. + +|link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/callback/Callback.html[Callback]`[]` +|Callbacks for lifecycle notifications. + +|link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/resolver/MigrationResolver.html[MigrationResolver]`[]` +|`MigrationResolvers` to be used in addition to the built-in ones for resolving Migrations to apply. + +|link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/ResourceProvider.html[ResourceProvider] +|`ResourceProvider` to be used to look up resources. + +|link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/ClassProvider.html[ClassProvider] +|ClassProvider to be used to look up `JavaMigration` classes. + +|=== + + + +The provided implementation must have a `@Named` qualifier that matches the name of the Flyway configuration name. For example, an array of link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/migration/JavaMigration.html[JavaMigrations] can be provided as in the following example: + +[source, java] +---- +include::{flywaytests}/groovy/io/micronaut/flyway/FlywayConfigurationCustomizerSpec.groovy[tags="typesfactorystart,migrations,javamigration,typesfactoryend"] +---- +<1> The factory method has a `@Named` qualifier +<2> A new `JavaMigration` instance is created +<3> The returned class follows Flyway's naming conventions for `JavaMigrations` + +=== Customizing Flyway Configuration + +Further customization of Flyway configuration can be done by providing a `@Named` implementation of api:flyway.FlywayConfigurationCustomizer[]. This allows you to directly set properties on Flyway's link:https://javadoc.io/doc/org.flywaydb/flyway-core/latest/org/flywaydb/core/api/configuration/FluentConfiguration.html[FluentConfiguration] builder before the migrations are executed. For example, you might want to set a property that is not yet supported in api:flyway.FlywayConfigurationProperties[], or you might want to override a configuration value based on some runtime calculation. + +[source, java] +---- +include::{flywaytests}/groovy/io/micronaut/flyway/FlywayConfigurationCustomizerSpec.groovy[tags="customizer"] +---- +<1> The bean implements `FlywayConfigurationCustomizer` +<2> A `JavaMigration` array is injected via a `@Named` qualifier +<3> The migrations are set on the builder + +NOTE: Providing your own implementation of api:flyway.FlywayConfigurationCustomizer[] will override the default implementation which is used for the configuration of custom Flyway type implementations above. If you need to provide such types along with customizing the configuration, it must be done by your `FlywayConfigurationCustomizer` implementation. Your implementation may extend api:flyway.DefaultFlywayConfigurationCustomizer[] in order to retain the built-in behavior and augment it with further configuration adjustments of your own.