【7.0.2】更新flyway初始化过程,数据库驱动改为配置文件配置 I3BY48

pull/5/head
fengshuonan 2021-03-21 21:49:00 +08:00
parent 98ecac17c2
commit f53ed687a6
3 changed files with 11 additions and 5 deletions

View File

@ -24,6 +24,7 @@
*/
package cn.stylefeng.roses.kernel.db.api.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.constants.DbConstants;
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
@ -40,4 +41,8 @@ public class DaoException extends ServiceException {
super(DbConstants.DB_MODULE_NAME, exception);
}
public DaoException(AbstractExceptionEnum exception, Object... params) {
super(DbConstants.DB_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
}
}

View File

@ -41,12 +41,12 @@ public enum FlywayExceptionEnum implements AbstractExceptionEnum {
/**
* application.yml
*/
DB_CONFIG_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "01", "获取不到application.yml中的数据库配置无法为flyway创建数据库链接"),
DB_CONFIG_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "01", "获取不到application.yml中的数据库配置无法为flyway创建数据库链接请检查spring.datasource配置"),
/**
* flyway
*/
FLYWAY_MIGRATE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "02", "脚本错误flyway执行迁移异常");
FLYWAY_MIGRATE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "02", "脚本错误flyway执行迁移异常,具体原因:{}");
/**
*

View File

@ -59,6 +59,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
String dataSourceUrl = environment.getProperty("spring.datasource.url");
String dataSourceUsername = environment.getProperty("spring.datasource.username");
String dataSourcePassword = environment.getProperty("spring.datasource.password");
String driverClassName = environment.getProperty("spring.datasource.driver-class-name");
// flyway的配置
String enabledStr = environment.getProperty("spring.flyway.enabled");
@ -78,7 +79,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
}
// 如果有为空的配置,终止执行
if (ObjectUtil.hasEmpty(dataSourceUrl, dataSourceUsername, dataSourcePassword)) {
if (ObjectUtil.hasEmpty(dataSourceUrl, dataSourceUsername, dataSourcePassword, driverClassName)) {
throw new DaoException(FlywayExceptionEnum.DB_CONFIG_ERROR);
}
@ -104,7 +105,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
assert dataSourceUrl != null;
// 手动创建数据源
dmDataSource = new DriverManagerDataSource();
dmDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dmDataSource.setDriverClassName(driverClassName);
dmDataSource.setUrl(dataSourceUrl);
dmDataSource.setUsername(dataSourceUsername);
dmDataSource.setPassword(dataSourcePassword);
@ -128,7 +129,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
} catch (Exception e) {
log.error("flyway初始化失败", e);
throw new DaoException(FlywayExceptionEnum.FLYWAY_MIGRATE_ERROR);
throw new DaoException(FlywayExceptionEnum.FLYWAY_MIGRATE_ERROR, e.getMessage());
}
}