|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.flyway; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.annotation.PostConstruct; |
| 23 | +import javax.sql.DataSource; |
| 24 | + |
| 25 | +import org.flywaydb.core.Flyway; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.ObjectProvider; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
| 29 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 31 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 32 | +import org.springframework.boot.jdbc.DatabaseDriver; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.core.io.ResourceLoader; |
| 35 | +import org.springframework.jdbc.support.JdbcUtils; |
| 36 | +import org.springframework.jdbc.support.MetaDataAccessException; |
| 37 | +import org.springframework.util.Assert; |
| 38 | + |
| 39 | +/** |
| 40 | + * {@link EnableAutoConfiguration Auto configuration} to validate flyway locations on |
| 41 | + * startup. |
| 42 | + * |
| 43 | + * @author Eddú Meléndez |
| 44 | + */ |
| 45 | +@Configuration |
| 46 | +@AutoConfigureBefore(FlywayAutoConfiguration.class) |
| 47 | +@ConditionalOnProperty(prefix = "flyway", name = "check-location", havingValue = "true") |
| 48 | +@EnableConfigurationProperties(FlywayProperties.class) |
| 49 | +public class FlywayValidatorAutoConfiguration { |
| 50 | + |
| 51 | + private static final String VENDOR_PLACEHOLDER = "{vendor}"; |
| 52 | + |
| 53 | + private final FlywayProperties properties; |
| 54 | + |
| 55 | + private final ResourceLoader resourceLoader; |
| 56 | + |
| 57 | + private final DataSource dataSource; |
| 58 | + |
| 59 | + private final DataSource flywayDataSource; |
| 60 | + |
| 61 | + public FlywayValidatorAutoConfiguration(FlywayProperties properties, |
| 62 | + ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource, |
| 63 | + @FlywayDataSource ObjectProvider<DataSource> flywayDataSource) { |
| 64 | + this.properties = properties; |
| 65 | + this.resourceLoader = resourceLoader; |
| 66 | + this.dataSource = dataSource.getIfAvailable(); |
| 67 | + this.flywayDataSource = flywayDataSource.getIfAvailable(); |
| 68 | + } |
| 69 | + |
| 70 | + @PostConstruct |
| 71 | + public void checkLocationExists() { |
| 72 | + Flyway flyway = new Flyway(); |
| 73 | + if (this.properties.isCreateDataSource()) { |
| 74 | + flyway.setDataSource(this.properties.getUrl(), this.properties.getUser(), |
| 75 | + this.properties.getPassword(), |
| 76 | + this.properties.getInitSqls().toArray(new String[0])); |
| 77 | + } |
| 78 | + else if (this.flywayDataSource != null) { |
| 79 | + flyway.setDataSource(this.flywayDataSource); |
| 80 | + } |
| 81 | + else { |
| 82 | + flyway.setDataSource(this.dataSource); |
| 83 | + } |
| 84 | + |
| 85 | + List<String> locations = new ArrayList<String>(); |
| 86 | + try { |
| 87 | + String url = (String) JdbcUtils.extractDatabaseMetaData( |
| 88 | + flyway.getDataSource(), "getURL"); |
| 89 | + DatabaseDriver vendor = DatabaseDriver.fromJdbcUrl(url); |
| 90 | + if (vendor != DatabaseDriver.UNKNOWN) { |
| 91 | + for (int i = 0; i < this.properties.getLocations().size(); i++) { |
| 92 | + locations.add(this.properties.getLocations().get(i) |
| 93 | + .replace(VENDOR_PLACEHOLDER, vendor.getId())); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + catch (MetaDataAccessException ex) { |
| 98 | + throw new IllegalStateException(ex); |
| 99 | + } |
| 100 | + |
| 101 | + Assert.state(!(locations.isEmpty()), "Migration script locations not configured"); |
| 102 | + boolean exists = hasAtLeastOneLocation(locations); |
| 103 | + Assert.state(exists, "Cannot find migrations location in: " + locations |
| 104 | + + " (please add migrations or check your Flyway configuration)"); |
| 105 | + } |
| 106 | + |
| 107 | + private boolean hasAtLeastOneLocation(List<String> locations) { |
| 108 | + for (String location : locations) { |
| 109 | + if (this.resourceLoader.getResource(location).exists()) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments