Commit bf27c9e0 authored by 黎博's avatar 黎博

新增同步表结构接口

parent 61aed89a
...@@ -5,6 +5,7 @@ import cn.qg.holmes.service.effect.DatabaseSyncService; ...@@ -5,6 +5,7 @@ import cn.qg.holmes.service.effect.DatabaseSyncService;
import cn.qg.holmes.service.k8s.K8sService; import cn.qg.holmes.service.k8s.K8sService;
import cn.qg.holmes.utils.JenkinsService; import cn.qg.holmes.utils.JenkinsService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.JdbcNamedParameter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -116,6 +117,26 @@ public class DbSyncController { ...@@ -116,6 +117,26 @@ public class DbSyncController {
return StringUtils.join(tableList, " "); return StringUtils.join(tableList, " ");
} }
/**
* 同步表结构
* @param ip
* @param port
* @param database
* @param table
* @return
*/
@GetMapping("/table/structure")
public JsonResult handleTableStructure(@RequestParam String ip, @RequestParam String port, @RequestParam String database, @RequestParam String table) {
try {
databaseSyncService.getSourceDbStructure(ip, port, username, password, database, table);
databaseSyncService.syncDbStructureToDest(ip, port, "qa", "qatest", database, table);
} catch (Exception e) {
e.printStackTrace();
return JsonResult.buildErrorStateResult("创建表结构失败!", false);
}
return JsonResult.buildSuccessResult("创建" + ip + ":" + port + "环境表结构成功!");
}
/** /**
* 老数据库同步,调用Jenkins job sync_database_schema * 老数据库同步,调用Jenkins job sync_database_schema
* @param dbName 数据库名 * @param dbName 数据库名
......
...@@ -15,4 +15,10 @@ public interface DatabaseSyncService { ...@@ -15,4 +15,10 @@ public interface DatabaseSyncService {
List<String> getDatabaseList(String ip, String port, String username, String password); List<String> getDatabaseList(String ip, String port, String username, String password);
List<String> getTableListByDb(String ip, String port, String username, String password, String dbName); List<String> getTableListByDb(String ip, String port, String username, String password, String dbName);
// 从同步库获取表结构
boolean getSourceDbStructure(String ip, String port, String username, String password, String dbName, String tableName);
// 将同步库的表结构同步到环境
boolean syncDbStructureToDest(String ip, String port, String username, String password, String dbName, String tableName);
} }
\ No newline at end of file
...@@ -651,6 +651,150 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService { ...@@ -651,6 +651,150 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
} }
} }
/**
* 从同步库获取表结构
* @param ip
* @param port
* @param username
* @param password
* @param dbName
* @param tableName
* @return
*/
@Override
public boolean getSourceDbStructure(String ip, String port, String username, String password, String dbName, String tableName) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName;
Connection connection = null;
Statement statement = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(true);
if ("all".equals(tableName)) {
String tableListKey = dbSyncPrefix + dbName + ":tableList";
List<String> tableList = getTableListByDb(ip, port, username, password, dbName);
redisUtils.set(tableListKey, StringUtils.join(tableList, " "), 600);
for (String table: tableList) {
String tableRedisKey = dbSyncPrefix + dbName + ":" + table + ":create";
if (!redisUtils.hasKey(tableRedisKey)) {
String sql = String.format("SHOW CREATE TABLE %s", table);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
redisUtils.set(tableRedisKey, resultSet.getString(2), 600);
}
resultSet.close();
statement.clearBatch();
}
}
} else {
String tableRedisKey = dbSyncPrefix + dbName + ":" + tableName + ":create";
if (!redisUtils.hasKey(tableRedisKey)) {
String sql = String.format("SHOW CREATE TABLE %s", tableName);
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
redisUtils.set(tableRedisKey, resultSet.getString(2), 600);
}
resultSet.close();
}
statement.clearBatch();
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* 将同步库的表结构同步到环境中
* @param ip
* @param port
* @param username
* @param password
* @param dbName
* @param tableName
* @return
*/
@Override
public boolean syncDbStructureToDest(String ip, String port, String username, String password, String dbName, String tableName) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName;
Connection connection = null;
Statement statement = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(true);
statement = connection.createStatement();
if ("all".equals(tableName)) {
String tableListKey = dbSyncPrefix + dbName + ":tableList";
// List<String> tableList = Arrays.asList(redisUtils.get(tableListKey).toString().split(" "));
// 删除key
// redisUtils.del(tableListKey);
List<String> tableList = getTableListByDb(ip, port, username, password, dbName);
for (String table: tableList) {
// 首先删除表
String dropTableSql = "DROP TABLE IF EXISTS " + table;
statement.execute(dropTableSql);
statement.clearBatch();
// 然后同步表结构
String tableRedisKey = dbSyncPrefix + dbName + ":" + table + ":create";
String createTableSql = redisUtils.get(tableRedisKey).toString();
statement.execute(createTableSql);
statement.clearBatch();
}
} else {
// 首先删除表
String dropTableSql = "DROP TABLE IF EXISTS" + tableName;
statement.execute(dropTableSql);
// 然后同步表结构
String tableRedisKey = dbSyncPrefix + dbName + ":" + tableName + ":create";
String createTableSql = redisUtils.get(tableRedisKey).toString();
statement.execute(createTableSql);
statement.clearBatch();
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/** /**
* 获取表数据一行的所有值 * 获取表数据一行的所有值
* @param rs * @param rs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment