feat:模块名称调整

This commit is contained in:
曾文豪
2023-01-11 11:21:01 +08:00
parent 61a4a6494a
commit c721e4877f
125 changed files with 56 additions and 56 deletions

View File

@@ -0,0 +1,12 @@
package com.tiesheng.migration;
import org.springframework.context.annotation.ComponentScan;
/**
* @author hao
*/
@ComponentScan({
"com.tiesheng.migration.**.*"
})
public class MigrationAutoConfigurer {
}

View File

@@ -0,0 +1,75 @@
package com.tiesheng.migration.config;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.sql.SQLException;
import java.util.List;
/**
* 数据库表版本对比
*
* @author hao
*/
@Configuration
@ConfigurationProperties(prefix = "tiesheng.db-migration")
public class DbMigrationConfig {
private String table = "core_db_migration";
private List<String> locations = CollUtil.newArrayList("classpath*:db/migration/*.sql");
private String ignoreSqls = "drop,delete";
/**
* 检查数据库是否存在
*
* @param db
* @throws SQLException
*/
public void checkDbExists(Db db) throws SQLException {
try {
db.count(Entity.create(getTable()));
} catch (SQLException ignored) {
db.execute("CREATE TABLE `" + getTable() + "` (\n" +
" `id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,\n" +
" `create_time` datetime(0) NOT NULL,\n" +
" `update_time` datetime(0) NOT NULL,\n" +
" `is_deleted` int(6) NOT NULL DEFAULT 0,\n" +
" `file` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '文件名称',\n" +
" `checksum` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '文件checksum',\n" +
" PRIMARY KEY (`id`) USING BTREE\n" +
") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'db-合并' ROW_FORMAT = Dynamic;");
}
}
///////////////////////////////////////////////////////////////////////////
// setter\getter
///////////////////////////////////////////////////////////////////////////
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public List<String> getLocations() {
return locations;
}
public void setLocations(List<String> locations) {
this.locations = locations;
}
public String getIgnoreSqls() {
return ignoreSqls;
}
public void setIgnoreSqls(String ignoreSqls) {
this.ignoreSqls = ignoreSqls;
}
}

View File

@@ -0,0 +1,137 @@
package com.tiesheng.migration.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.func.VoidFunc1;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.tiesheng.migration.config.DbMigrationConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.zip.Checksum;
/**
* 数据库表版本对比
*
* @author hao
*/
@Component
public class DbMigrationInitializer implements ServletContextInitializer {
@Autowired
DataSource dataSource;
@Autowired
DbMigrationConfig dbMigrationConfig;
@Override
public void onStartup(ServletContext servletContext) {
try {
startDeal();
} catch (Exception ignore) {
}
}
/**
* 开始处理数据
*
* @throws Exception
*/
private void startDeal() throws Exception {
Db coreDb = Db.use(dataSource);
dbMigrationConfig.checkDbExists(coreDb);
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
for (String location : dbMigrationConfig.getLocations()) {
Resource[] resources = patternResolver.getResources(location);
if (ArrayUtil.isEmpty(resources)) {
return;
}
for (Resource resource : resources) {
migrationByResource(resource, coreDb);
}
}
}
/**
* 根据资源合并数据库结构
*
* @param resource
* @param db
* @throws Exception
*/
private void migrationByResource(Resource resource, Db db) throws Exception {
InputStream inputStream = resource.getInputStream();
String readUtf8 = IoUtil.readUtf8(inputStream);
String filename = resource.getFilename();
Checksum checksum = IoUtil.checksum(resource.getInputStream(), null);
Entity entity = fileChecksum(db, filename, checksum.getValue());
if (entity == null) {
return;
}
db.tx((VoidFunc1<Db>) parameter -> {
List<String> split = StrUtil.split(readUtf8, ";");
for (String sql : split) {
sql = sql.trim();
if (StrUtil.isEmpty(sql)) {
continue;
}
if (StrUtil.startWithAnyIgnoreCase(sql, StrUtil.splitToArray(dbMigrationConfig.getIgnoreSqls(), ","))) {
continue;
}
try {
parameter.execute(sql);
} catch (Exception ignore) {
}
}
});
entity.set("checksum", checksum.getValue());
entity.set("update_time", DateUtil.date());
db.update(entity, Entity.create(dbMigrationConfig.getTable()).set("id", entity.get("id")));
}
/**
* 检查文件是否执行
*
* @param db
* @param fileName
* @param checksum
*/
private Entity fileChecksum(Db db, String fileName, long checksum) throws SQLException {
Entity entity = db.get(dbMigrationConfig.getTable(), "file", fileName);
if (entity == null) {
entity = new Entity(dbMigrationConfig.getTable());
entity.set("id", IdUtil.getSnowflakeNextId());
entity.set("create_time", DateUtil.date());
entity.set("update_time", DateUtil.date());
entity.set("is_deleted", 0);
entity.set("file", fileName);
entity.set("checksum", 0);
db.insert(entity);
}
if (entity.getLong("checksum") != checksum) {
return entity;
}
return null;
}
}