45 lines
3.9 KiB
Java
45 lines
3.9 KiB
Java
import java.nio.charset.StandardCharsets;
|
||
import java.nio.file.*;
|
||
import java.sql.*;
|
||
import java.util.*;
|
||
|
||
/** 仅执行子任务迁移中的两个固定 ALTER;避免放宽公共迁移工具的 DROP 防护。 */
|
||
public class SubtaskMigration {
|
||
/** 校验目标开发库和完整 SQL 白名单,检查元数据后幂等执行;不修改任何业务行。 */
|
||
public static void main(String[] args) throws Exception {
|
||
String addition="ALTER TABLE pm_work_item ADD COLUMN parent_task_id bigint DEFAULT NULL, ADD KEY idx_pm_child(project_id,parent_task_id,deleted), ADD CONSTRAINT fk_pm_parent FOREIGN KEY(project_id,parent_task_id) REFERENCES pm_work_item(project_id,id)";
|
||
String status="ALTER TABLE pm_work_item DROP CHECK ck_pm_item_status, ADD CONSTRAINT ck_pm_item_status CHECK(status IN ('READY','IN_PROGRESS','COMPLETED','CANCELLED','CLOSED'))";
|
||
String sql=Files.readString(Path.of("ruoyi-project/src/main/resources/db/005_subtasks_closed.sql"),StandardCharsets.UTF_8);
|
||
String normalized=sql.replaceAll("(?m)^--.*$","").replaceAll("\\s+"," ").trim();
|
||
if(!normalized.equals(addition+"; "+status+";")) throw new IllegalStateException("迁移内容不符合固定白名单");
|
||
Map<String,String> config=new HashMap<>(); boolean master=false;
|
||
for(String line:Files.readAllLines(Path.of("ruoyi-admin/src/main/resources/application-druid.yml"),StandardCharsets.UTF_8)) {
|
||
if(line.trim().equals("master:")) { master=true; continue; } if(line.trim().equals("slave:")) break;
|
||
int colon=line.indexOf(':'); if(master&&colon>0) config.put(line.substring(0,colon).trim(),value(line.substring(colon+1).trim()));
|
||
}
|
||
String url=config.get("url");
|
||
if(url==null||!url.matches("^jdbc:mysql://192\\.168\\.28\\.10:3306/cmvr-project-dev(?:\\?.*)?$")) throw new IllegalStateException("拒绝访问未授权数据库");
|
||
Properties props=new Properties(); props.setProperty("user",config.get("username")); props.setProperty("password",config.get("password")); props.setProperty("connectTimeout","5000"); props.setProperty("socketTimeout","30000");
|
||
try(Connection connection=DriverManager.getConnection(url,props); Statement st=connection.createStatement()) {
|
||
try(ResultSet rs=st.executeQuery("SELECT DATABASE()")) { rs.next(); if(!"cmvr-project-dev".equals(rs.getString(1))) throw new IllegalStateException("数据库不匹配"); }
|
||
boolean exists; try(ResultSet rs=connection.getMetaData().getColumns("cmvr-project-dev",null,"pm_work_item","parent_task_id")) { exists=rs.next(); }
|
||
if(!exists) st.execute(addition);
|
||
try(ResultSet rs=st.executeQuery("SELECT CHECK_CLAUSE FROM information_schema.CHECK_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=DATABASE() AND CONSTRAINT_NAME='ck_pm_item_status'")) {
|
||
if(!rs.next()) throw new IllegalStateException("原状态约束不存在,停止迁移");
|
||
String check=rs.getString(1); if(!check.contains("READY")||!check.contains("CANCELLED")) throw new IllegalStateException("原状态约束不符合预期:"+check);
|
||
exists=check.contains("CLOSED");
|
||
}
|
||
if(!exists) st.execute(status);
|
||
System.out.println("APPLIED 005_subtasks_closed.sql; existing work items preserved");
|
||
}
|
||
}
|
||
|
||
/** 解析仓库环境占位与 YAML 引号;凭据只用于连接,不写入日志。 */
|
||
private static String value(String text) {
|
||
if(text.startsWith("${")&&text.endsWith("}")) { String inside=text.substring(2,text.length()-1); int colon=inside.indexOf(':'); String found=System.getenv(colon<0?inside:inside.substring(0,colon)); text=found!=null?found:colon<0?"":inside.substring(colon+1); }
|
||
if(text.length()>1&&text.startsWith("'")&&text.endsWith("'")) return text.substring(1,text.length()-1).replace("''","'");
|
||
if(text.length()>1&&text.startsWith("\"")&&text.endsWith("\"")) return text.substring(1,text.length()-1);
|
||
return text;
|
||
}
|
||
}
|