mirror of https://gitee.com/stylefeng/roses
【7.0.2】更新flyway初始化过程,数据库驱动改为配置文件配置 I3BY48
parent
98ecac17c2
commit
f53ed687a6
|
@ -24,6 +24,7 @@
|
||||||
*/
|
*/
|
||||||
package cn.stylefeng.roses.kernel.db.api.exception;
|
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.db.api.constants.DbConstants;
|
||||||
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
|
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
|
||||||
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
||||||
|
@ -40,4 +41,8 @@ public class DaoException extends ServiceException {
|
||||||
super(DbConstants.DB_MODULE_NAME, exception);
|
super(DbConstants.DB_MODULE_NAME, exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DaoException(AbstractExceptionEnum exception, Object... params) {
|
||||||
|
super(DbConstants.DB_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,12 +41,12 @@ public enum FlywayExceptionEnum implements AbstractExceptionEnum {
|
||||||
/**
|
/**
|
||||||
* 获取不到application.yml中的数据库配置
|
* 获取不到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执行迁移异常
|
||||||
*/
|
*/
|
||||||
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执行迁移异常,具体原因:{}");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 错误编码
|
* 错误编码
|
||||||
|
|
|
@ -59,6 +59,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
|
||||||
String dataSourceUrl = environment.getProperty("spring.datasource.url");
|
String dataSourceUrl = environment.getProperty("spring.datasource.url");
|
||||||
String dataSourceUsername = environment.getProperty("spring.datasource.username");
|
String dataSourceUsername = environment.getProperty("spring.datasource.username");
|
||||||
String dataSourcePassword = environment.getProperty("spring.datasource.password");
|
String dataSourcePassword = environment.getProperty("spring.datasource.password");
|
||||||
|
String driverClassName = environment.getProperty("spring.datasource.driver-class-name");
|
||||||
|
|
||||||
// flyway的配置
|
// flyway的配置
|
||||||
String enabledStr = environment.getProperty("spring.flyway.enabled");
|
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);
|
throw new DaoException(FlywayExceptionEnum.DB_CONFIG_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +105,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
|
||||||
assert dataSourceUrl != null;
|
assert dataSourceUrl != null;
|
||||||
// 手动创建数据源
|
// 手动创建数据源
|
||||||
dmDataSource = new DriverManagerDataSource();
|
dmDataSource = new DriverManagerDataSource();
|
||||||
dmDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
dmDataSource.setDriverClassName(driverClassName);
|
||||||
dmDataSource.setUrl(dataSourceUrl);
|
dmDataSource.setUrl(dataSourceUrl);
|
||||||
dmDataSource.setUsername(dataSourceUsername);
|
dmDataSource.setUsername(dataSourceUsername);
|
||||||
dmDataSource.setPassword(dataSourcePassword);
|
dmDataSource.setPassword(dataSourcePassword);
|
||||||
|
@ -128,7 +129,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("flyway初始化失败", e);
|
log.error("flyway初始化失败", e);
|
||||||
throw new DaoException(FlywayExceptionEnum.FLYWAY_MIGRATE_ERROR);
|
throw new DaoException(FlywayExceptionEnum.FLYWAY_MIGRATE_ERROR, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue