Skip to content

Commit

Permalink
#30 fix warnings about using deprecated api after upgrade to Flyway 5…
Browse files Browse the repository at this point in the history
….2.1
  • Loading branch information
tomix26 committed Dec 7, 2018
1 parent a899862 commit 05b3d39
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 62 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ ext {
[core: '4.2.0', test: '5.0.0'],

[core: '5.1.4', test: '5.1.0'],
[core: '5.2.0', test: '5.1.0'],
[core: '5.2.1', test: '5.1.0'],

[core: '5.0.7', test: '5.0.0'] // default version
]
embeddedPostgresVersions = ['9.3.24', '9.4.19', '9.5.14', '9.6.10', '10.5.0']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;

import static org.springframework.test.util.ReflectionTestUtils.getField;
import static org.springframework.test.util.ReflectionTestUtils.invokeMethod;

public class FlywayClassUtils {

private static final boolean flywayNameAttributePresent = ClassUtils.hasMethod(FlywayTest.class, "flywayName");
Expand Down Expand Up @@ -40,7 +43,12 @@ public class FlywayClassUtils {
if (flywayVersion >= 50) {
boolean isCommercial;
try {
new Flyway().getUndoSqlMigrationPrefix();
if (flywayVersion >= 51) {
Object flywayConfig = getField(new Flyway(), "configuration");
invokeMethod(flywayConfig, "getUndoSqlMigrationPrefix");
} else {
new Flyway().getUndoSqlMigrationPrefix();
}
isCommercial = true;
} catch (FlywayException e) {
isCommercial = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.Objects;

import static org.springframework.test.util.ReflectionTestUtils.getField;
import static org.springframework.test.util.ReflectionTestUtils.invokeMethod;

/**
Expand Down Expand Up @@ -50,7 +51,6 @@ public class FlywayConfigSnapshot {
private final String repeatableSqlMigrationPrefix;
private final String sqlMigrationSeparator;
private final String sqlMigrationPrefix;
private final String sqlMigrationSuffix;
private final String placeholderPrefix;
private final String placeholderSuffix;
private final Object encoding;
Expand Down Expand Up @@ -79,47 +79,53 @@ public class FlywayConfigSnapshot {
private final int connectRetries;

public FlywayConfigSnapshot(Flyway flyway) {
this.classLoader = flyway.getClassLoader();
this.dataSource = flyway.getDataSource();
this.resolvers = flyway.getResolvers();
this.callbacks = invokeMethod(flyway, "getCallbacks");
this.sqlMigrationSuffix = flyway.getSqlMigrationSuffix();
this.sqlMigrationSeparator = flyway.getSqlMigrationSeparator();
this.sqlMigrationPrefix = flyway.getSqlMigrationPrefix();
this.placeholderSuffix = flyway.getPlaceholderSuffix();
this.placeholderPrefix = flyway.getPlaceholderPrefix();
this.placeholders = flyway.getPlaceholders();
this.target = flyway.getTarget();
this.table = flyway.getTable();
this.schemas = flyway.getSchemas();
this.encoding = invokeMethod(flyway, "getEncoding");
this.locations = invokeMethod(flyway, "getLocations");
this.outOfOrder = flyway.isOutOfOrder();
this.validateOnMigrate = flyway.isValidateOnMigrate();
this.cleanOnValidationError = flyway.isCleanOnValidationError();
final Object config;
if (flywayVersion >= 51) {
config = getField(flyway, "configuration");
} else {
config = flyway;
}

this.classLoader = invokeMethod(config, "getClassLoader");
this.dataSource = invokeMethod(config, "getDataSource");
this.resolvers = invokeMethod(config, "getResolvers");
this.callbacks = invokeMethod(config, "getCallbacks");
this.sqlMigrationSeparator = invokeMethod(config, "getSqlMigrationSeparator");
this.sqlMigrationPrefix = invokeMethod(config, "getSqlMigrationPrefix");
this.placeholderSuffix = invokeMethod(config, "getPlaceholderSuffix");
this.placeholderPrefix = invokeMethod(config, "getPlaceholderPrefix");
this.placeholders = invokeMethod(config, "getPlaceholders");
this.target = invokeMethod(config, "getTarget");
this.table = invokeMethod(config, "getTable");
this.schemas = invokeMethod(config, "getSchemas");
this.encoding = invokeMethod(config, "getEncoding");
this.locations = invokeMethod(config, "getLocations");
this.outOfOrder = invokeMethod(config, "isOutOfOrder");
this.validateOnMigrate = invokeMethod(config, "isValidateOnMigrate");
this.cleanOnValidationError = invokeMethod(config, "isCleanOnValidationError");

if (flywayVersion >= 31) {
this.baselineVersion = flyway.getBaselineVersion();
this.baselineDescription = flyway.getBaselineDescription();
this.baselineOnMigrate = flyway.isBaselineOnMigrate();
this.baselineVersion = invokeMethod(config, "getBaselineVersion");
this.baselineDescription = invokeMethod(config, "getBaselineDescription");
this.baselineOnMigrate = invokeMethod(config, "isBaselineOnMigrate");
} else {
this.baselineVersion = null;
this.baselineDescription = null;
this.baselineOnMigrate = false;
}

if (flywayVersion >= 32) {
this.placeholderReplacement = flyway.isPlaceholderReplacement();
this.placeholderReplacement = invokeMethod(config, "isPlaceholderReplacement");
} else {
this.placeholderReplacement = true;
}

if (flywayVersion >= 40) {
this.skipDefaultResolvers = flyway.isSkipDefaultResolvers();
this.skipDefaultCallbacks = flyway.isSkipDefaultCallbacks();
this.repeatableSqlMigrationPrefix = flyway.getRepeatableSqlMigrationPrefix();
this.ignoreFutureMigrations = flyway.isIgnoreFutureMigrations();
this.cleanDisabled = flyway.isCleanDisabled();
this.skipDefaultResolvers = invokeMethod(config, "isSkipDefaultResolvers");
this.skipDefaultCallbacks = invokeMethod(config, "isSkipDefaultCallbacks");
this.repeatableSqlMigrationPrefix = invokeMethod(config, "getRepeatableSqlMigrationPrefix");
this.ignoreFutureMigrations = invokeMethod(config, "isIgnoreFutureMigrations");
this.cleanDisabled = invokeMethod(config, "isCleanDisabled");
} else {
this.skipDefaultResolvers = false;
this.skipDefaultCallbacks = false;
Expand All @@ -129,54 +135,55 @@ public FlywayConfigSnapshot(Flyway flyway) {
}

if (flywayVersion >= 41) {
this.ignoreMissingMigrations = flyway.isIgnoreMissingMigrations();
this.installedBy = flyway.getInstalledBy();
this.ignoreMissingMigrations = invokeMethod(config, "isIgnoreMissingMigrations");
this.installedBy = invokeMethod(config, "getInstalledBy");
} else {
this.ignoreMissingMigrations = false;
this.installedBy = null;
}

if (flywayVersion >= 41 && flywayVersion < 50) {
this.allowMixedMigrations = invokeMethod(flyway, "isAllowMixedMigrations");
this.allowMixedMigrations = invokeMethod(config, "isAllowMixedMigrations");
} else {
this.allowMixedMigrations = false;
}

if (flywayVersion >= 42) {
this.mixed = flyway.isMixed();
this.group = flyway.isGroup();
this.mixed = invokeMethod(config, "isMixed");
this.group = invokeMethod(config, "isGroup");
} else {
this.mixed = false;
this.group = false;
}

if (flywayVersion >= 50) {
this.sqlMigrationSuffixes = flyway.getSqlMigrationSuffixes();
this.sqlMigrationSuffixes = invokeMethod(config, "getSqlMigrationSuffixes");
} else {
this.sqlMigrationSuffixes = null;
String sqlMigrationSuffix = invokeMethod(config, "getSqlMigrationSuffix");
this.sqlMigrationSuffixes = new String[] {sqlMigrationSuffix};
}

if (flywayVersion >= 50 && isFlywayPro) {
this.undoSqlMigrationPrefix = flyway.getUndoSqlMigrationPrefix();
this.errorHandlers = flyway.getErrorHandlers();
this.dryRun = flyway.getDryRunOutput() != null;
this.undoSqlMigrationPrefix = invokeMethod(config, "getUndoSqlMigrationPrefix");
this.errorHandlers = invokeMethod(config, "getErrorHandlers");
this.dryRun = invokeMethod(config, "getDryRunOutput") != null;
} else {
this.undoSqlMigrationPrefix = null;
this.errorHandlers = null;
this.dryRun = false;
}

if (flywayVersion >= 51) {
this.ignoreIgnoredMigrations = invokeMethod(flyway, "isIgnoreIgnoredMigrations");
this.ignoreIgnoredMigrations = invokeMethod(config, "isIgnoreIgnoredMigrations");
} else {
this.ignoreIgnoredMigrations = false;
}

if (flywayVersion >= 51 && isFlywayPro) {
this.errorOverrides = invokeMethod(flyway, "getErrorOverrides");
this.stream = invokeMethod(flyway, "isStream");
this.batch = invokeMethod(flyway, "isBatch");
this.oracleSqlPlus = invokeMethod(flyway, "isOracleSqlplus");
this.errorOverrides = invokeMethod(config, "getErrorOverrides");
this.stream = invokeMethod(config, "isStream");
this.batch = invokeMethod(config, "isBatch");
this.oracleSqlPlus = invokeMethod(config, "isOracleSqlplus");
} else {
this.errorOverrides = null;
this.stream = false;
Expand All @@ -185,17 +192,17 @@ public FlywayConfigSnapshot(Flyway flyway) {
}

if (flywayVersion >= 52) {
this.ignorePendingMigrations = invokeMethod(flyway, "isIgnorePendingMigrations");
this.connectRetries = invokeMethod(flyway, "getConnectRetries");
this.initSql = invokeMethod(flyway, "getInitSql");
this.ignorePendingMigrations = invokeMethod(config, "isIgnorePendingMigrations");
this.connectRetries = invokeMethod(config, "getConnectRetries");
this.initSql = invokeMethod(config, "getInitSql");
} else {
this.ignorePendingMigrations = false;
this.connectRetries = 0;
this.initSql = null;
}

if (flywayVersion >= 52 && isFlywayPro) {
this.licenseKey = invokeMethod(flyway, "getLicenseKey");
this.licenseKey = invokeMethod(config, "getLicenseKey");
} else {
this.licenseKey = null;
}
Expand Down Expand Up @@ -233,10 +240,6 @@ public boolean isSkipDefaultCallbacks() {
return skipDefaultCallbacks;
}

public String getSqlMigrationSuffix() {
return sqlMigrationSuffix;
}

public String[] getSqlMigrationSuffixes() {
return sqlMigrationSuffixes;
}
Expand Down Expand Up @@ -421,7 +424,6 @@ public boolean equals(Object o) {
Objects.equals(repeatableSqlMigrationPrefix, that.repeatableSqlMigrationPrefix) &&
Objects.equals(sqlMigrationSeparator, that.sqlMigrationSeparator) &&
Objects.equals(sqlMigrationPrefix, that.sqlMigrationPrefix) &&
Objects.equals(sqlMigrationSuffix, that.sqlMigrationSuffix) &&
Objects.equals(placeholderPrefix, that.placeholderPrefix) &&
Objects.equals(placeholderSuffix, that.placeholderSuffix) &&
Objects.equals(encoding, that.encoding) &&
Expand All @@ -438,8 +440,8 @@ public int hashCode() {
Arrays.hashCode(sqlMigrationSuffixes), Arrays.hashCode(errorOverrides),
baselineVersion, target, placeholders, table, baselineDescription,
undoSqlMigrationPrefix, repeatableSqlMigrationPrefix,
sqlMigrationSeparator, sqlMigrationPrefix, sqlMigrationSuffix,
placeholderPrefix, placeholderSuffix, encoding, initSql, licenseKey,
sqlMigrationSeparator, sqlMigrationPrefix, placeholderPrefix,
placeholderSuffix, encoding, initSql, licenseKey,
skipDefaultResolvers, skipDefaultCallbacks, placeholderReplacement, baselineOnMigrate,
outOfOrder, ignoreMissingMigrations, ignoreIgnoredMigrations, ignorePendingMigrations,
ignoreFutureMigrations, validateOnMigrate, cleanOnValidationError, cleanDisabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ protected static DataSource reloadDataSource(FlywayDataSourceContext dataSourceC
String[] oldLocations = getFlywayLocations(flywayBean);
try {
if (annotation.overrideLocations()) {
flywayBean.setLocations(annotation.locationsForMigrate());
setFlywayLocations(flywayBean, annotation.locationsForMigrate());
} else {
flywayBean.setLocations(ObjectArrays.concat(oldLocations, annotation.locationsForMigrate(), String.class));
setFlywayLocations(flywayBean, ObjectArrays.concat(oldLocations, annotation.locationsForMigrate(), String.class));
}
return dataSourceContext.reload(flywayBean).get();
} finally {
flywayBean.setLocations(oldLocations);
setFlywayLocations(flywayBean, oldLocations);
}
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ protected static Collection<ResolvedMigration> resolveMigrations(Flyway flyway,
protected static MigrationResolver createMigrationResolver(Flyway flyway, String... locations) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
String[] oldLocations = getFlywayLocations(flyway);
try {
flyway.setLocations(locations);
setFlywayLocations(flyway, locations);

if (flywayVersion >= 52) {
Object configuration = getField(flyway, "configuration");
Expand All @@ -278,7 +278,7 @@ protected static MigrationResolver createMigrationResolver(Flyway flyway, String
return invokeMethod(flyway, "createMigrationResolver", (Object) null);
}
} finally {
flyway.setLocations(oldLocations);
setFlywayLocations(flyway, oldLocations);
}
}

Expand All @@ -292,14 +292,24 @@ protected Flyway getFlywayBean(ApplicationContext applicationContext, FlywayTest

protected static String[] getFlywayLocations(Flyway flyway) {
if (flywayVersion >= 51) {
return Arrays.stream((Object[]) invokeMethod(flyway, "getLocations"))
Object configuration = getField(flyway, "configuration");
return Arrays.stream((Object[]) invokeMethod(configuration, "getLocations"))
.map(location -> invokeMethod(location, "getDescriptor"))
.toArray(String[]::new);
} else {
return flyway.getLocations();
}
}

protected static void setFlywayLocations(Flyway flyway, String[] locations) {
if (flywayVersion >= 51) {
Object configuration = getField(flyway, "configuration");
invokeMethod(configuration, "setLocationsAsStrings", (Object) locations);
} else {
flyway.setLocations(locations);
}
}

protected static FlywayDataSourceContext getDataSourceContext(ApplicationContext context, Flyway flywayBean) {
Map<String, Flyway> flywayBeans = context.getBeansOfType(Flyway.class);
String flywayBeanName = flywayBeans.entrySet().stream()
Expand Down

0 comments on commit 05b3d39

Please sign in to comment.