feat(database): 增加db/data目录,存放初始化数据的sql
This commit is contained in:
@@ -20,7 +20,8 @@ import java.util.List;
|
||||
public class DbMigrationConfig {
|
||||
|
||||
private String table = "core_config_db";
|
||||
private List<String> locations = CollUtil.newArrayList("classpath*:db/migration/*.sql");
|
||||
private List<String> migrations = CollUtil.newArrayList("classpath*:db/migration/*.sql");
|
||||
private List<String> datas = CollUtil.newArrayList("classpath*:db/data/*.sql");
|
||||
private String ignoreSqls = "drop table,delete from";
|
||||
|
||||
/**
|
||||
@@ -57,12 +58,20 @@ public class DbMigrationConfig {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public List<String> getLocations() {
|
||||
return locations;
|
||||
public List<String> getMigrations() {
|
||||
return migrations;
|
||||
}
|
||||
|
||||
public void setLocations(List<String> locations) {
|
||||
this.locations = locations;
|
||||
public void setMigrations(List<String> migrations) {
|
||||
this.migrations = migrations;
|
||||
}
|
||||
|
||||
public List<String> getDatas() {
|
||||
return datas;
|
||||
}
|
||||
|
||||
public void setDatas(List<String> datas) {
|
||||
this.datas = datas;
|
||||
}
|
||||
|
||||
public String getIgnoreSqls() {
|
||||
|
||||
@@ -8,7 +8,6 @@ import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.Db;
|
||||
import cn.hutool.db.Entity;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.database.config.DbMigrationConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
@@ -45,7 +44,14 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) {
|
||||
try {
|
||||
startDeal();
|
||||
|
||||
// 初始化
|
||||
Db coreDb = Db.use(dataSource);
|
||||
dbMigrationConfig.checkDbExists(coreDb);
|
||||
|
||||
startDeal(coreDb, dbMigrationConfig.getMigrations());
|
||||
startDeal(coreDb, dbMigrationConfig.getDatas());
|
||||
|
||||
if (dbDataInitializerList != null) {
|
||||
dbDataInitializerList.forEach(DbDataInitializer::init);
|
||||
}
|
||||
@@ -59,18 +65,13 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private void startDeal() throws Exception {
|
||||
Db coreDb = Db.use(dataSource);
|
||||
dbMigrationConfig.checkDbExists(coreDb);
|
||||
|
||||
private void startDeal(Db coreDb, List<String> locations) throws Exception {
|
||||
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
|
||||
List<Resource> resourceList = new ArrayList<>();
|
||||
for (String location : dbMigrationConfig.getLocations()) {
|
||||
for (String location : locations) {
|
||||
Resource[] resources = patternResolver.getResources(location);
|
||||
resourceList.addAll(Arrays.asList(resources));
|
||||
}
|
||||
|
||||
// 排序后执行sql
|
||||
resourceList.sort((o1, o2) -> StrUtil.compare(o1.getFilename(), o2.getFilename(), true));
|
||||
for (Resource resource : resourceList) {
|
||||
migrationByResource(resource, coreDb);
|
||||
@@ -94,8 +95,8 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
AtomicReference<Integer> success= new AtomicReference<>(0);
|
||||
AtomicReference<Integer> fail= new AtomicReference<>(0);
|
||||
AtomicReference<Integer> success = new AtomicReference<>(0);
|
||||
AtomicReference<Integer> fail = new AtomicReference<>(0);
|
||||
db.tx((VoidFunc1<Db>) parameter -> {
|
||||
List<String> split = StrUtil.split(readUtf8, ";");
|
||||
for (String sql : split) {
|
||||
@@ -114,7 +115,6 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
}
|
||||
}
|
||||
});
|
||||
LogFactory.get().info("执行sql文件{},成功数:{},失败数:{}。",filename,success.get(),fail.get());
|
||||
entity.set("checksum", checksum.getValue());
|
||||
entity.set("update_time", DateUtil.date());
|
||||
db.update(entity, Entity.create(dbMigrationConfig.getTable()).set("id", entity.get("id")));
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
-- ----------------------------
|
||||
-- Table for data
|
||||
-- ----------------------------
|
||||
|
||||
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
|
||||
`config_type`, `remark`, `extra`, `read_only`)
|
||||
VALUES ('manager_web_copyright', '2022-02-23 16:52:48', '2022-02-23 16:52:49', 0, 'manager_web_copyright',
|
||||
'杭州铁晟提供技术支持', 0, '网站底部版权信息', '', 0);
|
||||
|
||||
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
|
||||
`config_type`, `remark`, `extra`, `read_only`)
|
||||
VALUES ('manager_web_title', '2022-02-24 11:56:53', '2022-02-24 11:56:53', 0, 'manager_web_title', '网站名称', 0,
|
||||
'网站名称', '', 0);
|
||||
|
||||
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
|
||||
`config_type`, `remark`, `extra`, `read_only`)
|
||||
VALUES ('manager_web_logo', '2022-02-24 11:56:53', '2022-02-24 11:56:53', 0, 'manager_web_logo', '', 1,
|
||||
'网站LOGO', '', 0);
|
||||
@@ -50,23 +50,4 @@ CREATE TABLE `core_config_func`
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8mb4 COMMENT ='职位-功能点';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table for data
|
||||
-- ----------------------------
|
||||
|
||||
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
|
||||
`config_type`, `remark`, `extra`, `read_only`)
|
||||
VALUES ('manager_web_copyright', '2022-02-23 16:52:48', '2022-02-23 16:52:49', 0, 'manager_web_copyright',
|
||||
'杭州铁晟提供技术支持', 0, '网站底部版权信息', '', 0);
|
||||
|
||||
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
|
||||
`config_type`, `remark`, `extra`, `read_only`)
|
||||
VALUES ('manager_web_title', '2022-02-24 11:56:53', '2022-02-24 11:56:53', 0, 'manager_web_title', '网站名称', 0,
|
||||
'网站名称', '', 0);
|
||||
|
||||
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
|
||||
`config_type`, `remark`, `extra`, `read_only`)
|
||||
VALUES ('manager_web_logo', '2022-02-24 11:56:53', '2022-02-24 11:56:53', 0, 'manager_web_logo', '', 1,
|
||||
'网站LOGO', '', 0);
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
|
||||
Reference in New Issue
Block a user