102 lines
6.8 KiB
Java
102 lines
6.8 KiB
Java
|
|
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.PreparedStatement;
|
|||
|
|
import java.sql.ResultSet;
|
|||
|
|
import java.sql.Statement;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.HashMap;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Map;
|
|||
|
|
import java.util.Properties;
|
|||
|
|
import java.util.regex.Matcher;
|
|||
|
|
import java.util.regex.Pattern;
|
|||
|
|
|
|||
|
|
/** 008 协作迁移执行器;只允许仓库配置的开发库,重复运行或 DDL 中断后可安全续跑。 */
|
|||
|
|
public class StageRevisionMigration {
|
|||
|
|
private static final String DATABASE="cmvr-project-dev";
|
|||
|
|
private static final Path SCRIPT=Path.of("ruoyi-project/src/main/resources/db/008_stage_revisions.sql");
|
|||
|
|
|
|||
|
|
/** 严格检查目标库并顺序执行本模块迁移,不读取命令行任意 SQL 路径。 */
|
|||
|
|
public static void main(String[] args) throws Exception {
|
|||
|
|
if(args.length!=0) throw new IllegalArgumentException("本执行器不接受外部 SQL 路径");
|
|||
|
|
Map<String,String> config=config(); String url=config.get("url");
|
|||
|
|
if(url==null||!url.matches("^jdbc:mysql://192\\.168\\.28\\.10:3306/"+DATABASE+"(?:\\?.*)?$")) throw new IllegalStateException("只允许指定开发库");
|
|||
|
|
Properties properties=new Properties(); properties.setProperty("user",config.get("username")); properties.setProperty("password",config.get("password"));
|
|||
|
|
properties.setProperty("connectTimeout","5000"); properties.setProperty("socketTimeout","30000");
|
|||
|
|
try(Connection connection=DriverManager.getConnection(url,properties)) {
|
|||
|
|
if(!DATABASE.equals(connection.getCatalog())) throw new IllegalStateException("当前数据库不符");
|
|||
|
|
if(applied(connection)) { verify(connection); System.out.println("008 已应用;结构核验通过"); return; }
|
|||
|
|
String sql=Files.readString(SCRIPT,StandardCharsets.UTF_8);
|
|||
|
|
if(!sql.startsWith("-- target-database: "+DATABASE)) throw new IllegalStateException("迁移文件缺少数据库白名单标记");
|
|||
|
|
for(String statement:statements(sql)) {
|
|||
|
|
if(skipExistingDdl(connection,statement)) continue;
|
|||
|
|
try(Statement command=connection.createStatement()) { command.execute(statement); }
|
|||
|
|
}
|
|||
|
|
verify(connection); if(!applied(connection)) throw new IllegalStateException("迁移标记没有写入");
|
|||
|
|
System.out.println("008 阶段版本迁移已应用并核验");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 配置只从现有主数据源读取,密码不输出也不写到协作模块。 */
|
|||
|
|
private static Map<String,String> config() throws Exception {
|
|||
|
|
Map<String,String> values=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;
|
|||
|
|
if(master) { int colon=line.indexOf(':'); if(colon>0) values.put(line.substring(0,colon).trim(),value(line.substring(colon+1).trim())); }
|
|||
|
|
}
|
|||
|
|
return values;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 与配置的环境变量默认值语法一致,不回显实际凭据。 */
|
|||
|
|
private static String value(String raw) {
|
|||
|
|
if(raw.startsWith("${")&&raw.endsWith("}")) {
|
|||
|
|
String inside=raw.substring(2,raw.length()-1); int split=inside.indexOf(':');
|
|||
|
|
String environment=System.getenv(split<0?inside:inside.substring(0,split));
|
|||
|
|
raw=environment!=null?environment:split<0?"":inside.substring(split+1);
|
|||
|
|
}
|
|||
|
|
if(raw.length()>1&&(raw.startsWith("'")&&raw.endsWith("'")||raw.startsWith("\"")&&raw.endsWith("\""))) return raw.substring(1,raw.length()-1);
|
|||
|
|
return raw;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 迁移标记只判定完整成功,部分 DDL 不会被误认为已经完成。 */
|
|||
|
|
private static boolean applied(Connection connection) throws Exception {
|
|||
|
|
try(PreparedStatement statement=connection.prepareStatement("SELECT COUNT(*) FROM pm_collab_schema_migration WHERE name='008_stage_revisions'")) {
|
|||
|
|
try(ResultSet result=statement.executeQuery()) { result.next(); return result.getInt(1)>0; }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 对已有列和外键跳过重复 DDL,使 DDL 自动提交后中断仍可续跑。 */
|
|||
|
|
private static boolean skipExistingDdl(Connection connection,String sql) throws Exception {
|
|||
|
|
Matcher column=Pattern.compile("(?is)^ALTER TABLE\\s+(\\w+)\\s+ADD COLUMN\\s+(\\w+)\\s+.*").matcher(sql);
|
|||
|
|
if(column.matches()) try(ResultSet result=connection.getMetaData().getColumns(DATABASE,null,column.group(1),column.group(2))) { return result.next(); }
|
|||
|
|
Matcher constraint=Pattern.compile("(?is)^ALTER TABLE\\s+(\\w+)\\s+ADD CONSTRAINT\\s+(\\w+)\\s+.*").matcher(sql);
|
|||
|
|
if(constraint.matches()) try(PreparedStatement statement=connection.prepareStatement("SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=?")) {
|
|||
|
|
statement.setString(1,DATABASE); statement.setString(2,constraint.group(1)); statement.setString(3,constraint.group(2));
|
|||
|
|
try(ResultSet result=statement.executeQuery()) { result.next(); return result.getInt(1)>0; }
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 移除本文件的行注释并按分号拆句;迁移不包含存储过程或字符串内分号。 */
|
|||
|
|
private static List<String> statements(String sql) {
|
|||
|
|
String clean=sql.replaceAll("(?m)^--.*$",""); List<String> result=new ArrayList<>();
|
|||
|
|
for(String part:clean.split(";")) if(!part.isBlank()) result.add(part.strip());
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 结构与回填都核验后才接受迁移完成;其它业务表不参与。 */
|
|||
|
|
private static void verify(Connection connection) throws Exception {
|
|||
|
|
for(String table:List.of("pm_collab_stage_revision","pm_collab_stage_rework","pm_collab_flow_definition_revision"))
|
|||
|
|
try(ResultSet result=connection.getMetaData().getTables(DATABASE,null,table,null)) { if(!result.next()) throw new IllegalStateException("缺少 "+table); }
|
|||
|
|
for(String column:List.of("stage_revision_id","origin_work_item_id","schedule_review_rework_id"))
|
|||
|
|
try(ResultSet result=connection.getMetaData().getColumns(DATABASE,null,"pm_collab_work_item",column)) { if(!result.next()) throw new IllegalStateException("缺少 "+column); }
|
|||
|
|
try(Statement statement=connection.createStatement();ResultSet result=statement.executeQuery("SELECT COUNT(*) FROM pm_collab_project_stage s WHERE s.current_revision_id IS NULL OR NOT EXISTS(SELECT 1 FROM pm_collab_stage_revision r WHERE r.id=s.current_revision_id AND r.stage_id=s.id AND r.project_id=s.project_id)")) {
|
|||
|
|
result.next(); if(result.getLong(1)!=0) throw new IllegalStateException("阶段版本回填不完整");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|