| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /** Apply only CREATE TABLE IF NOT EXISTS blocks from lobster schema sync (skip ALTER on missing tables). */
- public class ApplyLobsterCreateTablesOnly {
- private static final String URL = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/ylrz_saas"
- + "?useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
- private static final String USER = "root";
- private static final String PWD = "Ylrz_1q2w3e4r5t6y";
- public static void main(String[] args) throws Exception {
- String sqlFile = "d:/ylrz_saas_new/java/fs-service/src/main/resources/db/tenant-initTable-migration-lobster-schema-sync.sql";
- String content = Files.readString(Path.of(sqlFile), StandardCharsets.UTF_8);
- List<String> creates = extractCreateTableStatements(content);
- System.out.println("Found " + creates.size() + " CREATE TABLE statements");
- try (Connection c = DriverManager.getConnection(URL, USER, PWD);
- Statement st = c.createStatement()) {
- for (String ddl : creates) {
- String name = ddl.replaceAll("(?s).*CREATE TABLE IF NOT EXISTS `([^`]+)`.*", "$1");
- try {
- st.execute(ddl);
- System.out.println("OK " + name);
- } catch (Exception e) {
- System.out.println("FAIL " + name + ": " + e.getMessage());
- }
- }
- st.execute("CREATE TABLE IF NOT EXISTS lobster_tool_config ("
- + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
- + "company_id BIGINT NOT NULL DEFAULT 0,"
- + "tool_name VARCHAR(128) NOT NULL,"
- + "tool_type VARCHAR(64) DEFAULT NULL,"
- + "description VARCHAR(500) DEFAULT NULL,"
- + "config_json TEXT,"
- + "enabled TINYINT NOT NULL DEFAULT 1,"
- + "deleted TINYINT NOT NULL DEFAULT 0,"
- + "create_time DATETIME DEFAULT CURRENT_TIMESTAMP,"
- + "UNIQUE KEY uk_company_tool (company_id, tool_name)"
- + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
- System.out.println("OK lobster_tool_config (explicit)");
- }
- }
- private static List<String> extractCreateTableStatements(String content) {
- List<String> out = new ArrayList<>();
- Pattern p = Pattern.compile("CREATE TABLE IF NOT EXISTS `[^`]+` \\([\\s\\S]*?\\) ENGINE=InnoDB[^;]*;", Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(content);
- while (m.find()) {
- out.add(m.group());
- }
- return out;
- }
- }
|