
org.flywaydb.core.CustomFlyway Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flyway-plus Show documentation
Show all versions of flyway-plus Show documentation
Flyway extended implementation
The newest version!
/*
* Copyright 2018 the original author or 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
*
* http://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 org.flywaydb.core;
import java.lang.reflect.Field;
import java.util.Collection;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.internal.callback.CallbackExecutor;
import org.flywaydb.core.internal.database.Database;
import org.flywaydb.core.internal.database.Schema;
import org.flywaydb.core.internal.resolver.CompositeMigrationResolver;
import org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver;
import org.flywaydb.core.internal.schemahistory.SchemaHistory;
import org.flywaydb.core.internal.util.placeholder.PlaceholderReplacer;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
/**
* Custom {@link Flyway}
*/
public class CustomFlyway extends Flyway {
/**
* {@link PlaceholderReplacer}
*/
@Getter
@Setter
private PlaceholderReplacer placeholderReplacer;
@Override
protected T execute(Command command) {
return super.execute(new InternalCommand<>(command, this.placeholderReplacer));
}
/**
* Internal {@link org.flywaydb.core.Flyway.Command}
*
* @param result type
*/
@RequiredArgsConstructor
protected static class InternalCommand implements Command {
/**
* Delegate
*/
@NonNull
private final Command delegate;
/**
* {@link PlaceholderReplacer}
*/
private final PlaceholderReplacer placeholderReplacer;
@Override
public T execute(
/* @formatter:off */
MigrationResolver migrationResolver,
SchemaHistory schemaHistory,
@SuppressWarnings("rawtypes") Database database,
@SuppressWarnings("rawtypes") Schema[] schemas,
CallbackExecutor callbackExecutor) {
/* @formatter:on */
if (this.placeholderReplacer != null) {
this.resetPlaceholderReplacer(migrationResolver, this.placeholderReplacer);
}
return this.delegate.execute(migrationResolver, schemaHistory, database, schemas, callbackExecutor);
}
/**
* Reset {@link PlaceholderReplacer}
*
* @param migrationResolver {@link MigrationResolver}
* @param placeholderReplacer {@link PlaceholderReplacer}
*/
protected void resetPlaceholderReplacer(MigrationResolver migrationResolver,
PlaceholderReplacer placeholderReplacer) {
// Extract child resolvers
if (migrationResolver instanceof CompositeMigrationResolver) {
try {
Field field = CompositeMigrationResolver.class.getDeclaredField("migrationResolvers");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Collection childResolvers = (Collection) field
.get(migrationResolver);
// Recursive call
for (MigrationResolver childResolver : childResolvers) {
this.resetPlaceholderReplacer(childResolver, placeholderReplacer);
}
}
catch (ReflectiveOperationException e) {
throw new IllegalStateException("Failed to get 'MigrationResolver'", e);
}
}
// Replace
else if (migrationResolver instanceof SqlMigrationResolver) {
try {
Field field = SqlMigrationResolver.class.getDeclaredField("placeholderReplacer");
field.setAccessible(true);
field.set(migrationResolver, placeholderReplacer);
}
catch (ReflectiveOperationException e) {
throw new IllegalStateException("Failed to set 'PlaceholderReplacer'", e);
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy