package net.geedge.handler; import cn.hutool.log.Log; import cn.hutool.setting.dialect.Props; import net.geedge.dao.SqlDao; import net.geedge.util.AesUtil; import net.geedge.util.Constant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.List; /** * 此类用于数据同步完成后 * 1. 清除无用表 * 2. 修改 nezha.properties 密码加密 * @author admin * */ @Component @Order(3) public class AfterHandler implements CommandLineRunner { private Log log = Log.get(); @Autowired private SqlDao sqlDao; @Autowired private SqlHandler sqlHandler; @Override public void run(String... args) throws Exception { String dropColumnSql = "ALTER TABLE `monitor_module` DROP COLUMN `type`;" + "ALTER TABLE `monitor_endpoint` DROP COLUMN `configs`;" + "ALTER TABLE `monitor_endpoint` DROP COLUMN `hash`;" + "ALTER TABLE `monitor_endpoint` DROP COLUMN `enabled`;"; sqlDao.execute(dropColumnSql); log.info("remove column successful"); StringBuilder sb = new StringBuilder(); List sqls = sqlDao.allRemoveCopyTables(); for (String sql : sqls) { sb.append(sql); } sqlDao.execute(sb.toString()); log.info("remove table successful"); sqlHandler.dropTable(); log.info("remove useless table successful"); // nezha.properties database.pin // 21.07 -> 22.02 pin 加密 String propsPath = "/opt/nezha/nz-web/config/nezha.properties"; Props props = new Props(propsPath, "UTF-8"); String dbPin = props.getStr("database.pin"); String encryptPin = AesUtil.encrypt(dbPin, Constant.AES_SECRET_KEY); props.put("database.pin", encryptPin); // store props.store(propsPath); log.info("nezha.properties database.pin encrypt successful"); log.info("-----------------transfer data success-----------------------"); } }