feat(database): 增加db/data目录,存放初始化数据的sql
This commit is contained in:
@@ -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")));
|
||||
|
||||
Reference in New Issue
Block a user