matrix.boot.jdbc.config.FlywayAutoConfiguration Maven / Gradle / Ivy
The newest version!
package matrix.boot.jdbc.config;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import matrix.boot.jdbc.beans.MoreDataSource;
import matrix.boot.jdbc.beans.MoreFlyway;
import matrix.boot.jdbc.enums.JdbcInitType;
import matrix.boot.jdbc.enums.ShardingType;
import matrix.boot.jdbc.properties.FlywayProperties;
import matrix.boot.jdbc.properties.JdbcProperties;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.flywaydb.core.api.migration.JavaMigration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* flyway自动装配
*
* @author wangcheng
* 2021/8/16
**/
@EnableConfigurationProperties({JdbcProperties.class, FlywayProperties.class})
@ConditionalOnProperty(value = {"matrix.jdbc.enabled"})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@Slf4j
public class FlywayAutoConfiguration {
/**
* 获取多flyway实例
*
* @param jdbcProperties jdbc配置
* @param beanFactory bean工厂
* @param resourceLoader 资源加载器
* @param fluentConfigurationCustomizers 自定义配置
* @param javaMigrations java迁移器
* @param callbacks 回调
* @return 多flyway实例
*/
@Bean
public MoreFlyway moreFlyway(JdbcProperties jdbcProperties, ConfigurableBeanFactory beanFactory, ResourceLoader resourceLoader, ObjectProvider fluentConfigurationCustomizers, ObjectProvider javaMigrations, ObjectProvider callbacks) {
//定义flywayMap
Map flywayMap = new HashMap<>();
//获取默认的flyway
FlywayProperties defaultFlywayProperties = jdbcProperties.getFlyway();
//获取多数据源实体
MoreDataSource moreDataSource = beanFactory.getBean(MoreDataSource.class);
if (JdbcInitType.ShardingJdbc.equals(JdbcInitType.getByCode(jdbcProperties.getInitType())) && ShardingType.Sharding.equals(ShardingType.getByCode(jdbcProperties.getSharding().getType()))) {
//当初始化类型为shardingJdbc时且为分片逻辑,只执行主flyway的数据
if (defaultFlywayProperties != null && defaultFlywayProperties.isEnabled()) {
Flyway flyway = buildFlyway(defaultFlywayProperties, resourceLoader, beanFactory.getBean(DataSource.class), fluentConfigurationCustomizers, javaMigrations, callbacks);
flywayMap.put(moreDataSource.getMasterDataSourceKey(), flyway);
flyway.migrate();
}
return new MoreFlyway(flywayMap);
}
Map dataSourceMap = moreDataSource.getDataSourceMap();
//数据库列表
jdbcProperties.getDbList().forEach((key, db) -> {
if (!db.isEnabled()) {
return;
}
FlywayProperties flywayProperties = null;
if (db.getFlyway() != null && db.getFlyway().isEnabled()) {
flywayProperties = db.getFlyway();
} else if (defaultFlywayProperties != null && defaultFlywayProperties.isEnabled()) {
flywayProperties = defaultFlywayProperties;
}
if (flywayProperties != null) {
Flyway flyway = buildFlyway(flywayProperties, resourceLoader, dataSourceMap.get(key), fluentConfigurationCustomizers, javaMigrations, callbacks);
flywayMap.put(key, flyway);
flyway.migrate();
}
});
return new MoreFlyway(flywayMap);
}
private Flyway buildFlyway(FlywayProperties flywayProperties, ResourceLoader resourceLoader, DataSource dataSource, ObjectProvider fluentConfigurationCustomizers, ObjectProvider javaMigrations, ObjectProvider callbacks) {
FluentConfiguration configuration = new FluentConfiguration(resourceLoader.getClassLoader());
DataSource dataSourceToMigrate = configureDataSource(configuration, flywayProperties, dataSource, dataSource);
checkLocationExists(dataSourceToMigrate, flywayProperties, resourceLoader);
configureProperties(configuration, flywayProperties);
List orderedCallbacks = callbacks.orderedStream().collect(Collectors.toList());
configureCallbacks(configuration, orderedCallbacks);
fluentConfigurationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configuration));
configureFlywayCallbacks(configuration, orderedCallbacks);
List migrations = javaMigrations.stream().collect(Collectors.toList());
configureJavaMigrations(configuration, migrations);
return configuration.load();
}
private DataSource configureDataSource(FluentConfiguration configuration, FlywayProperties properties, DataSource flywayDataSource, DataSource dataSource) {
if (properties.isCreateDataSource()) {
String url = getProperty(properties::getUrl, properties::getUrl);
String user = getProperty(properties::getUser, properties::getUser);
String password = getProperty(properties::getPassword, properties::getPassword);
configuration.dataSource(url, user, password);
if (!CollectionUtils.isEmpty(properties.getInitSqls())) {
String initSql = StringUtils.collectionToDelimitedString(properties.getInitSqls(), "\n");
configuration.initSql(initSql);
}
} else if (flywayDataSource != null) {
configuration.dataSource(flywayDataSource);
} else {
configuration.dataSource(dataSource);
}
return configuration.getDataSource();
}
private void checkLocationExists(DataSource dataSource, FlywayProperties properties, ResourceLoader resourceLoader) {
if (properties.isCheckLocation()) {
List locations = new LocationResolver(dataSource).resolveLocations(properties.getLocations());
if (!hasAtLeastOneLocation(resourceLoader, locations)) {
throw new FlywayMigrationScriptMissingException(locations);
}
}
}
private void configureProperties(FluentConfiguration configuration, FlywayProperties properties) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
String[] locations = new LocationResolver(configuration.getDataSource()).resolveLocations(properties.getLocations()).toArray(new String[0]);
map.from(locations).to(configuration::locations);
map.from(properties.getEncoding()).to(configuration::encoding);
map.from(properties.getConnectRetries()).to(configuration::connectRetries);
map.from(properties.getSchemas()).as(StringUtils::toStringArray).to(configuration::schemas);
map.from(properties.getTable()).to(configuration::table);
// No method reference for compatibility with Flyway 5.x
map.from(properties.getTablespace()).whenNonNull().to(configuration::tablespace);
map.from(properties.getBaselineDescription()).to(configuration::baselineDescription);
map.from(properties.getBaselineVersion()).to(configuration::baselineVersion);
map.from(properties.getInstalledBy()).to(configuration::installedBy);
map.from(properties.getPlaceholders()).to(configuration::placeholders);
map.from(properties.getPlaceholderPrefix()).to(configuration::placeholderPrefix);
map.from(properties.getPlaceholderSuffix()).to(configuration::placeholderSuffix);
map.from(properties.isPlaceholderReplacement()).to(configuration::placeholderReplacement);
map.from(properties.getSqlMigrationPrefix()).to(configuration::sqlMigrationPrefix);
map.from(properties.getSqlMigrationSuffixes()).as(StringUtils::toStringArray).to(configuration::sqlMigrationSuffixes);
map.from(properties.getSqlMigrationSeparator()).to(configuration::sqlMigrationSeparator);
map.from(properties.getRepeatableSqlMigrationPrefix()).to(configuration::repeatableSqlMigrationPrefix);
map.from(properties.getTarget()).to(configuration::target);
map.from(properties.isBaselineOnMigrate()).to(configuration::baselineOnMigrate);
map.from(properties.isCleanDisabled()).to(configuration::cleanDisabled);
map.from(properties.isCleanOnValidationError()).to(configuration::cleanOnValidationError);
map.from(properties.isGroup()).to(configuration::group);
map.from(properties.isIgnoreMissingMigrations()).to(configuration::ignoreMissingMigrations);
map.from(properties.isIgnoreIgnoredMigrations()).to(configuration::ignoreIgnoredMigrations);
map.from(properties.isIgnorePendingMigrations()).to(configuration::ignorePendingMigrations);
map.from(properties.isIgnoreFutureMigrations()).to(configuration::ignoreFutureMigrations);
map.from(properties.isMixed()).to(configuration::mixed);
map.from(properties.isOutOfOrder()).to(configuration::outOfOrder);
map.from(properties.isSkipDefaultCallbacks()).to(configuration::skipDefaultCallbacks);
map.from(properties.isSkipDefaultResolvers()).to(configuration::skipDefaultResolvers);
map.from(properties.isValidateOnMigrate()).to(configuration::validateOnMigrate);
// Pro properties
map.from(properties.getBatch()).whenNonNull().to(configuration::batch);
map.from(properties.getDryRunOutput()).whenNonNull().to(configuration::dryRunOutput);
map.from(properties.getErrorOverrides()).whenNonNull().to(configuration::errorOverrides);
map.from(properties.getLicenseKey()).whenNonNull().to(configuration::licenseKey);
map.from(properties.getOracleSqlplus()).whenNonNull().to(configuration::oracleSqlplus);
// No method reference for compatibility with Flyway 5.x
map.from(properties.getOracleSqlplusWarn()).whenNonNull().to(configuration::oracleSqlplusWarn);
map.from(properties.getStream()).whenNonNull().to(configuration::stream);
map.from(properties.getUndoSqlMigrationPrefix()).whenNonNull().to(configuration::undoSqlMigrationPrefix);
}
private void configureCallbacks(FluentConfiguration configuration, List callbacks) {
if (!callbacks.isEmpty()) {
configuration.callbacks(callbacks.toArray(new Callback[0]));
}
}
private void configureFlywayCallbacks(FluentConfiguration flyway, List callbacks) {
if (!callbacks.isEmpty()) {
flyway.callbacks(callbacks.toArray(new Callback[0]));
}
}
private void configureJavaMigrations(FluentConfiguration flyway, List migrations) {
if (!migrations.isEmpty()) {
try {
flyway.javaMigrations(migrations.toArray(new JavaMigration[0]));
} catch (NoSuchMethodError ex) {
// Flyway 5.x
}
}
}
private String getProperty(Supplier property, Supplier defaultValue) {
String value = property.get();
return (value != null) ? value : defaultValue.get();
}
private boolean hasAtLeastOneLocation(ResourceLoader resourceLoader, Collection locations) {
for (String location : locations) {
if (resourceLoader.getResource(normalizePrefix(location)).exists()) {
return true;
}
}
return false;
}
private String normalizePrefix(String location) {
return location.replace("filesystem:", "file:");
}
private static class LocationResolver {
private static final String VENDOR_PLACEHOLDER = "{vendor}";
private final DataSource dataSource;
LocationResolver(DataSource dataSource) {
this.dataSource = dataSource;
}
List resolveLocations(List locations) {
if (usesVendorLocation(locations)) {
DatabaseDriver databaseDriver = getDatabaseDriver();
return replaceVendorLocations(locations, databaseDriver);
}
return locations;
}
private List replaceVendorLocations(List locations, DatabaseDriver databaseDriver) {
if (databaseDriver == DatabaseDriver.UNKNOWN) {
return locations;
}
String vendor = databaseDriver.getId();
return locations.stream().map((location) -> location.replace(VENDOR_PLACEHOLDER, vendor)).collect(Collectors.toList());
}
private DatabaseDriver getDatabaseDriver() {
try {
String metaDataMethodName = "getURL";
String url = (String) JdbcUtils.extractDatabaseMetaData(this.dataSource, metaData -> {
try {
return DatabaseMetaData.class.getMethod(metaDataMethodName).invoke(metaData);
} catch (NoSuchMethodException ex) {
throw new MetaDataAccessException("No method named '" + metaDataMethodName + "' found on DatabaseMetaData instance [" + metaData + "]", ex);
} catch (IllegalAccessException ex) {
throw new MetaDataAccessException("Could not access DatabaseMetaData method '" + metaDataMethodName + "'", ex);
} catch (InvocationTargetException ex) {
if (ex.getTargetException() instanceof SQLException) {
throw (SQLException) ex.getTargetException();
}
throw new MetaDataAccessException("Invocation of DatabaseMetaData method '" + metaDataMethodName + "' failed", ex);
}
});
return DatabaseDriver.fromJdbcUrl(url);
} catch (MetaDataAccessException ex) {
throw new IllegalStateException(ex);
}
}
private boolean usesVendorLocation(Collection locations) {
for (String location : locations) {
if (location.contains(VENDOR_PLACEHOLDER)) {
return true;
}
}
return false;
}
}
@Getter
public static class FlywayMigrationScriptMissingException extends RuntimeException {
private final List locations;
FlywayMigrationScriptMissingException(List locations) {
super(locations.isEmpty() ? "Migration script locations not configured" : "Cannot find migration scripts in: " + locations + " (please add migration scripts or check your Flyway configuration)");
this.locations = new ArrayList<>(locations);
}
}
}