feat:增加操作日志
This commit is contained in:
@@ -2,9 +2,12 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -16,10 +19,32 @@ import java.util.List;
|
||||
@ConfigurationProperties(prefix = "tiesheng.db-migration")
|
||||
public class DbMigrationConfig {
|
||||
|
||||
private String table = "ts_db_migration";
|
||||
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
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -9,7 +9,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.migration.config.DbMigrationConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
||||
@@ -54,7 +53,7 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
*/
|
||||
private void startDeal() throws Exception {
|
||||
Db coreDb = Db.use(dataSource);
|
||||
checkDbMigration(coreDb);
|
||||
dbMigrationConfig.checkDbExists(coreDb);
|
||||
|
||||
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
|
||||
for (String location : dbMigrationConfig.getLocations()) {
|
||||
@@ -95,7 +94,10 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
if (StrUtil.startWithAnyIgnoreCase(sql, StrUtil.splitToArray(dbMigrationConfig.getIgnoreSqls(), ","))) {
|
||||
continue;
|
||||
}
|
||||
parameter.execute(sql);
|
||||
try {
|
||||
parameter.execute(sql);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -132,26 +134,4 @@ public class DbMigrationInitializer implements ServletContextInitializer {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查数据库是否存在
|
||||
*
|
||||
* @param db
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void checkDbMigration(Db db) throws SQLException {
|
||||
try {
|
||||
db.count(Entity.create(dbMigrationConfig.getTable()));
|
||||
} catch (SQLException ignored) {
|
||||
db.execute("CREATE TABLE `" + dbMigrationConfig.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;");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user