Skip to content

Commit 05b3d39

Browse files
committed
#30 fix warnings about using deprecated api after upgrade to Flyway 5.2.1
1 parent a899862 commit 05b3d39

File tree

4 files changed

+83
-62
lines changed

4 files changed

+83
-62
lines changed

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ ext {
2424
[core: '4.2.0', test: '5.0.0'],
2525

2626
[core: '5.1.4', test: '5.1.0'],
27-
[core: '5.2.0', test: '5.1.0'],
27+
[core: '5.2.1', test: '5.1.0'],
28+
2829
[core: '5.0.7', test: '5.0.0'] // default version
2930
]
3031
embeddedPostgresVersions = ['9.3.24', '9.4.19', '9.5.14', '9.6.10', '10.5.0']

embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/FlywayClassUtils.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import org.springframework.core.io.ClassPathResource;
99
import org.springframework.util.ClassUtils;
1010

11+
import static org.springframework.test.util.ReflectionTestUtils.getField;
12+
import static org.springframework.test.util.ReflectionTestUtils.invokeMethod;
13+
1114
public class FlywayClassUtils {
1215

1316
private static final boolean flywayNameAttributePresent = ClassUtils.hasMethod(FlywayTest.class, "flywayName");
@@ -40,7 +43,12 @@ public class FlywayClassUtils {
4043
if (flywayVersion >= 50) {
4144
boolean isCommercial;
4245
try {
43-
new Flyway().getUndoSqlMigrationPrefix();
46+
if (flywayVersion >= 51) {
47+
Object flywayConfig = getField(new Flyway(), "configuration");
48+
invokeMethod(flywayConfig, "getUndoSqlMigrationPrefix");
49+
} else {
50+
new Flyway().getUndoSqlMigrationPrefix();
51+
}
4452
isCommercial = true;
4553
} catch (FlywayException e) {
4654
isCommercial = false;

embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/FlywayConfigSnapshot.java

+56-54
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.Objects;
1111

12+
import static org.springframework.test.util.ReflectionTestUtils.getField;
1213
import static org.springframework.test.util.ReflectionTestUtils.invokeMethod;
1314

1415
/**
@@ -50,7 +51,6 @@ public class FlywayConfigSnapshot {
5051
private final String repeatableSqlMigrationPrefix;
5152
private final String sqlMigrationSeparator;
5253
private final String sqlMigrationPrefix;
53-
private final String sqlMigrationSuffix;
5454
private final String placeholderPrefix;
5555
private final String placeholderSuffix;
5656
private final Object encoding;
@@ -79,47 +79,53 @@ public class FlywayConfigSnapshot {
7979
private final int connectRetries;
8080

8181
public FlywayConfigSnapshot(Flyway flyway) {
82-
this.classLoader = flyway.getClassLoader();
83-
this.dataSource = flyway.getDataSource();
84-
this.resolvers = flyway.getResolvers();
85-
this.callbacks = invokeMethod(flyway, "getCallbacks");
86-
this.sqlMigrationSuffix = flyway.getSqlMigrationSuffix();
87-
this.sqlMigrationSeparator = flyway.getSqlMigrationSeparator();
88-
this.sqlMigrationPrefix = flyway.getSqlMigrationPrefix();
89-
this.placeholderSuffix = flyway.getPlaceholderSuffix();
90-
this.placeholderPrefix = flyway.getPlaceholderPrefix();
91-
this.placeholders = flyway.getPlaceholders();
92-
this.target = flyway.getTarget();
93-
this.table = flyway.getTable();
94-
this.schemas = flyway.getSchemas();
95-
this.encoding = invokeMethod(flyway, "getEncoding");
96-
this.locations = invokeMethod(flyway, "getLocations");
97-
this.outOfOrder = flyway.isOutOfOrder();
98-
this.validateOnMigrate = flyway.isValidateOnMigrate();
99-
this.cleanOnValidationError = flyway.isCleanOnValidationError();
82+
final Object config;
83+
if (flywayVersion >= 51) {
84+
config = getField(flyway, "configuration");
85+
} else {
86+
config = flyway;
87+
}
88+
89+
this.classLoader = invokeMethod(config, "getClassLoader");
90+
this.dataSource = invokeMethod(config, "getDataSource");
91+
this.resolvers = invokeMethod(config, "getResolvers");
92+
this.callbacks = invokeMethod(config, "getCallbacks");
93+
this.sqlMigrationSeparator = invokeMethod(config, "getSqlMigrationSeparator");
94+
this.sqlMigrationPrefix = invokeMethod(config, "getSqlMigrationPrefix");
95+
this.placeholderSuffix = invokeMethod(config, "getPlaceholderSuffix");
96+
this.placeholderPrefix = invokeMethod(config, "getPlaceholderPrefix");
97+
this.placeholders = invokeMethod(config, "getPlaceholders");
98+
this.target = invokeMethod(config, "getTarget");
99+
this.table = invokeMethod(config, "getTable");
100+
this.schemas = invokeMethod(config, "getSchemas");
101+
this.encoding = invokeMethod(config, "getEncoding");
102+
this.locations = invokeMethod(config, "getLocations");
103+
this.outOfOrder = invokeMethod(config, "isOutOfOrder");
104+
this.validateOnMigrate = invokeMethod(config, "isValidateOnMigrate");
105+
this.cleanOnValidationError = invokeMethod(config, "isCleanOnValidationError");
100106

101107
if (flywayVersion >= 31) {
102-
this.baselineVersion = flyway.getBaselineVersion();
103-
this.baselineDescription = flyway.getBaselineDescription();
104-
this.baselineOnMigrate = flyway.isBaselineOnMigrate();
108+
this.baselineVersion = invokeMethod(config, "getBaselineVersion");
109+
this.baselineDescription = invokeMethod(config, "getBaselineDescription");
110+
this.baselineOnMigrate = invokeMethod(config, "isBaselineOnMigrate");
105111
} else {
106112
this.baselineVersion = null;
107113
this.baselineDescription = null;
108114
this.baselineOnMigrate = false;
109115
}
110116

111117
if (flywayVersion >= 32) {
112-
this.placeholderReplacement = flyway.isPlaceholderReplacement();
118+
this.placeholderReplacement = invokeMethod(config, "isPlaceholderReplacement");
113119
} else {
114120
this.placeholderReplacement = true;
115121
}
116122

117123
if (flywayVersion >= 40) {
118-
this.skipDefaultResolvers = flyway.isSkipDefaultResolvers();
119-
this.skipDefaultCallbacks = flyway.isSkipDefaultCallbacks();
120-
this.repeatableSqlMigrationPrefix = flyway.getRepeatableSqlMigrationPrefix();
121-
this.ignoreFutureMigrations = flyway.isIgnoreFutureMigrations();
122-
this.cleanDisabled = flyway.isCleanDisabled();
124+
this.skipDefaultResolvers = invokeMethod(config, "isSkipDefaultResolvers");
125+
this.skipDefaultCallbacks = invokeMethod(config, "isSkipDefaultCallbacks");
126+
this.repeatableSqlMigrationPrefix = invokeMethod(config, "getRepeatableSqlMigrationPrefix");
127+
this.ignoreFutureMigrations = invokeMethod(config, "isIgnoreFutureMigrations");
128+
this.cleanDisabled = invokeMethod(config, "isCleanDisabled");
123129
} else {
124130
this.skipDefaultResolvers = false;
125131
this.skipDefaultCallbacks = false;
@@ -129,54 +135,55 @@ public FlywayConfigSnapshot(Flyway flyway) {
129135
}
130136

131137
if (flywayVersion >= 41) {
132-
this.ignoreMissingMigrations = flyway.isIgnoreMissingMigrations();
133-
this.installedBy = flyway.getInstalledBy();
138+
this.ignoreMissingMigrations = invokeMethod(config, "isIgnoreMissingMigrations");
139+
this.installedBy = invokeMethod(config, "getInstalledBy");
134140
} else {
135141
this.ignoreMissingMigrations = false;
136142
this.installedBy = null;
137143
}
138144

139145
if (flywayVersion >= 41 && flywayVersion < 50) {
140-
this.allowMixedMigrations = invokeMethod(flyway, "isAllowMixedMigrations");
146+
this.allowMixedMigrations = invokeMethod(config, "isAllowMixedMigrations");
141147
} else {
142148
this.allowMixedMigrations = false;
143149
}
144150

145151
if (flywayVersion >= 42) {
146-
this.mixed = flyway.isMixed();
147-
this.group = flyway.isGroup();
152+
this.mixed = invokeMethod(config, "isMixed");
153+
this.group = invokeMethod(config, "isGroup");
148154
} else {
149155
this.mixed = false;
150156
this.group = false;
151157
}
152158

153159
if (flywayVersion >= 50) {
154-
this.sqlMigrationSuffixes = flyway.getSqlMigrationSuffixes();
160+
this.sqlMigrationSuffixes = invokeMethod(config, "getSqlMigrationSuffixes");
155161
} else {
156-
this.sqlMigrationSuffixes = null;
162+
String sqlMigrationSuffix = invokeMethod(config, "getSqlMigrationSuffix");
163+
this.sqlMigrationSuffixes = new String[] {sqlMigrationSuffix};
157164
}
158165

159166
if (flywayVersion >= 50 && isFlywayPro) {
160-
this.undoSqlMigrationPrefix = flyway.getUndoSqlMigrationPrefix();
161-
this.errorHandlers = flyway.getErrorHandlers();
162-
this.dryRun = flyway.getDryRunOutput() != null;
167+
this.undoSqlMigrationPrefix = invokeMethod(config, "getUndoSqlMigrationPrefix");
168+
this.errorHandlers = invokeMethod(config, "getErrorHandlers");
169+
this.dryRun = invokeMethod(config, "getDryRunOutput") != null;
163170
} else {
164171
this.undoSqlMigrationPrefix = null;
165172
this.errorHandlers = null;
166173
this.dryRun = false;
167174
}
168175

169176
if (flywayVersion >= 51) {
170-
this.ignoreIgnoredMigrations = invokeMethod(flyway, "isIgnoreIgnoredMigrations");
177+
this.ignoreIgnoredMigrations = invokeMethod(config, "isIgnoreIgnoredMigrations");
171178
} else {
172179
this.ignoreIgnoredMigrations = false;
173180
}
174181

175182
if (flywayVersion >= 51 && isFlywayPro) {
176-
this.errorOverrides = invokeMethod(flyway, "getErrorOverrides");
177-
this.stream = invokeMethod(flyway, "isStream");
178-
this.batch = invokeMethod(flyway, "isBatch");
179-
this.oracleSqlPlus = invokeMethod(flyway, "isOracleSqlplus");
183+
this.errorOverrides = invokeMethod(config, "getErrorOverrides");
184+
this.stream = invokeMethod(config, "isStream");
185+
this.batch = invokeMethod(config, "isBatch");
186+
this.oracleSqlPlus = invokeMethod(config, "isOracleSqlplus");
180187
} else {
181188
this.errorOverrides = null;
182189
this.stream = false;
@@ -185,17 +192,17 @@ public FlywayConfigSnapshot(Flyway flyway) {
185192
}
186193

187194
if (flywayVersion >= 52) {
188-
this.ignorePendingMigrations = invokeMethod(flyway, "isIgnorePendingMigrations");
189-
this.connectRetries = invokeMethod(flyway, "getConnectRetries");
190-
this.initSql = invokeMethod(flyway, "getInitSql");
195+
this.ignorePendingMigrations = invokeMethod(config, "isIgnorePendingMigrations");
196+
this.connectRetries = invokeMethod(config, "getConnectRetries");
197+
this.initSql = invokeMethod(config, "getInitSql");
191198
} else {
192199
this.ignorePendingMigrations = false;
193200
this.connectRetries = 0;
194201
this.initSql = null;
195202
}
196203

197204
if (flywayVersion >= 52 && isFlywayPro) {
198-
this.licenseKey = invokeMethod(flyway, "getLicenseKey");
205+
this.licenseKey = invokeMethod(config, "getLicenseKey");
199206
} else {
200207
this.licenseKey = null;
201208
}
@@ -233,10 +240,6 @@ public boolean isSkipDefaultCallbacks() {
233240
return skipDefaultCallbacks;
234241
}
235242

236-
public String getSqlMigrationSuffix() {
237-
return sqlMigrationSuffix;
238-
}
239-
240243
public String[] getSqlMigrationSuffixes() {
241244
return sqlMigrationSuffixes;
242245
}
@@ -421,7 +424,6 @@ public boolean equals(Object o) {
421424
Objects.equals(repeatableSqlMigrationPrefix, that.repeatableSqlMigrationPrefix) &&
422425
Objects.equals(sqlMigrationSeparator, that.sqlMigrationSeparator) &&
423426
Objects.equals(sqlMigrationPrefix, that.sqlMigrationPrefix) &&
424-
Objects.equals(sqlMigrationSuffix, that.sqlMigrationSuffix) &&
425427
Objects.equals(placeholderPrefix, that.placeholderPrefix) &&
426428
Objects.equals(placeholderSuffix, that.placeholderSuffix) &&
427429
Objects.equals(encoding, that.encoding) &&
@@ -438,8 +440,8 @@ public int hashCode() {
438440
Arrays.hashCode(sqlMigrationSuffixes), Arrays.hashCode(errorOverrides),
439441
baselineVersion, target, placeholders, table, baselineDescription,
440442
undoSqlMigrationPrefix, repeatableSqlMigrationPrefix,
441-
sqlMigrationSeparator, sqlMigrationPrefix, sqlMigrationSuffix,
442-
placeholderPrefix, placeholderSuffix, encoding, initSql, licenseKey,
443+
sqlMigrationSeparator, sqlMigrationPrefix, placeholderPrefix,
444+
placeholderSuffix, encoding, initSql, licenseKey,
443445
skipDefaultResolvers, skipDefaultCallbacks, placeholderReplacement, baselineOnMigrate,
444446
outOfOrder, ignoreMissingMigrations, ignoreIgnoredMigrations, ignorePendingMigrations,
445447
ignoreFutureMigrations, validateOnMigrate, cleanOnValidationError, cleanDisabled,

embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/OptimizedFlywayTestExecutionListener.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ protected static DataSource reloadDataSource(FlywayDataSourceContext dataSourceC
187187
String[] oldLocations = getFlywayLocations(flywayBean);
188188
try {
189189
if (annotation.overrideLocations()) {
190-
flywayBean.setLocations(annotation.locationsForMigrate());
190+
setFlywayLocations(flywayBean, annotation.locationsForMigrate());
191191
} else {
192-
flywayBean.setLocations(ObjectArrays.concat(oldLocations, annotation.locationsForMigrate(), String.class));
192+
setFlywayLocations(flywayBean, ObjectArrays.concat(oldLocations, annotation.locationsForMigrate(), String.class));
193193
}
194194
return dataSourceContext.reload(flywayBean).get();
195195
} finally {
196-
flywayBean.setLocations(oldLocations);
196+
setFlywayLocations(flywayBean, oldLocations);
197197
}
198198
}
199199
}
@@ -254,7 +254,7 @@ protected static Collection<ResolvedMigration> resolveMigrations(Flyway flyway,
254254
protected static MigrationResolver createMigrationResolver(Flyway flyway, String... locations) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
255255
String[] oldLocations = getFlywayLocations(flyway);
256256
try {
257-
flyway.setLocations(locations);
257+
setFlywayLocations(flyway, locations);
258258

259259
if (flywayVersion >= 52) {
260260
Object configuration = getField(flyway, "configuration");
@@ -278,7 +278,7 @@ protected static MigrationResolver createMigrationResolver(Flyway flyway, String
278278
return invokeMethod(flyway, "createMigrationResolver", (Object) null);
279279
}
280280
} finally {
281-
flyway.setLocations(oldLocations);
281+
setFlywayLocations(flyway, oldLocations);
282282
}
283283
}
284284

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

293293
protected static String[] getFlywayLocations(Flyway flyway) {
294294
if (flywayVersion >= 51) {
295-
return Arrays.stream((Object[]) invokeMethod(flyway, "getLocations"))
295+
Object configuration = getField(flyway, "configuration");
296+
return Arrays.stream((Object[]) invokeMethod(configuration, "getLocations"))
296297
.map(location -> invokeMethod(location, "getDescriptor"))
297298
.toArray(String[]::new);
298299
} else {
299300
return flyway.getLocations();
300301
}
301302
}
302303

304+
protected static void setFlywayLocations(Flyway flyway, String[] locations) {
305+
if (flywayVersion >= 51) {
306+
Object configuration = getField(flyway, "configuration");
307+
invokeMethod(configuration, "setLocationsAsStrings", (Object) locations);
308+
} else {
309+
flyway.setLocations(locations);
310+
}
311+
}
312+
303313
protected static FlywayDataSourceContext getDataSourceContext(ApplicationContext context, Flyway flywayBean) {
304314
Map<String, Flyway> flywayBeans = context.getBeansOfType(Flyway.class);
305315
String flywayBeanName = flywayBeans.entrySet().stream()

0 commit comments

Comments
 (0)