|
17 | 17 | package org.springframework.boot.autoconfigure.flyway; |
18 | 18 |
|
19 | 19 | import java.util.Arrays; |
| 20 | +import java.util.Collection; |
20 | 21 | import java.util.Collections; |
21 | 22 | import java.util.HashSet; |
22 | 23 | import java.util.List; |
@@ -104,8 +105,6 @@ public static class FlywayConfiguration { |
104 | 105 |
|
105 | 106 | private List<FlywayCallback> flywayCallbacks; |
106 | 107 |
|
107 | | - private static final String VENDOR_PLACEHOLDER = "{vendor}"; |
108 | | - |
109 | 108 | public FlywayConfiguration(FlywayProperties properties, |
110 | 109 | ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource, |
111 | 110 | @FlywayDataSource ObjectProvider<DataSource> flywayDataSource, |
@@ -136,49 +135,22 @@ else if (this.flywayDataSource != null) { |
136 | 135 | } |
137 | 136 | flyway.setCallbacks(this.flywayCallbacks |
138 | 137 | .toArray(new FlywayCallback[this.flywayCallbacks.size()])); |
139 | | - String[] locations = resolveLocations(this.properties.getLocations().toArray(new String[0]), flyway.getDataSource()); |
| 138 | + String[] locations = new LocationResolver(flyway.getDataSource()) |
| 139 | + .resolveLocations(this.properties.getLocations()); |
140 | 140 | checkLocationExists(locations); |
141 | 141 | flyway.setLocations(locations); |
142 | 142 | return flyway; |
143 | 143 | } |
144 | 144 |
|
145 | | - private static String[] resolveLocations(String[] locations, DataSource dataSource) { |
146 | | - if (usesVendorLocation(locations)) { |
147 | | - try { |
148 | | - String url = (String) JdbcUtils |
149 | | - .extractDatabaseMetaData(dataSource, "getURL"); |
150 | | - DatabaseDriver vendor = DatabaseDriver.fromJdbcUrl(url); |
151 | | - if (vendor != DatabaseDriver.UNKNOWN) { |
152 | | - for (int i = 0; i < locations.length; i++) { |
153 | | - locations[i] = locations[i].replace(VENDOR_PLACEHOLDER, |
154 | | - vendor.getId()); |
155 | | - } |
156 | | - } |
157 | | - } |
158 | | - catch (MetaDataAccessException ex) { |
159 | | - throw new IllegalStateException(ex); |
160 | | - } |
161 | | - } |
162 | | - return locations; |
163 | | - } |
164 | | - |
165 | | - private static boolean usesVendorLocation(String... locations) { |
166 | | - for (String location : locations) { |
167 | | - if (location.contains(VENDOR_PLACEHOLDER)) { |
168 | | - return true; |
169 | | - } |
170 | | - } |
171 | | - return false; |
172 | | - } |
173 | | - |
174 | 145 | private void checkLocationExists(String... locations) { |
175 | 146 | if (this.properties.isCheckLocation()) { |
176 | 147 | Assert.state(locations.length != 0, |
177 | 148 | "Migration script locations not configured"); |
178 | 149 | boolean exists = hasAtLeastOneLocation(locations); |
179 | 150 | Assert.state(exists, |
180 | | - () -> "Cannot find migrations location in: " + Arrays.asList(locations) |
181 | | - + " (please add migrations or check your Flyway configuration)"); |
| 151 | + () -> "Cannot find migrations location in: " + Arrays.asList( |
| 152 | + locations) |
| 153 | + + " (please add migrations or check your Flyway configuration)"); |
182 | 154 | } |
183 | 155 | } |
184 | 156 |
|
@@ -235,9 +207,64 @@ private static class SpringBootFlyway extends Flyway { |
235 | 207 |
|
236 | 208 | @Override |
237 | 209 | public void setLocations(String... locations) { |
238 | | - locations = FlywayConfiguration.resolveLocations(locations, |
239 | | - getDataSource()); |
240 | | - super.setLocations(locations); |
| 210 | + super.setLocations( |
| 211 | + new LocationResolver(getDataSource()).resolveLocations(locations)); |
| 212 | + } |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + private static class LocationResolver { |
| 217 | + |
| 218 | + private static final String VENDOR_PLACEHOLDER = "{vendor}"; |
| 219 | + |
| 220 | + private final DataSource dataSource; |
| 221 | + |
| 222 | + public LocationResolver(DataSource dataSource) { |
| 223 | + this.dataSource = dataSource; |
| 224 | + } |
| 225 | + |
| 226 | + public String[] resolveLocations(Collection<String> locations) { |
| 227 | + return resolveLocations(locations.toArray(new String[locations.size()])); |
| 228 | + } |
| 229 | + |
| 230 | + public String[] resolveLocations(String[] locations) { |
| 231 | + if (usesVendorLocation(locations)) { |
| 232 | + DatabaseDriver databaseDriver = getDatabaseDriver(); |
| 233 | + return replaceVendorLocations(locations, databaseDriver); |
| 234 | + } |
| 235 | + return locations; |
| 236 | + } |
| 237 | + |
| 238 | + private String[] replaceVendorLocations(String[] locations, |
| 239 | + DatabaseDriver databaseDriver) { |
| 240 | + if (databaseDriver == DatabaseDriver.UNKNOWN) { |
| 241 | + return locations; |
| 242 | + } |
| 243 | + String vendor = databaseDriver.getId(); |
| 244 | + return Arrays.stream(locations) |
| 245 | + .map((location) -> location.replace(VENDOR_PLACEHOLDER, vendor)) |
| 246 | + .toArray(String[]::new); |
| 247 | + } |
| 248 | + |
| 249 | + private DatabaseDriver getDatabaseDriver() { |
| 250 | + try { |
| 251 | + String url = (String) JdbcUtils.extractDatabaseMetaData(this.dataSource, |
| 252 | + "getURL"); |
| 253 | + return DatabaseDriver.fromJdbcUrl(url); |
| 254 | + } |
| 255 | + catch (MetaDataAccessException ex) { |
| 256 | + throw new IllegalStateException(ex); |
| 257 | + } |
| 258 | + |
| 259 | + } |
| 260 | + |
| 261 | + private boolean usesVendorLocation(String... locations) { |
| 262 | + for (String location : locations) { |
| 263 | + if (location.contains(VENDOR_PLACEHOLDER)) { |
| 264 | + return true; |
| 265 | + } |
| 266 | + } |
| 267 | + return false; |
241 | 268 | } |
242 | 269 |
|
243 | 270 | } |
|
0 commit comments