Skip to content

Commit

Permalink
flyway#2698. Configure custom comparator defining repeatable migratio…
Browse files Browse the repository at this point in the history
…ns order.
  • Loading branch information
antonbakalets committed Apr 28, 2020
1 parent 58eeb4c commit b46c1dc
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 97 deletions.
4 changes: 4 additions & 0 deletions flyway-commandline/src/main/assembly/flyway.conf
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
# defined by 'flyway.resolvers' are used. (default: false)
# flyway.skipDefaultResolvers=

# Fully qualified class name of custom Comparator to determine the order of repeatable migrations
# By default repeatable migrations are ordered by description.
# flyway.repeatableMigrationComparator=

# Comma-separated list of directories containing JDBC drivers and Java-based migrations.
# (default: <INSTALL-DIR>/jars)
# flyway.jarDirs=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ private static void printUsage() {
LOG.info("locations : Classpath locations to scan recursively for migrations");
LOG.info("resolvers : Comma-separated list of custom MigrationResolvers");
LOG.info("skipDefaultResolvers : Skips default resolvers (jdbc, sql and Spring-jdbc)");
LOG.info("repeatableMigrationComparator: Fully qualified class name of repeatable migrations comparator");
LOG.info("sqlMigrationPrefix : File name prefix for versioned SQL migrations");
LOG.info("undoSqlMigrationPrefix : [" + "pro] File name prefix for undo SQL migrations");
LOG.info("repeatableSqlMigrationPrefix : File name prefix for repeatable SQL migrations");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
import org.flywaydb.core.api.logging.LogFactory;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.api.resolver.ResolvedMigration;
import org.flywaydb.core.internal.configuration.ConfigUtils;
import org.flywaydb.core.internal.jdbc.DriverDataSource;
import org.flywaydb.core.internal.license.Edition;
import org.flywaydb.core.internal.resolver.ResolvedMigrationComparator;
import org.flywaydb.core.internal.util.ClassUtils;
import org.flywaydb.core.internal.util.Locations;
import org.flywaydb.core.internal.util.StringUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -389,80 +389,12 @@ public class ClassicConfiguration implements Configuration {
*/
private String installedBy;











































































/**
* The {@link Comparator} to determine the order of repeatable migrations.
* <p>
* By default repeatable migrations are applied in the order of their description.
*/
private Comparator<ResolvedMigration> repeatableMigrationComparator = ResolvedMigrationComparator.DEFAULT_REPEATABLE_MIGRATION_COMPARATOR;

/**
* Creates a new default configuration.
Expand Down Expand Up @@ -722,6 +654,11 @@ public boolean outputQueryResults() {



}

@Override
public Comparator<ResolvedMigration> getRepeatableMigrationComparator() {
return repeatableMigrationComparator;
}

/**
Expand Down Expand Up @@ -872,6 +809,25 @@ public void setInstalledBy(String installedBy) {
this.installedBy = installedBy;
}

/**
* Sets the {@link Comparator} to determine the order of repeatable migrations.
*
* @param repeatableMigrationComparator The comparator to determine the order of repeatable migrations.
*/
public void setRepeatableMigrationComparator(Comparator<ResolvedMigration> repeatableMigrationComparator) {
this.repeatableMigrationComparator = repeatableMigrationComparator;
}

/**
* Sets custom {@link Comparator} to determine the order of repeatable migrations.
*
* @param comparator The fully qualified class name of the custom {@link Comparator} to determine the order of repeatable migrations.
*/
public void setRepeatableMigrationComparatorAsClassName(String comparator) {
Comparator<ResolvedMigration> repeatableMigrationComparator = ClassUtils.instantiate(comparator, classLoader);
setRepeatableMigrationComparator(repeatableMigrationComparator);
}

/**
* Whether to allow mixing transactional and non-transactional statements within the same migration. Enabling this
* automatically causes the entire affected migration to be run without a transaction.
Expand Down Expand Up @@ -1643,6 +1599,7 @@ public void configure(Configuration configuration) {
setTablespace(configuration.getTablespace());
setTarget(configuration.getTarget());
setValidateOnMigrate(configuration.isValidateOnMigrate());
setRepeatableMigrationComparator(configuration.getRepeatableMigrationComparator());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.api.resolver.ResolvedMigration;

import javax.sql.DataSource;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Comparator;
import java.util.Map;

/**
Expand Down Expand Up @@ -515,4 +517,11 @@ public interface Configuration {
* @return {@code true} to output the results table (default: {@code true})
*/
boolean outputQueryResults();

/**
* The {@link Comparator} to determine the order of repeatable migrations.
* <p>
* By default repeatable migrations are applied in the order of their description.
*/
Comparator<ResolvedMigration> getRepeatableMigrationComparator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.api.resolver.ResolvedMigration;
import org.flywaydb.core.internal.configuration.ConfigUtils;
import org.flywaydb.core.internal.util.ClassUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Comparator;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -296,6 +297,21 @@ public boolean outputQueryResults() {
return config.outputQueryResults();
}

@Override
public Comparator<ResolvedMigration> getRepeatableMigrationComparator() {
return config.getRepeatableMigrationComparator();
}

public FluentConfiguration repeatableMigrationComparator(String comparator) {
config.setRepeatableMigrationComparatorAsClassName(comparator);
return this;
}

public FluentConfiguration repeatableMigrationComparator(Comparator<ResolvedMigration> comparator) {
config.setRepeatableMigrationComparator(comparator);
return this;
}

/**
* Sets the stream where to output the SQL statements of a migration dry run. {@code null} to execute the SQL statements
* directly against the database. The stream when be closing when Flyway finishes writing the output.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Configuration-related utilities.
*/
public class ConfigUtils {

private static Log LOG = LogFactory.getLog(ConfigUtils.class);

/**
Expand Down Expand Up @@ -71,6 +72,7 @@ public class ConfigUtils {
public static final String PLACEHOLDERS_PROPERTY_PREFIX = "flyway.placeholders.";
public static final String REPEATABLE_SQL_MIGRATION_PREFIX = "flyway.repeatableSqlMigrationPrefix";
public static final String RESOLVERS = "flyway.resolvers";
public static final String REPEATABLE_MIGRATION_COMPARATOR = "flyway.repeatableMigrationComparator";
public static final String SCHEMAS = "flyway.schemas";
public static final String SKIP_DEFAULT_CALLBACKS = "flyway.skipDefaultCallbacks";
public static final String SKIP_DEFAULT_RESOLVERS = "flyway.skipDefaultResolvers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -107,7 +108,8 @@ public List<ResolvedMigration> resolveMigrations(Context context) {
*/
private List<ResolvedMigration> doFindAvailableMigrations(Context context) throws FlywayException {
List<ResolvedMigration> migrations = new ArrayList<>(collectMigrations(migrationResolvers, context));
Collections.sort(migrations, new ResolvedMigrationComparator());
Comparator<ResolvedMigration> repeatableMigrationComparator = context.getConfiguration().getRepeatableMigrationComparator();
Collections.sort(migrations, new ResolvedMigrationComparator(repeatableMigrationComparator));

checkForIncompatibilities(migrations);

Expand Down Expand Up @@ -137,7 +139,7 @@ static Collection<ResolvedMigration> collectMigrations(Collection<MigrationResol
*/
/* private -> for testing */
static void checkForIncompatibilities(List<ResolvedMigration> migrations) {
ResolvedMigrationComparator resolvedMigrationComparator = new ResolvedMigrationComparator();
ResolvedMigrationComparator resolvedMigrationComparator = new ResolvedMigrationComparator(ResolvedMigrationComparator.DEFAULT_REPEATABLE_MIGRATION_COMPARATOR);
// check for more than one migration with same version
for (int i = 0; i < migrations.size() - 1; i++) {
ResolvedMigration current = migrations.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@
* Comparator for ResolvedMigration.
*/
public class ResolvedMigrationComparator implements Comparator<ResolvedMigration> {
@Override
public int compare(ResolvedMigration o1, ResolvedMigration o2) {
if ((o1.getVersion() != null) && o2.getVersion() != null) {
int v = o1.getVersion().compareTo(o2.getVersion());










public static final Comparator<ResolvedMigration> DEFAULT_REPEATABLE_MIGRATION_COMPARATOR =
Comparator.comparing(ResolvedMigration::getDescription);

private final Comparator<ResolvedMigration> repeatableMigrationComparator;

public ResolvedMigrationComparator(Comparator<ResolvedMigration> repeatableMigrationComparator) {
this.repeatableMigrationComparator = repeatableMigrationComparator;
}

@Override
public int compare(ResolvedMigration o1, ResolvedMigration o2) {
if ((o1.getVersion() != null) && o2.getVersion() != null) {
int v = o1.getVersion().compareTo(o2.getVersion());
return v;
}
if (o1.getVersion() != null) {
Expand All @@ -48,6 +45,6 @@ public int compare(ResolvedMigration o1, ResolvedMigration o2) {
if (o2.getVersion() != null) {
return 1;
}
return o1.getDescription().compareTo(o2.getDescription());
return repeatableMigrationComparator.compare(o1, o2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<ResolvedMigration> resolveMigrations(Context context) {
migrations.add(new ResolvedJavaMigration(javaMigration));
}

Collections.sort(migrations, new ResolvedMigrationComparator());
Collections.sort(migrations, new ResolvedMigrationComparator(context.getConfiguration().getRepeatableMigrationComparator()));
return migrations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<ResolvedMigration> resolveMigrations(Context context) {
migrations.add(new ResolvedJavaMigration(javaMigration));
}

Collections.sort(migrations, new ResolvedMigrationComparator());
Collections.sort(migrations, new ResolvedMigrationComparator(context.getConfiguration().getRepeatableMigrationComparator()));
return migrations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public List<ResolvedMigration> resolveMigrations(Context context) {

);

Collections.sort(migrations, new ResolvedMigrationComparator());
Collections.sort(migrations, new ResolvedMigrationComparator(context.getConfiguration().getRepeatableMigrationComparator()));
return migrations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public class FlywayExtension {
*/
public Boolean skipDefaultResolvers;

/**
* Fully qualified class name of custom Comparator to determine the order of repeatable migrations.
* <p>(default: by description)</p>
*/
public String repeatableMigrationComparator;

/**
* The file name prefix for versioned SQL migrations. (default: V)
* <p>Versioned SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ public abstract class AbstractFlywayTask extends DefaultTask {
*/
public Boolean skipDefaultResolvers;

/**
* Fully qualified class name of custom Comparator to determine the order of repeatable migrations.
* <p>(default: by description)</p>
*/
public String repeatableMigrationComparator;

/**
* The file name prefix for versioned SQL migrations. (default: V)
* <p>Versioned SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,
Expand Down Expand Up @@ -671,6 +677,7 @@ private Map<String, String> createFlywayConfig(Map<String, String> envVars) {
putIfSet(conf, ConfigUtils.CALLBACKS, StringUtils.arrayToCommaDelimitedString(callbacks), StringUtils.arrayToCommaDelimitedString(extension.callbacks));
putIfSet(conf, ConfigUtils.ERROR_OVERRIDES, StringUtils.arrayToCommaDelimitedString(errorOverrides), StringUtils.arrayToCommaDelimitedString(extension.errorOverrides));

putIfSet(conf, ConfigUtils.REPEATABLE_MIGRATION_COMPARATOR, repeatableMigrationComparator, extension.repeatableMigrationComparator);
putIfSet(conf, ConfigUtils.DRYRUN_OUTPUT, dryRunOutput, extension.dryRunOutput);
putIfSet(conf, ConfigUtils.STREAM, stream, extension.stream);
putIfSet(conf, ConfigUtils.BATCH, batch, extension.batch);
Expand Down

0 comments on commit b46c1dc

Please sign in to comment.