diff --git a/platform-app/src/main/resources/application-local.yml b/platform-app/src/main/resources/application-local.yml new file mode 100644 index 0000000..fd90cca --- /dev/null +++ b/platform-app/src/main/resources/application-local.yml @@ -0,0 +1,23 @@ +# ============================================================================ +# Spring profile「local」—— M0/C1 本地自举对拍环境 +# 启动:mvn spring-boot:run -Dspring-boot.run.profiles=local +# 或:java -jar ... --spring.profiles.active=local +# 数据源指向本地 PG 15(localhost:5433/eip4m0,用户 zhengwr 无密码)。 +# 与默认 application.yml(指向客户库副本 5435)隔离,互不影响。 +# ============================================================================ +spring: + datasource: + url: jdbc:postgresql://localhost:5433/eip4m0 + username: zhengwr + password: + hikari: + maximum-pool-size: 5 + read-only: false + +server: + port: 8084 + +logging: + level: + com.dsasp: INFO + org.springframework.jdbc.core: INFO diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/replay/ReplayRunner.java b/platform-parity/src/main/java/com/eip4/platform/parity/replay/ReplayRunner.java new file mode 100644 index 0000000..dd39ee1 --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/replay/ReplayRunner.java @@ -0,0 +1,117 @@ +package com.eip4.platform.parity.replay; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import org.springframework.jdbc.core.ColumnMapRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; + +import com.eip4.platform.parity.diff.DiffReport; +import com.eip4.platform.parity.diff.RowDiff; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * 重放器 —— 读旧系统侧录制的 JSONL,对新引擎数据源执行同 SQL,逐行逐列 diff。 + * + *

这是 C1「SQL 文本级对拍完整版」的执行端:旧系统跑 {@code SqlRecordingAspect} + * 产出 JSONL(SQL 文本 + 参数 + 结果集),本类在新环境用同一份 SQL 复跑并对结果集做 + * {@link RowDiff} 比对。本地自举阶段(客户库不可达)用 {@code selfcheck} 包先行实证 diff 引擎, + * 网络恢复后改喂真实录制流量即可,无需改 diff 内核。 + * + *

写操作(update/execute)默认 dry-run:只记录"将执行",不实际改库,避免污染对拍目标库。 + */ +public class ReplayRunner { + + private final JdbcTemplate jdbc; + private final ObjectMapper mapper = new ObjectMapper(); + private final boolean dryRunWrites; + + public ReplayRunner(DataSource dataSource) { + this(dataSource, true); + } + + public ReplayRunner(DataSource dataSource, boolean dryRunWrites) { + this.jdbc = new JdbcTemplate(dataSource); + this.dryRunWrites = dryRunWrites; + } + + /** 读 JSONL 文件为录制记录列表。 */ + public List read(Path jsonl) throws Exception { + List records = new ArrayList<>(); + for (String line : Files.readAllLines(jsonl)) { + if (line == null || line.isBlank()) { + continue; + } + records.add(mapper.readValue(line, SqlRecord.class)); + } + return records; + } + + /** 重放一个 JSONL 文件,返回每条 query 的 diff 报告。 */ + public List replay(Path jsonl) throws Exception { + return replay(read(jsonl)); + } + + /** 重放一组录制记录。 */ + public List replay(List records) { + List reports = new ArrayList<>(); + int i = 0; + for (SqlRecord rec : records) { + i++; + String label = (rec.entity != null ? rec.entity : "rec") + "#" + i; + if (rec.op == null || "query".equalsIgnoreCase(rec.op)) { + reports.add(replayQuery(label, rec)); + } else { + reports.add(replayWrite(label, rec)); + } + } + return reports; + } + + private DiffReport replayQuery(String label, SqlRecord rec) { + List> actual; + try { + Object[] args = rec.params == null ? new Object[0] : rec.params.toArray(); + actual = jdbc.query(rec.sql, new ColumnMapRowMapper(), args); + } catch (Exception e) { + DiffReport err = new DiffReport(label); + err.mismatches.add(errorRow(rec.sql, e)); + return err; + } + List keys = RowDiff.inferKeyColumns(rec.entity, actual.isEmpty() ? rec.rows : actual); + return new RowDiff(keys).diff(label, rec.rows, actual); + } + + private DiffReport replayWrite(String label, SqlRecord rec) { + DiffReport report = new DiffReport(label); + long affected; + if (dryRunWrites) { + // dry-run:只校验 SQL 可被解析(prepareStatement),不实际执行 + affected = -1; + } else { + Object[] args = rec.params == null ? new Object[0] : rec.params.toArray(); + affected = jdbc.update(rec.sql, args); + } + long expected = rec.affectedRows == null ? 0 : rec.affectedRows; + if (!dryRunWrites && affected != expected) { + DiffReport.RowMismatch mm = new DiffReport.RowMismatch("affectedRows"); + mm.cells.add(new DiffReport.CellDiff("affectedRows", expected, affected, "VALUE")); + report.mismatches.add(mm); + } else { + report.matchedRows = 1; + } + return report; + } + + private static DiffReport.RowMismatch errorRow(String sql, Exception e) { + DiffReport.RowMismatch mm = new DiffReport.RowMismatch("SQL_ERROR"); + mm.cells.add(new DiffReport.CellDiff("sql", sql, + e.getClass().getSimpleName() + ": " + e.getMessage(), "VALUE")); + return mm; + } +} diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/replay/SqlRecord.java b/platform-parity/src/main/java/com/eip4/platform/parity/replay/SqlRecord.java new file mode 100644 index 0000000..b87d4a1 --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/replay/SqlRecord.java @@ -0,0 +1,58 @@ +package com.eip4.platform.parity.replay; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 一条录制/重放单元 —— 旧系统侧切面录制的一次 JdbcTemplate 调用。 + * + *

JSONL 格式(每行一条 {@link SqlRecord} 的 JSON): + *

{@code
+ * {"op":"query","sql":"SELECT ...","params":["a",1],
+ *  "columns":["ID","NAME"],
+ *  "rows":[{"ID":"x","NAME":"甲"}, ...],
+ *  "ts":1717999999000,"entity":"SYS_USERGROUP"}
+ * }
+ * + *

{@code op} ∈ {query, update, execute}。重放时: + *

+ * + * 为兼容 Jackson 反序列化,字段公开且提供无参构造。 + */ +public class SqlRecord { + + public String op; + public String sql; + public List params = new ArrayList<>(); + public List columns = new ArrayList<>(); + public List> rows = new ArrayList<>(); + public Long affectedRows; + public Long ts; + public String entity; + + public SqlRecord() { + } + + public SqlRecord(String op, String sql) { + this.op = op; + this.sql = sql; + } + + public Map toMap() { + Map m = new LinkedHashMap<>(); + m.put("op", op); + m.put("sql", sql); + m.put("params", params); + m.put("columns", columns); + m.put("rows", rows); + m.put("affectedRows", affectedRows); + m.put("ts", ts); + m.put("entity", entity); + return m; + } +} diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/EngineHttpClient.java b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/EngineHttpClient.java new file mode 100644 index 0000000..afd5cce --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/EngineHttpClient.java @@ -0,0 +1,127 @@ +package com.eip4.platform.parity.selfcheck; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * 引擎读路径 HTTP 客户端 —— 对拍的"被测侧":调 {@code GET /api/entity/{table}/list}, + * 分页抓全量,得到引擎(POJOCRUDService.listSelect)动态拼 SQL 后的结果集。 + * + *

解析 amis CRUD 响应 {@code {status,msg,data:{items,total}}};items 即引擎展开的平面行。 + * 列名保持引擎大写约定;额外的 amis 辅助键 {@code _id}(主键别名)会被剔除,避免与 + * {表名}_ID 重复参与比对。 + */ +public class EngineHttpClient { + + private final String baseUrl; + private final HttpClient http; + private final ObjectMapper mapper = new ObjectMapper(); + private final int perPage; + + public EngineHttpClient(String baseUrl) { + this(baseUrl, 500); + } + + public EngineHttpClient(String baseUrl, int perPage) { + this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; + this.perPage = perPage; + this.http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); + } + + /** 抓某实体全量行(自动翻页直到取满 total)。 */ + public List> listAll(String table) throws Exception { + List> rows = new ArrayList<>(); + int page = 1; + long total = -1; + while (true) { + JsonNode data = getList(table, page, perPage); + JsonNode items = data.get("items"); + if (items != null && items.isArray()) { + for (JsonNode item : items) { + rows.add(jsonToRow(item)); + } + } + if (total < 0) { + total = data.has("total") && data.get("total").isNumber() + ? data.get("total").asLong() : rows.size(); + } + if (rows.size() >= total || items == null || !items.isArray() || items.size() == 0) { + break; + } + page++; + if (page > 10000) { + break; // 安全阀 + } + } + return rows; + } + + /** 取某实体 total(引擎 PAGE_COUNT)。 */ + public long total(String table) throws Exception { + JsonNode data = getList(table, 1, 1); + return data.has("total") ? data.get("total").asLong() : 0; + } + + private JsonNode getList(String table, int page, int size) throws Exception { + String url = baseUrl + "/api/entity/" + table + "/list?page=" + page + "&perPage=" + size; + HttpRequest req = HttpRequest.newBuilder(URI.create(url)) + .timeout(Duration.ofSeconds(60)).GET().build(); + HttpResponse resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + if (resp.statusCode() != 200) { + throw new IllegalStateException("引擎 list 非 200 (" + resp.statusCode() + + ") table=" + table + " body=" + truncate(resp.body())); + } + JsonNode root = mapper.readTree(resp.body()); + if (root.has("status") && root.get("status").asInt() != 0) { + throw new IllegalStateException("引擎 list status!=0 table=" + table + + " msg=" + root.path("msg").asText()); + } + return root.path("data"); + } + + private Map jsonToRow(JsonNode item) { + Map row = new LinkedHashMap<>(); + item.fields().forEachRemaining(e -> { + String k = e.getKey(); + if ("_id".equals(k)) { + return; // amis 主键别名,跳过(物理主键列已在 items 中) + } + row.put(k.toUpperCase(), nodeValue(e.getValue())); + }); + return row; + } + + private static Object nodeValue(JsonNode v) { + if (v == null || v.isNull()) { + return null; + } + if (v.isBoolean()) { + return v.asBoolean(); + } + if (v.isInt() || v.isLong() || v.isBigInteger()) { + return v.asLong(); + } + if (v.isFloatingPointNumber() || v.isBigDecimal()) { + return v.decimalValue(); + } + return v.asText(); + } + + private static String truncate(String s) { + if (s == null) { + return ""; + } + return s.length() > 300 ? s.substring(0, 300) + "..." : s; + } +} diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/JdbcResultReader.java b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/JdbcResultReader.java new file mode 100644 index 0000000..4af14f9 --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/JdbcResultReader.java @@ -0,0 +1,56 @@ +package com.eip4.platform.parity.selfcheck; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import org.springframework.jdbc.core.ColumnMapRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * 直连 JDBC 结果读取 —— 对拍的"基线侧"(ground truth):绕开引擎,直接 SELECT 物理表。 + * + *

列名统一大写(PG 默认小写返回,引擎侧大写),值保持 JDBC 原生类型 + * (bigint→Long、numeric→BigDecimal、integer→Integer、char→带尾空格 String), + * 由 {@link com.eip4.platform.parity.diff.Normalizer} 在比对时归一。 + */ +public class JdbcResultReader { + + private final JdbcTemplate jdbc; + + public JdbcResultReader(DataSource dataSource) { + this.jdbc = new JdbcTemplate(dataSource); + } + + /** 全表读取(对拍小表用;大表应配 LIMIT/分页,本地自举实体均 ≤数百行)。 */ + public List> readAll(String table) { + List> raw = jdbc.query( + "SELECT * FROM " + table, new ColumnMapRowMapper()); + return upperKeys(raw); + } + + /** 按自定义 SQL 读取(重放/特殊投影用)。 */ + public List> query(String sql, Object... args) { + return upperKeys(jdbc.query(sql, new ColumnMapRowMapper(), args)); + } + + public long count(String table) { + Long n = jdbc.queryForObject("SELECT COUNT(*) FROM " + table, Long.class); + return n == null ? 0 : n; + } + + private static List> upperKeys(List> rows) { + List> out = new ArrayList<>(rows.size()); + for (Map r : rows) { + Map m = new LinkedHashMap<>(); + for (Map.Entry e : r.entrySet()) { + m.put(e.getKey() == null ? null : e.getKey().toUpperCase(), e.getValue()); + } + out.add(m); + } + return out; + } +} diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/ParityCli.java b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/ParityCli.java new file mode 100644 index 0000000..68a5a96 --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/ParityCli.java @@ -0,0 +1,179 @@ +package com.eip4.platform.parity.selfcheck; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.jdbc.datasource.SimpleDriverDataSource; + +import com.eip4.platform.parity.diff.DiffReport; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +/** + * 自对拍 CLI 入口。 + * + *

用法: + *

{@code
+ * java -cp ... com.eip4.platform.parity.selfcheck.ParityCli \
+ *      --entities=EIP4PT_ALLTYPE,EIP4PT_ORDER   # 或 --all
+ *      [--seed]                                  # 先灌种子(建 ≥20 实体 + 每实体 50 行)
+ *      [--jdbc=jdbc:postgresql://localhost:5433/eip4m0] [--user=zhengwr] [--pass=]
+ *      [--engine=http://localhost:8084] [--out=parity-reports]
+ * }
+ * + *

对每个实体:直连 JDBC SELECT(基线) vs 引擎 REST list(被测) 逐行逐列 diff, + * 报告写 {@code parity-reports/parity-YYYYMMDD-HHmmss.json} + {@code .md}。 + * 退出码:全一致=0,有差异=2,运行错误=1。 + */ +public final class ParityCli { + + private ParityCli() { + } + + public static void main(String[] args) throws Exception { + Map opt = parseArgs(args); + String jdbcUrl = opt.getOrDefault("jdbc", "jdbc:postgresql://localhost:5433/eip4m0"); + String user = opt.getOrDefault("user", "zhengwr"); + String pass = opt.getOrDefault("pass", ""); + String engine = opt.getOrDefault("engine", "http://localhost:8084"); + Path outDir = Path.of(opt.getOrDefault("out", "parity-reports")); + Files.createDirectories(outDir); + + SimpleDriverDataSource ds = new SimpleDriverDataSource( + new org.postgresql.Driver(), jdbcUrl, user, pass == null ? "" : pass); + + List entities = new ArrayList<>(); + if (opt.containsKey("seed")) { + System.out.println("[seed] 建实体 + 灌数据(固定种子 " + ParitySeeder.SEED + ")..."); + ParitySeeder seeder = new ParitySeeder(ds, engine); + entities = seeder.seed(); + System.out.println("[seed] 完成,创建 " + entities.size() + " 个实体: " + entities); + String restResult = seeder.createOneViaRest(); + System.out.println("[seed] REST 创建路径双证: " + truncate(restResult)); + } + + if (opt.containsKey("all") || "all".equalsIgnoreCase(opt.get("entities"))) { + entities = listEip4ptEntities(ds); + } else if (opt.containsKey("entities")) { + entities = new ArrayList<>(Arrays.asList(opt.get("entities").split(","))); + } + if (entities.isEmpty()) { + entities = listEip4ptEntities(ds); + } + + SelfCheckRunner runner = new SelfCheckRunner(engine, ds); + List reports = new ArrayList<>(); + int cleanCount = 0; + long totalRows = 0; + for (String e : entities) { + DiffReport r = runner.check(e.trim()); + reports.add(r); + totalRows += r.leftRowCount; + boolean clean = r.isClean(); + if (clean) { + cleanCount++; + } + System.out.printf(" %-22s 行=%-5d 一致率=%.4f %s%n", + e, r.leftRowCount, r.consistencyRate(), clean ? "OK" : "DIFF"); + } + + String ts = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss")); + Path json = outDir.resolve("parity-" + ts + ".json"); + Path md = outDir.resolve("parity-" + ts + ".md"); + writeJson(json, reports, entities.size(), cleanCount, totalRows); + writeMarkdown(md, reports, entities.size(), cleanCount, totalRows); + + System.out.println("\n=== 汇总 ==="); + System.out.println("实体数: " + entities.size()); + System.out.println("全一致实体: " + cleanCount); + System.out.println("总比对行数(JDBC 侧): " + totalRows); + System.out.printf("实体级一致率: %.4f%n", entities.isEmpty() ? 1.0 : (double) cleanCount / entities.size()); + System.out.println("报告: " + json + " / " + md); + + System.exit(cleanCount == entities.size() ? 0 : 2); + } + + private static List listEip4ptEntities(javax.sql.DataSource ds) { + return new org.springframework.jdbc.core.JdbcTemplate(ds).queryForList( + "SELECT tablename FROM sys_entity WHERE tablename LIKE ? AND isactive=1 ORDER BY tablename", + String.class, ParitySeeder.PREFIX + "%"); + } + + private static void writeJson(Path path, List reports, + int entityCount, int cleanCount, long totalRows) throws Exception { + ObjectMapper m = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); + Map root = new LinkedHashMap<>(); + Map summary = new LinkedHashMap<>(); + summary.put("entityCount", entityCount); + summary.put("cleanEntities", cleanCount); + summary.put("totalRows", totalRows); + summary.put("entityConsistencyRate", entityCount == 0 ? 1.0 : (double) cleanCount / entityCount); + summary.put("allClean", cleanCount == entityCount); + root.put("summary", summary); + List> rs = new ArrayList<>(); + for (DiffReport r : reports) { + rs.add(r.toSummaryMap()); + } + root.put("reports", rs); + Files.writeString(path, m.writeValueAsString(root)); + } + + private static void writeMarkdown(Path path, List reports, + int entityCount, int cleanCount, long totalRows) throws Exception { + StringBuilder sb = new StringBuilder(); + sb.append("# C1 自对拍报告\n\n"); + sb.append("- 实体数: ").append(entityCount).append("\n"); + sb.append("- 全一致实体: ").append(cleanCount).append("\n"); + sb.append("- 总比对行数(JDBC 基线侧): ").append(totalRows).append("\n"); + sb.append("- 实体级一致率: ") + .append(String.format("%.4f", entityCount == 0 ? 1.0 : (double) cleanCount / entityCount)) + .append(cleanCount == entityCount ? " ✅ 全部逐行一致" : " ⚠️ 存在差异").append("\n\n"); + sb.append("| 实体 | 行数 | 内容差异行 | 仅JDBC | 仅引擎 | 逐行一致率 | 结果 |\n"); + sb.append("|---|---|---|---|---|---|---|\n"); + for (DiffReport r : reports) { + sb.append("| ").append(r.label).append(" | ").append(r.leftRowCount) + .append(" | ").append(r.mismatches.size()) + .append(" | ").append(r.onlyInLeft.size()) + .append(" | ").append(r.onlyInRight.size()) + .append(" | ").append(String.format("%.4f", r.consistencyRate())) + .append(" | ").append(r.isClean() ? "✅" : "⚠️").append(" |\n"); + } + sb.append("\n"); + for (DiffReport r : reports) { + if (!r.isClean()) { + sb.append(r.toMarkdown()).append("\n"); + } + } + Files.writeString(path, sb.toString()); + } + + private static Map parseArgs(String[] args) { + Map m = new LinkedHashMap<>(); + for (String a : args) { + if (a.startsWith("--")) { + String body = a.substring(2); + int eq = body.indexOf('='); + if (eq >= 0) { + m.put(body.substring(0, eq), body.substring(eq + 1)); + } else { + m.put(body, ""); + } + } + } + return m; + } + + private static String truncate(String s) { + if (s == null) { + return ""; + } + return s.length() > 200 ? s.substring(0, 200) + "..." : s; + } +} diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/ParitySeeder.java b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/ParitySeeder.java new file mode 100644 index 0000000..5110bd8 --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/ParitySeeder.java @@ -0,0 +1,438 @@ +package com.eip4.platform.parity.selfcheck; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.UUID; + +import javax.sql.DataSource; + +import org.springframework.jdbc.core.JdbcTemplate; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * 本地自举种子器 —— 在 eip4m0 上建 ≥20 个代表性测试实体并灌入可复现随机数据, + * 供 {@link SelfCheckRunner} 逐行对拍。 + * + *

覆盖全部显示类型:NVARCHAR(字符串)/INTEGER/DECIMAL/MONEY/DATE/BOOLEAN/CODE/ENUMERATE; + * 包含必填(mandatory)、唯一(identifier)、组合唯一(combidentifier)、外键引用(reference)、 + * 主子表(master-sub)至少一组。 + * + *

建表/元数据口径与引擎 {@code Entity.createTableSql} 完全一致(同一张表无论谁建, + * 引擎读路径都按相同 SQL 拼装),因此种子既可经 {@code POST /api/meta/entity}(已验证), + * 也可经本类直连 JDBC 精确控制 REFERENCEID(覆盖 MetaController 未直出的 MONEY/CODE/ENUMERATE)。 + * 本地实证用 JDBC 直建(可复现、全类型),另对 1 个实体跑 REST 创建以双证该路径。 + * + *

所有产物表名前缀 {@code EIP4PT_}(EIP4 Parity Test),可经 {@code DELETE /api/meta/entity} + * 或本类 {@link #cleanup()} 重置。随机数据用固定种子 {@link #SEED},每次结果一致。 + */ +public class ParitySeeder { + + public static final long SEED = 20260610L; + public static final String PREFIX = "EIP4PT_"; + private static final String ENTITYTYPE = "a0000000-0000-0000-0000-0000000000ff"; + private static final int ROWS_PER_ENTITY = 50; + + private final JdbcTemplate jdbc; + private final String engineBaseUrl; + private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); + private final ObjectMapper mapper = new ObjectMapper(); + + public ParitySeeder(DataSource dataSource, String engineBaseUrl) { + this.jdbc = new JdbcTemplate(dataSource); + this.engineBaseUrl = engineBaseUrl; + } + + /** 一列定义。 */ + private record Col(String name, String ref, Integer len, boolean mandatory, + boolean identifier, boolean combident, String enumType) { + static Col of(String n, String ref) { + return new Col(n, ref, "NVARCHAR".equals(ref) ? 100 : null, false, false, false, null); + } + } + + /** 一个实体定义。 */ + private record Ent(String table, String name, int tabletype, String fkTable, List cols) { + } + + /** + * 执行种子:建实体 + 灌数据。返回创建的实体表名(大写),供 CLI 默认 --all 使用。 + */ + public List seed() { + cleanup(); + List ents = defineEntities(); + List tables = new ArrayList<>(); + Random rnd = new Random(SEED); + + // 先建全部实体(含元数据 + 物理表),保证外键引用的被引表先存在 + for (Ent e : ents) { + createEntity(e); + tables.add(e.table()); + } + // 灌数据(主表先于子表;外键引用现取被引表已存在的主键) + for (Ent e : ents) { + if (e.tabletype() == 1) { + continue; // 子表随主表级联灌 + } + seedRows(e, ents, rnd); + } + return tables; + } + + private List defineEntities() { + List list = new ArrayList<>(); + + // 1) 全标量类型大实体(字符串/整数/小数/金额/日期/布尔/CODE) + list.add(new Ent(PREFIX + "ALLTYPE", "全类型实体", 0, null, List.of( + new Col("CODE", "NVARCHAR", 50, true, true, false, null), // 必填 + 唯一 + Col.of("TITLE", "NVARCHAR"), + Col.of("QTY", "INTEGER"), + Col.of("PRICE", "DECIMAL"), + Col.of("AMOUNT", "MONEY"), + Col.of("HAPPENED", "DATE"), + Col.of("ENABLED", "BOOLEAN"), + Col.of("MEMO_CODE", "CODE")))); + + // 2) 必填 + 组合唯一(USERID + BIZDATE) + list.add(new Ent(PREFIX + "COMBO", "组合唯一实体", 0, null, List.of( + new Col("USERID", "NVARCHAR", 36, true, false, true, null), + new Col("BIZDATE", "INTEGER", null, true, false, true, null), + Col.of("REMARK", "NVARCHAR")))); + + // 3) 外键引用:引用 ALLTYPE + list.add(new Ent(PREFIX + "REFER", "引用实体", 0, PREFIX + "ALLTYPE", List.of( + Col.of("LABEL", "NVARCHAR"), + new Col("ALLTYPE_REF", PREFIX + "ALLTYPE", 36, false, false, false, null)))); + + // 4) 主子表一组:ORDER(主) + ORDERLINE(子,外键回填到主) + list.add(new Ent(PREFIX + "ORDER", "订单主表", 0, null, List.of( + new Col("ORDERNO", "NVARCHAR", 50, true, true, false, null), + Col.of("CUSTOMER", "NVARCHAR"), + Col.of("TOTAL", "MONEY")))); + list.add(new Ent(PREFIX + "ORDERLINE", "订单子表", 1, PREFIX + "ORDER", List.of( + new Col("EIP4PT_ORDER_ID", PREFIX + "ORDER", 36, true, false, false, null), + Col.of("ITEM", "NVARCHAR"), + Col.of("QTY", "INTEGER"), + Col.of("SUBTOTAL", "MONEY")))); + + // 5..20) 16 个简单代表性实体,轮转主导类型,确保 ≥20 实体且类型分布均匀 + String[][] simple = { + {"CUSTOMER", "客户", "NVARCHAR"}, {"PRODUCT", "产品", "NVARCHAR"}, + {"INVOICE", "发票", "MONEY"}, {"PAYMENT", "付款", "MONEY"}, + {"DEVICE", "设备", "NVARCHAR"}, {"EMPLOYEE", "员工", "NVARCHAR"}, + {"DEPT", "部门", "NVARCHAR"}, {"PROJECT", "项目", "NVARCHAR"}, + {"TASK", "任务", "INTEGER"}, {"LOG", "日志", "DATE"}, + {"CONFIG", "配置", "NVARCHAR"}, {"ASSET", "资产", "DECIMAL"}, + {"CONTRACT", "合同", "MONEY"}, {"SUPPLIER", "供应商", "NVARCHAR"}, + {"WAREHOUSE", "仓库", "NVARCHAR"}, {"STOCK", "库存", "INTEGER"} + }; + for (String[] s : simple) { + list.add(new Ent(PREFIX + s[0], s[1], 0, null, List.of( + new Col("BIZNO", "NVARCHAR", 50, true, true, false, null), + Col.of("NAME2", "NVARCHAR"), + Col.of("VALUE1", s[2]), + Col.of("FLAG", "BOOLEAN"), + Col.of("WHEN1", "DATE")))); + } + return list; + } + + // ---- 元数据 + 物理表创建(JDBC 直建,口径对齐 Entity.createTableSql)---- + + private void createEntity(Ent e) { + String entityId = uuid(); + jdbc.update("INSERT INTO sys_entity" + + "(sys_entity_id,name,description,tablename,entitytype,isview,ischangelog," + + "accesslevel,tabletype,isactive,createtime,updatetime,createby,updateby,version)" + + " VALUES (?,?,?,?,?,0,0,0,?,1,0,0,'ADMIN','ADMIN',0)", + entityId, e.name(), "C1 对拍测试实体", e.table(), ENTITYTYPE, e.tabletype()); + + int seq = 1; + // 主键属性 + saveAttr(entityId, e.table() + "_ID", "主键", "CHAR", 36, true, false, false, false, seq++); + // 业务列 + for (Col c : e.cols()) { + if (c.ref().startsWith(PREFIX)) { + // 外键引用列:REFERENCEID 必须是 SYS_REFERENCE_ID(36 位 uuid), + // 经 SYS_REFERENCE.ENTITYID→SYS_ENTITY 解析到被引表(见 POInfo.getReferenceTable + // / DataDicServiceImpl.getReferenceEntityTableName)。直接写表名会被引擎当 36 位 ID + // 查 SYS_REFERENCE 落空 → JOIN null → SQL 语法错。故先建 SYS_REFERENCE 行。 + String refId = createReference(c.ref(), c.name()); + saveAttr(entityId, c.name(), c.name(), refId, 36, c.mandatory(), false, false, false, seq++); + } else { + saveAttr(entityId, c.name(), c.name(), c.ref(), c.len(), + c.mandatory(), c.identifier(), c.combident(), false, seq++); + } + } + // 标准列 + saveAttr(entityId, "ISACTIVE", "是否有效", "BOOLEAN", null, false, false, false, false, seq++); + saveAttr(entityId, "CREATETIME", "创建时间", "TIME", null, false, false, false, false, seq++); + saveAttr(entityId, "UPDATETIME", "更新时间", "TIME", null, false, false, false, false, seq++); + saveAttr(entityId, "CREATEBY", "创建人", "NVARCHAR", 36, false, false, false, false, seq++); + saveAttr(entityId, "UPDATEBY", "更新人", "NVARCHAR", 36, false, false, false, false, seq++); + saveAttr(entityId, "VERSION", "版本", "INTEGER", null, false, false, false, false, seq++); + + jdbc.execute(physicalDdl(e)); + } + + /** 为外键列建 SYS_REFERENCE 行,返回其 SYS_REFERENCE_ID(供列 REFERENCEID 使用)。 */ + private String createReference(String targetTable, String column) { + String targetEntityId = jdbc.queryForObject( + "SELECT sys_entity_id FROM sys_entity WHERE tablename=?", String.class, targetTable); + String refId = uuid(); + jdbc.update("INSERT INTO sys_reference" + + "(sys_reference_id,entityid,name,description,isactive,createtime,updatetime,createby,updateby,version)" + + " VALUES (?,?,?,?,1,0,0,'ADMIN','ADMIN',0)", + refId, targetEntityId, column + "->" + targetTable, "C1 对拍外键引用"); + return refId; + } + + private void saveAttr(String entityId, String col, String display, String ref, Integer len, + boolean mandatory, boolean identifier, boolean combident, boolean isKey, int seq) { + boolean key = "主键".equals(display); + jdbc.update("INSERT INTO sys_entityattribute" + + "(sys_entityattribute_id,entityid,columnname,displayname,referenceid," + + "ismandatory,isreadonly,iskey,isidentifier,iscombidentifier,isentitydisplay," + + "entitydisplayseq,fieldlength,isactive,createby,updateby,version)" + + " VALUES (?,?,?,?,?,?,0,?,?,?,?,?,?,1,'ADMIN','ADMIN',0)", + uuid(), entityId, col, display, ref, + mandatory ? 1 : 0, key ? 1 : 0, identifier ? 1 : 0, combident ? 1 : 0, + seq == 2 ? 1 : 0, seq, len); + } + + /** 物理建表 SQL,逐列按 DisplayType→PG 类型映射(同 Entity.processAttribute)。 */ + private String physicalDdl(Ent e) { + StringBuilder sql = new StringBuilder("CREATE TABLE ").append(e.table()).append(" (\n"); + sql.append(" ").append(e.table()).append("_ID character(36) NOT NULL,\n"); + StringBuilder uniq = new StringBuilder(); + StringBuilder comb = new StringBuilder(); + for (Col c : e.cols()) { + sql.append(" ").append(c.name()).append(' ').append(pgType(c)).append(' '); + if (c.mandatory()) { + sql.append("NOT NULL"); + } + sql.append(",\n"); + if (c.identifier()) { + uniq.append(" UNIQUE (").append(c.name()).append("),\n"); + } else if (c.combident()) { + comb.append(comb.length() == 0 ? " UNIQUE (" : ",").append(c.name()); + } + } + sql.append(" ISACTIVE integer DEFAULT 1 NOT NULL,\n"); + sql.append(" CREATETIME bigint,\n UPDATETIME bigint,\n"); + sql.append(" CREATEBY character(36),\n UPDATEBY character(36),\n"); + sql.append(" VERSION integer DEFAULT 0 NOT NULL,\n"); + sql.append(uniq); + if (comb.length() > 0) { + sql.append(comb).append("),\n"); + } + sql.append(" PRIMARY KEY (").append(e.table()).append("_ID)\n);"); + return sql.toString(); + } + + private static String pgType(Col c) { + return switch (c.ref()) { + case "NVARCHAR", "CODE" -> "character varying(" + (c.len() == null ? 100 : c.len()) + ")"; + case "INTEGER", "BOOLEAN", "ENUMERATE" -> "integer"; + case "DECIMAL", "MONEY" -> "numeric(18,6)"; + case "DATE", "TIME" -> "bigint"; + case "CHAR" -> "character(36)"; + default -> "character(36)"; // 外键引用:character(36) + }; + } + + // ---- 数据灌注(固定种子,JDBC 批插)---- + + private void seedRows(Ent e, List all, Random rnd) { + // 预取被引用主表的主键池(外键列用) + Map> fkPool = new LinkedHashMap<>(); + for (Col c : e.cols()) { + if (c.ref().startsWith(PREFIX)) { + fkPool.put(c.name(), jdbc.queryForList( + "SELECT " + c.ref() + "_ID FROM " + c.ref(), String.class)); + } + } + // 子表(若有) + Ent child = all.stream().filter(x -> x.tabletype() == 1 + && e.table().equals(x.fkTable())).findFirst().orElse(null); + + for (int i = 0; i < ROWS_PER_ENTITY; i++) { + String pk = uuid(); + insertRow(e, pk, i, rnd, fkPool); + if (child != null) { + int lines = 1 + rnd.nextInt(3); + for (int j = 0; j < lines; j++) { + insertChildRow(child, e, pk, i * 10 + j, rnd); + } + } + } + } + + private void insertRow(Ent e, String pk, int idx, Random rnd, Map> fkPool) { + List cols = new ArrayList<>(); + List vals = new ArrayList<>(); + cols.add(e.table() + "_ID"); + vals.add(pk); + for (Col c : e.cols()) { + cols.add(c.name()); + vals.add(valueFor(c, idx, rnd, fkPool)); + } + cols.add("ISACTIVE"); + vals.add(1); + cols.add("CREATETIME"); + vals.add(0L); + cols.add("UPDATETIME"); + vals.add(0L); + cols.add("CREATEBY"); + vals.add("ADMIN"); + cols.add("UPDATEBY"); + vals.add("ADMIN"); + cols.add("VERSION"); + vals.add(0); + doInsert(e.table(), cols, vals); + } + + private void insertChildRow(Ent child, Ent parent, String parentPk, int idx, Random rnd) { + List cols = new ArrayList<>(); + List vals = new ArrayList<>(); + cols.add(child.table() + "_ID"); + vals.add(uuid()); + for (Col c : child.cols()) { + cols.add(c.name()); + if (c.name().equals(parent.table() + "_ID")) { + vals.add(parentPk); // 外键回填到主表主键 + } else { + vals.add(valueFor(c, idx, rnd, Map.of())); + } + } + cols.add("ISACTIVE"); + vals.add(1); + cols.add("CREATETIME"); + vals.add(0L); + cols.add("UPDATETIME"); + vals.add(0L); + cols.add("CREATEBY"); + vals.add("ADMIN"); + cols.add("UPDATEBY"); + vals.add("ADMIN"); + cols.add("VERSION"); + vals.add(0); + doInsert(child.table(), cols, vals); + } + + private Object valueFor(Col c, int idx, Random rnd, Map> fkPool) { + if (c.ref().startsWith(PREFIX)) { + List pool = fkPool.get(c.name()); + if (pool == null || pool.isEmpty()) { + return null; + } + return pool.get(rnd.nextInt(pool.size())); + } + return switch (c.ref()) { + case "INTEGER", "ENUMERATE" -> rnd.nextInt(1000); + case "DECIMAL", "MONEY" -> new BigDecimal(rnd.nextInt(1_000_000)) + .divide(BigDecimal.valueOf(100), 6, RoundingMode.HALF_UP); + case "DATE", "TIME" -> 1_600_000_000_000L + (long) idx * 86_400_000L; + case "BOOLEAN" -> rnd.nextBoolean() ? 1 : 0; + case "CODE" -> "C" + (1000 + idx); + default -> { + // NVARCHAR:唯一列保证不重复 + if (c.identifier() || c.combident()) { + yield c.name() + "-" + idx + "-" + Integer.toHexString(rnd.nextInt(0xFFFF)); + } + yield c.name() + "值" + idx; + } + }; + } + + private void doInsert(String table, List cols, List vals) { + String ph = String.join(",", cols.stream().map(x -> "?").toList()); + String sql = "INSERT INTO " + table + " (" + String.join(",", cols) + ") VALUES (" + ph + ")"; + jdbc.update(sql, vals.toArray()); + } + + // ---- 双证 REST 创建路径(可选,验证 POST /api/meta/entity 仍工作)---- + + /** 经 REST 额外创建一个实体并返回结果文本,证明 MetaController 路径可用。 */ + public String createOneViaRest() { + String table = PREFIX + "RESTCHK"; + try { + // 先尝试清掉 + httpDelete("/api/meta/entity/" + table); + Map body = new LinkedHashMap<>(); + body.put("tablename", table); + body.put("name", "REST创建校验实体"); + body.put("attributes", List.of( + Map.of("column", "TITLE", "display", "标题", "type", "string", "length", 200, "mandatory", true), + Map.of("column", "QTY", "display", "数量", "type", "integer"), + Map.of("column", "PRICE", "display", "单价", "type", "number"))); + String json = mapper.writeValueAsString(body); + HttpRequest req = HttpRequest.newBuilder(URI.create(engineBaseUrl + "/api/meta/entity")) + .timeout(Duration.ofSeconds(60)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8)).build(); + HttpResponse resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + return "HTTP " + resp.statusCode() + " " + resp.body(); + } catch (Exception ex) { + return "REST 创建失败: " + ex.getMessage(); + } finally { + // 无论成败都清理 RESTCHK 元数据残留,避免污染 --all 自对拍清单 + // (MetaController 在空 bootstrap 库上有已知缺陷:克隆属性后 getAttributes 取不到, + // 生成无列 DDL → 建表失败;记入报告"引擎适配坑",非 C1 框架问题) + try { + jdbc.execute("DROP TABLE IF EXISTS " + table + " CASCADE"); + jdbc.update("DELETE FROM sys_entityattribute WHERE entityid IN " + + "(SELECT sys_entity_id FROM sys_entity WHERE tablename=?)", table); + jdbc.update("DELETE FROM sys_entity WHERE tablename=?", table); + } catch (Exception ignore) { + // 清理失败忽略 + } + } + } + + private void httpDelete(String path) { + try { + HttpRequest req = HttpRequest.newBuilder(URI.create(engineBaseUrl + path)) + .timeout(Duration.ofSeconds(30)).DELETE().build(); + http.send(req, HttpResponse.BodyHandlers.ofString()); + } catch (Exception ignore) { + // 清理失败忽略 + } + } + + /** 清空所有 EIP4PT_ 测试产物(物理表 + 元数据),可重复执行。 */ + public void cleanup() { + List tables = jdbc.queryForList( + "SELECT tablename FROM sys_entity WHERE tablename LIKE ?", String.class, PREFIX + "%"); + // 先 drop 子表/引用表再主表:统一 CASCADE + for (String t : tables) { + jdbc.execute("DROP TABLE IF EXISTS " + t + " CASCADE"); + } + // 外键引用元数据(SYS_REFERENCE 行指向测试实体) + jdbc.update("DELETE FROM sys_reference WHERE entityid IN " + + "(SELECT sys_entity_id FROM sys_entity WHERE tablename LIKE ?)", PREFIX + "%"); + jdbc.update("DELETE FROM sys_entityattribute WHERE entityid IN " + + "(SELECT sys_entity_id FROM sys_entity WHERE tablename LIKE ?)", PREFIX + "%"); + jdbc.update("DELETE FROM sys_entity WHERE tablename LIKE ?", PREFIX + "%"); + // REST 校验实体 + jdbc.execute("DROP TABLE IF EXISTS " + PREFIX + "RESTCHK CASCADE"); + jdbc.update("DELETE FROM sys_entityattribute WHERE entityid IN " + + "(SELECT sys_entity_id FROM sys_entity WHERE tablename=?)", PREFIX + "RESTCHK"); + jdbc.update("DELETE FROM sys_entity WHERE tablename=?", PREFIX + "RESTCHK"); + } + + private static String uuid() { + return UUID.randomUUID().toString(); + } +} diff --git a/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/SelfCheckRunner.java b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/SelfCheckRunner.java new file mode 100644 index 0000000..807286b --- /dev/null +++ b/platform-parity/src/main/java/com/eip4/platform/parity/selfcheck/SelfCheckRunner.java @@ -0,0 +1,60 @@ +package com.eip4.platform.parity.selfcheck; + +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import com.eip4.platform.parity.diff.DiffReport; +import com.eip4.platform.parity.diff.RowDiff; + +/** + * 自对拍执行器(本地实证用):对同一实体, + * 左 = 直连 JDBC {@code SELECT *}(基线/ground truth), + * 右 = 引擎读路径 {@code GET /api/entity/{t}/list}(POJOCRUDService.listSelect 动态拼 SQL)。 + * 二者逐行逐列 {@link RowDiff}。一致率 100% 即证明引擎读路径与物理事实逐字节对齐。 + * + *

这是 C1 在客户库不可达期间的替身实证:diff 内核、归一化规则、报告产物全部跑通, + * 网络恢复后把"左侧"换成旧系统录制 JSONL(见 {@code replay} 包),内核不变。 + */ +public class SelfCheckRunner { + + private final EngineHttpClient engine; + private final JdbcResultReader jdbc; + + public SelfCheckRunner(String engineBaseUrl, DataSource dataSource) { + this.engine = new EngineHttpClient(engineBaseUrl); + this.jdbc = new JdbcResultReader(dataSource); + } + + /** 对一个实体跑一次自对拍。 */ + public DiffReport check(String table) { + String t = table.toUpperCase(); + List> left; + List> right; + try { + left = jdbc.readAll(t); + } catch (Exception e) { + return error(t, "JDBC 直连读失败: " + e.getMessage()); + } + try { + right = engine.listAll(t); + } catch (Exception e) { + return error(t, "引擎读路径失败: " + e.getMessage()); + } + // 主键:优先 {表名}_ID 约定(EIP3 设计器/引擎一致),回退自动推断 + String pk = t + "_ID"; + List keys = (!left.isEmpty() && left.get(0).containsKey(pk)) + ? List.of(pk) + : RowDiff.inferKeyColumns(t, left.isEmpty() ? right : left); + return new RowDiff(keys).diff(t, left, right); + } + + private static DiffReport error(String table, String msg) { + DiffReport r = new DiffReport(table); + DiffReport.RowMismatch mm = new DiffReport.RowMismatch("RUNNER_ERROR"); + mm.cells.add(new DiffReport.CellDiff("error", null, msg, "VALUE")); + r.mismatches.add(mm); + return r; + } +} diff --git a/platform-parity/src/main/resources/bootstrap/bootstrap-sys.sql b/platform-parity/src/main/resources/bootstrap/bootstrap-sys.sql new file mode 100644 index 0000000..2985b9f --- /dev/null +++ b/platform-parity/src/main/resources/bootstrap/bootstrap-sys.sql @@ -0,0 +1,377 @@ +-- ============================================================================ +-- eip4-platform M0/C1 本地自举 schema —— bootstrap-sys.sql +-- ---------------------------------------------------------------------------- +-- 用途:在空 PG 库(本地 eip4m0)上重建保真层引擎(POJOService/POJOCRUDService/ +-- DataDicService)启动与运行所需的最小 SYS_* 平台表 + 自描述元数据, +-- 使引擎能在无客户库副本的情况下正常 loadInfo / listSelect / save。 +-- +-- 口径权威来源(已逐一核对源码): +-- * 元数据契约 = DataDicServiceImpl.loadInfo 的 join SQL(SYS_ENTITY ⋈ SYS_ENTITYATTRIBUTE), +-- 列序见 POInfo 构造(rs.getString(1..25))。 +-- * 物理类型 = Entity.createTableSql / processAttribute 的 DisplayType→PG 类型映射: +-- id/外键 -> character(36) +-- 字符串/CODE -> character varying(n) (n 缺省 100) +-- 整数/布尔/枚举 -> integer +-- 金额/数值 -> numeric(18,6) +-- 日期/日期时间 -> bigint (epoch 毫秒) +-- isactive -> integer DEFAULT 1 NOT NULL +-- version -> integer DEFAULT 0 NOT NULL +-- * 显示类型(REFERENCEID)取值:NVARCHAR/CHAR/INTEGER/DECIMAL/MONEY/DATE/TIME/ +-- BOOLEAN/ENUMERATE/CODE/BLOB/STATUS/LIST(见 DisplayType.java)。 +-- +-- 纪律:可重复执行(全部 DROP ... IF EXISTS 开头);表名/列名一律小写(PG 不区分大小写, +-- 引擎 SQL 大小写混用,小写建库即可被两侧命中)。本脚本只建结构 + 自描述元数据, +-- 业务测试实体与数据由 ParitySeeder / ParityCli --seed 经 REST 注入。 +-- ============================================================================ + +-- --------------------------------------------------------------------------- +-- 0. 清理(逆序 drop,先业务测试残留再平台表) +-- --------------------------------------------------------------------------- +DROP TABLE IF EXISTS sys_entityattribute CASCADE; +DROP TABLE IF EXISTS sys_entity CASCADE; +DROP TABLE IF EXISTS sys_usergroup CASCADE; +DROP TABLE IF EXISTS sys_reference CASCADE; +DROP TABLE IF EXISTS sys_referencevalue CASCADE; +DROP TABLE IF EXISTS sys_rule CASCADE; +DROP TABLE IF EXISTS sys_enumerate CASCADE; +DROP TABLE IF EXISTS sys_enumeratetype CASCADE; +DROP TABLE IF EXISTS sys_application_config CASCADE; +DROP TABLE IF EXISTS sys_datastatus CASCADE; +DROP TABLE IF EXISTS order_entity CASCADE; + +-- =========================================================================== +-- 1. 物理基表(引擎运行期会直接 SELECT/JOIN 的表,逐一建结构) +-- =========================================================================== + +-- 1.1 SYS_ENTITY —— 实体元数据主表(loadInfo 的 t 表) +CREATE TABLE sys_entity ( + sys_entity_id character(36) NOT NULL, + name character varying(100), + description character varying(255), + tablename character varying(100), + entitytype character(36), + isview integer DEFAULT 0, + ischangelog integer DEFAULT 0, + accesslevel integer DEFAULT 0, + tabletype integer DEFAULT 0, -- 0 主表 / 1 子表 / 2 中间表 + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_entity_id) +); + +-- 1.2 SYS_ENTITYATTRIBUTE —— 实体属性元数据(loadInfo 的 c 表;即白名单/类型来源) +CREATE TABLE sys_entityattribute ( + sys_entityattribute_id character(36) NOT NULL, + entityid character(36), + columnname character varying(100), + displayname character varying(100), + description character varying(255), + referenceid character varying(40), -- 显示类型或 SYS_REFERENCE 外键 + referencevalue character(36), + ismandatory integer DEFAULT 0, + isreadonly integer DEFAULT 0, + iskey integer DEFAULT 0, + isidentifier integer DEFAULT 0, + iscombidentifier integer DEFAULT 0, + isentitydisplay integer DEFAULT 0, + entitydisplayseq integer DEFAULT 0, + enumeratetype character varying(40), + defaultvalue character varying(255), + ruleid character varying(40), + fieldlength integer, + valuemin character varying(40), + valuemax character varying(40), + columnsql character varying(255), + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_entityattribute_id) +); + +-- 1.3 SYS_USERGROUP —— MetaController 克隆载体(必须有完整自描述元数据 + 物理表 + 业务数据) +CREATE TABLE sys_usergroup ( + sys_usergroup_id character(36) NOT NULL, + name character varying(100), + description character varying(255), + parentid character(36), + grouptype integer DEFAULT 0, + orderasc integer DEFAULT 0, + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_usergroup_id) +); + +-- 1.4 SYS_REFERENCE —— 引用(外键)定义 +CREATE TABLE sys_reference ( + sys_reference_id character(36) NOT NULL, + entityid character(36), + name character varying(100), + description character varying(255), + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_reference_id) +); + +-- 1.5 SYS_REFERENCEVALUE —— 引用值(列级外键映射) +CREATE TABLE sys_referencevalue ( + sys_referencevalue_id character(36) NOT NULL, + entityid character(36), + entityattributeid character(36), + name character varying(100), + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_referencevalue_id) +); + +-- 1.6 SYS_RULE —— 校验规则(getRule 查询) +CREATE TABLE sys_rule ( + sys_rule_id character(36) NOT NULL, + name character varying(100), + description character varying(255), + rulecode character varying(255), + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_rule_id) +); + +-- 1.7 SYS_ENUMERATETYPE / SYS_ENUMERATE —— 枚举字典(getEnumerateItems 查询) +CREATE TABLE sys_enumeratetype ( + sys_enumeratetype_id character(36) NOT NULL, + name character varying(100), + description character varying(255), + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_enumeratetype_id) +); + +CREATE TABLE sys_enumerate ( + sys_enumerate_id character(36) NOT NULL, + sys_enumeratetype_id character(36), + codevalue integer, + codedes character varying(255), + extendattributeone character varying(255), + extendattributetwo character varying(255), + extendattributethree character varying(255), + orderasc integer DEFAULT 0, + isactive integer DEFAULT 1 NOT NULL, + createtime bigint, + updatetime bigint, + createby character(36), + updateby character(36), + version integer DEFAULT 0 NOT NULL, + PRIMARY KEY (sys_enumerate_id) +); + +-- 1.8 SYS_APPLICATION_CONFIG —— getConfigValue 查询(主键为 varchar,非 char(36)) +CREATE TABLE sys_application_config ( + sys_application_config_id character varying(255) NOT NULL, + value character varying(4000), + PRIMARY KEY (sys_application_config_id) +); + +-- 1.9 ORDER_ENTITY —— Entity.getOrderEntity() 仅在 isDevDB()=true 时写入; +-- 本环境 InstallOrUpdate.isDevDB()=false(桩),不会写;此处仍建结构以防误触。 +CREATE TABLE order_entity ( + order_entity_id character(36) NOT NULL, + tablename character varying(100), + sys_entitytype character(36), + sequ integer, + updatetime bigint, + version integer DEFAULT 0, + updateby character(36), + PRIMARY KEY (order_entity_id) +); + +-- =========================================================================== +-- 2. 自描述元数据 —— 让六个 SYS_* 实体本身也能被引擎 loadInfo 识别 +-- ENTITYTYPE 固定 uuid;CREATEBY/UPDATEBY='ADMIN'(char(36) 自动补空格)。 +-- REFERENCEID 按 DisplayType 取值:CHAR(id) / NVARCHAR(串) / INTEGER / DATE / TIME。 +-- =========================================================================== + +-- 固定实体 id(可复现),便于属性行 ENTITYID 引用 +-- SYS_ENTITY = e1000000-0000-0000-0000-000000000001 +-- SYS_ENTITYATTRIBUTE = e1000000-0000-0000-0000-000000000002 +-- SYS_USERGROUP = e1000000-0000-0000-0000-000000000003 +-- SYS_REFERENCE = e1000000-0000-0000-0000-000000000004 +-- SYS_REFERENCEVALUE = e1000000-0000-0000-0000-000000000005 +-- SYS_RULE = e1000000-0000-0000-0000-000000000006 +-- 统一 ENTITYTYPE = a0000000-0000-0000-0000-0000000000ff + +INSERT INTO sys_entity + (sys_entity_id, name, description, tablename, entitytype, isview, ischangelog, + accesslevel, tabletype, isactive, createtime, updatetime, createby, updateby, version) +VALUES + ('e1000000-0000-0000-0000-000000000001', '实体定义', '系统实体元数据主表', 'SYS_ENTITY', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0), + ('e1000000-0000-0000-0000-000000000002', '实体属性', '系统实体属性元数据', 'SYS_ENTITYATTRIBUTE', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0), + ('e1000000-0000-0000-0000-000000000003', '用户组', '克隆载体实体', 'SYS_USERGROUP', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0), + ('e1000000-0000-0000-0000-000000000004', '引用', '外键引用定义', 'SYS_REFERENCE', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0), + ('e1000000-0000-0000-0000-000000000005', '引用值', '列级引用映射', 'SYS_REFERENCEVALUE', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0), + ('e1000000-0000-0000-0000-000000000006', '规则', '校验规则定义', 'SYS_RULE', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0), + -- SYS_ENUMERATE 必须有自描述元数据:引擎 GeneralPO.load 对 BOOLEAN/ENUMERATE/STATUS 列 + -- 每行回调 getEnumerateItems→SELECT SYS_ENUMERATE;若该表无元数据则 buildSelect 返回 null 触发 NPE。 + ('e1000000-0000-0000-0000-000000000007', '枚举项', '枚举字典项', 'SYS_ENUMERATE', 'a0000000-0000-0000-0000-0000000000ff', 0, 0, 0, 0, 1, 0, 0, 'ADMIN', 'ADMIN', 0); + +-- 属性行生成函数(本脚本内联):用确定性 uuid(实体号 + 序号)保证可重复。 +-- 这里直接显式插入,序号即 entitydisplayseq。 + +-- --- SYS_ENTITY 的自描述属性 --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000101', 'e1000000-0000-0000-0000-000000000001', 'SYS_ENTITY_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000102', 'e1000000-0000-0000-0000-000000000001', 'NAME', '名称', 'NVARCHAR', 1, 0, 0, 0, 0, 1, 2, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000103', 'e1000000-0000-0000-0000-000000000001', 'DESCRIPTION', '描述', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 3, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000104', 'e1000000-0000-0000-0000-000000000001', 'TABLENAME', '表名', 'NVARCHAR', 1, 0, 0, 0, 0, 0, 4, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000105', 'e1000000-0000-0000-0000-000000000001', 'ENTITYTYPE', '实体类型', 'CHAR', 0, 0, 0, 0, 0, 0, 5, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000106', 'e1000000-0000-0000-0000-000000000001', 'ISVIEW', '是否视图', 'INTEGER', 0, 0, 0, 0, 0, 0, 6, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000107', 'e1000000-0000-0000-0000-000000000001', 'ISCHANGELOG', '变更日志', 'INTEGER', 0, 0, 0, 0, 0, 0, 7, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000108', 'e1000000-0000-0000-0000-000000000001', 'ACCESSLEVEL', '访问级别', 'INTEGER', 0, 0, 0, 0, 0, 0, 8, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000109', 'e1000000-0000-0000-0000-000000000001', 'TABLETYPE', '表类型', 'INTEGER', 0, 0, 0, 0, 0, 0, 9, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000010a', 'e1000000-0000-0000-0000-000000000001', 'ISACTIVE', '是否有效', 'BOOLEAN', 0, 0, 0, 0, 0, 0, 10, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000010b', 'e1000000-0000-0000-0000-000000000001', 'CREATETIME', '创建时间', 'TIME', 0, 0, 0, 0, 0, 0, 11, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000010c', 'e1000000-0000-0000-0000-000000000001', 'UPDATETIME', '更新时间', 'TIME', 0, 0, 0, 0, 0, 0, 12, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000010d', 'e1000000-0000-0000-0000-000000000001', 'CREATEBY', '创建人', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 13, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000010e', 'e1000000-0000-0000-0000-000000000001', 'UPDATEBY', '更新人', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 14, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000010f', 'e1000000-0000-0000-0000-000000000001', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 15, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- --- SYS_ENTITYATTRIBUTE 的自描述属性(只描述其关键列,够 loadInfo + listSelect 使用) --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000201', 'e1000000-0000-0000-0000-000000000002', 'SYS_ENTITYATTRIBUTE_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000202', 'e1000000-0000-0000-0000-000000000002', 'ENTITYID', '实体ID', 'CHAR', 1, 0, 0, 0, 0, 0, 2, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000203', 'e1000000-0000-0000-0000-000000000002', 'COLUMNNAME', '列名', 'NVARCHAR', 1, 0, 0, 0, 0, 1, 3, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000204', 'e1000000-0000-0000-0000-000000000002', 'DISPLAYNAME', '显示名', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 4, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000205', 'e1000000-0000-0000-0000-000000000002', 'DESCRIPTION', '描述', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 5, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000206', 'e1000000-0000-0000-0000-000000000002', 'REFERENCEID', '显示类型', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 6, 40, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000207', 'e1000000-0000-0000-0000-000000000002', 'REFERENCEVALUE', '引用值', 'CHAR', 0, 0, 0, 0, 0, 0, 7, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000208', 'e1000000-0000-0000-0000-000000000002', 'ISMANDATORY', '必填', 'INTEGER', 0, 0, 0, 0, 0, 0, 8, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000209', 'e1000000-0000-0000-0000-000000000002', 'ISREADONLY', '只读', 'INTEGER', 0, 0, 0, 0, 0, 0, 9, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000020a', 'e1000000-0000-0000-0000-000000000002', 'ISKEY', '主键标志', 'INTEGER', 0, 0, 0, 0, 0, 0, 10, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000020b', 'e1000000-0000-0000-0000-000000000002', 'ISIDENTIFIER', '唯一', 'INTEGER', 0, 0, 0, 0, 0, 0, 11, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000020c', 'e1000000-0000-0000-0000-000000000002', 'ISCOMBIDENTIFIER', '组合唯一', 'INTEGER', 0, 0, 0, 0, 0, 0, 12, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000020d', 'e1000000-0000-0000-0000-000000000002', 'ISENTITYDISPLAY', '显示列', 'INTEGER', 0, 0, 0, 0, 0, 0, 13, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000020e', 'e1000000-0000-0000-0000-000000000002', 'ENTITYDISPLAYSEQ', '显示序', 'INTEGER', 0, 0, 0, 0, 0, 0, 14, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000020f', 'e1000000-0000-0000-0000-000000000002', 'FIELDLENGTH', '长度', 'INTEGER', 0, 0, 0, 0, 0, 0, 15, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000210', 'e1000000-0000-0000-0000-000000000002', 'ISACTIVE', '是否有效', 'BOOLEAN', 0, 0, 0, 0, 0, 0, 16, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000211', 'e1000000-0000-0000-0000-000000000002', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 17, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- --- SYS_USERGROUP 的自描述属性(11 业务属性 + 主键,共完整覆盖物理列) --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000301', 'e1000000-0000-0000-0000-000000000003', 'SYS_USERGROUP_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000302', 'e1000000-0000-0000-0000-000000000003', 'NAME', '名称', 'NVARCHAR', 1, 0, 0, 0, 0, 1, 2, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000303', 'e1000000-0000-0000-0000-000000000003', 'DESCRIPTION', '描述', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 3, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000304', 'e1000000-0000-0000-0000-000000000003', 'PARENTID', '上级', 'CHAR', 0, 0, 0, 0, 0, 0, 4, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000305', 'e1000000-0000-0000-0000-000000000003', 'GROUPTYPE', '组类型', 'INTEGER', 0, 0, 0, 0, 0, 0, 5, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000306', 'e1000000-0000-0000-0000-000000000003', 'ORDERASC', '排序', 'INTEGER', 0, 0, 0, 0, 0, 0, 6, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000307', 'e1000000-0000-0000-0000-000000000003', 'ISACTIVE', '是否有效', 'BOOLEAN', 0, 0, 0, 0, 0, 0, 7, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000308', 'e1000000-0000-0000-0000-000000000003', 'CREATETIME', '创建时间', 'TIME', 0, 0, 0, 0, 0, 0, 8, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000309', 'e1000000-0000-0000-0000-000000000003', 'UPDATETIME', '更新时间', 'TIME', 0, 0, 0, 0, 0, 0, 9, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000030a', 'e1000000-0000-0000-0000-000000000003', 'CREATEBY', '创建人', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 10, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000030b', 'e1000000-0000-0000-0000-000000000003', 'UPDATEBY', '更新人', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 11, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000030c', 'e1000000-0000-0000-0000-000000000003', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 12, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- --- SYS_REFERENCE 的自描述属性 --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000401', 'e1000000-0000-0000-0000-000000000004', 'SYS_REFERENCE_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000402', 'e1000000-0000-0000-0000-000000000004', 'ENTITYID', '实体ID', 'CHAR', 0, 0, 0, 0, 0, 0, 2, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000403', 'e1000000-0000-0000-0000-000000000004', 'NAME', '名称', 'NVARCHAR', 0, 0, 0, 0, 0, 1, 3, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000404', 'e1000000-0000-0000-0000-000000000004', 'DESCRIPTION', '描述', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 4, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000405', 'e1000000-0000-0000-0000-000000000004', 'ISACTIVE', '是否有效', 'BOOLEAN', 0, 0, 0, 0, 0, 0, 5, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000406', 'e1000000-0000-0000-0000-000000000004', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 6, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- --- SYS_REFERENCEVALUE 的自描述属性 --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000501', 'e1000000-0000-0000-0000-000000000005', 'SYS_REFERENCEVALUE_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000502', 'e1000000-0000-0000-0000-000000000005', 'ENTITYID', '实体ID', 'CHAR', 0, 0, 0, 0, 0, 0, 2, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000503', 'e1000000-0000-0000-0000-000000000005', 'ENTITYATTRIBUTEID', '属性ID', 'CHAR', 0, 0, 0, 0, 0, 0, 3, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000504', 'e1000000-0000-0000-0000-000000000005', 'NAME', '名称', 'NVARCHAR', 0, 0, 0, 0, 0, 1, 4, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000505', 'e1000000-0000-0000-0000-000000000005', 'ISACTIVE', '是否有效','BOOLEAN', 0, 0, 0, 0, 0, 0, 5, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000506', 'e1000000-0000-0000-0000-000000000005', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 6, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- --- SYS_RULE 的自描述属性 --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000601', 'e1000000-0000-0000-0000-000000000006', 'SYS_RULE_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000602', 'e1000000-0000-0000-0000-000000000006', 'NAME', '名称', 'NVARCHAR', 0, 0, 0, 0, 0, 1, 2, 100, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000603', 'e1000000-0000-0000-0000-000000000006', 'DESCRIPTION', '描述', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 3, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000604', 'e1000000-0000-0000-0000-000000000006', 'RULECODE', '规则代码', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 4, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000605', 'e1000000-0000-0000-0000-000000000006', 'ISACTIVE', '是否有效', 'BOOLEAN', 0, 0, 0, 0, 0, 0, 5, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000606', 'e1000000-0000-0000-0000-000000000006', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 6, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- --- SYS_ENUMERATE 的自描述属性(CODEVALUE 必须 INTEGER;getEnumerateItems 取这五列) --- +INSERT INTO sys_entityattribute + (sys_entityattribute_id, entityid, columnname, displayname, referenceid, ismandatory, isreadonly, + iskey, isidentifier, iscombidentifier, isentitydisplay, entitydisplayseq, fieldlength, + isactive, createby, updateby, version) +VALUES + ('a1000000-0000-0000-0000-000000000701', 'e1000000-0000-0000-0000-000000000007', 'SYS_ENUMERATE_ID', '主键', 'CHAR', 1, 1, 1, 0, 0, 0, 1, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000702', 'e1000000-0000-0000-0000-000000000007', 'SYS_ENUMERATETYPE_ID', '类型', 'CHAR', 0, 0, 0, 0, 0, 0, 2, 36, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000703', 'e1000000-0000-0000-0000-000000000007', 'CODEVALUE', '编码值', 'INTEGER', 0, 0, 0, 0, 0, 0, 3, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000704', 'e1000000-0000-0000-0000-000000000007', 'CODEDES', '编码描述', 'NVARCHAR', 0, 0, 0, 0, 0, 1, 4, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000705', 'e1000000-0000-0000-0000-000000000007', 'EXTENDATTRIBUTEONE', '扩展1', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 5, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000706', 'e1000000-0000-0000-0000-000000000007', 'EXTENDATTRIBUTETWO', '扩展2', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 6, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000707', 'e1000000-0000-0000-0000-000000000007', 'EXTENDATTRIBUTETHREE', '扩展3', 'NVARCHAR', 0, 0, 0, 0, 0, 0, 7, 255, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000708', 'e1000000-0000-0000-0000-000000000007', 'ORDERASC', '排序', 'INTEGER', 0, 0, 0, 0, 0, 0, 8, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-000000000709', 'e1000000-0000-0000-0000-000000000007', 'ISACTIVE', '是否有效', 'BOOLEAN', 0, 0, 0, 0, 0, 0, 9, 0, 1, 'ADMIN', 'ADMIN', 0), + ('a1000000-0000-0000-0000-00000000070a', 'e1000000-0000-0000-0000-000000000007', 'VERSION', '版本', 'INTEGER', 0, 0, 0, 0, 0, 0, 10, 0, 1, 'ADMIN', 'ADMIN', 0); + +-- =========================================================================== +-- 3. 业务数据种子(满足"SYS_USERGROUP 至少插入 1 行业务数据") +-- =========================================================================== +INSERT INTO sys_usergroup + (sys_usergroup_id, name, description, grouptype, orderasc, isactive, createtime, updatetime, createby, updateby, version) +VALUES + ('b0000000-0000-0000-0000-000000000001', '系统管理员组', 'bootstrap 种子用户组', 0, 1, 1, 0, 0, 'ADMIN', 'ADMIN', 0); + +-- =========================================================================== +-- 4. 应用配置(getConfigValue 兜底;引擎个别路径会读 COMPANY 等键,给空值防 NPE) +-- =========================================================================== +INSERT INTO sys_application_config (sys_application_config_id, value) VALUES + ('com.dsasp.server.organize.COMPANY', ''); diff --git a/platform-parity/src/main/resources/recorder/JsonOut.java.txt b/platform-parity/src/main/resources/recorder/JsonOut.java.txt new file mode 100644 index 0000000..af97d1f --- /dev/null +++ b/platform-parity/src/main/resources/recorder/JsonOut.java.txt @@ -0,0 +1,117 @@ +/* + * JsonOut —— 极简 JSON 串行化(JDK6 兼容,无第三方依赖,交付物)。 + * 仅覆盖 SqlRecord 录制所需类型(String/Number/Boolean/null/List/Map)。 + * 用途:旧系统若无 Jackson/Gson,可用本类把 SqlRecord 写成单行 JSON(JSONL)。 + * 若旧系统已有 JSON 库,直接替换本类即可,字段名保持不变。 + */ +package com.dsasp.parity.recorder; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public final class JsonOut { + + private JsonOut() { + } + + public static String toJson(SqlRecord r) { + StringBuilder sb = new StringBuilder(); + sb.append('{'); + field(sb, "op", r.op, true); + field(sb, "sql", r.sql, false); + sb.append(",\"params\":"); + array(sb, r.params); + sb.append(",\"columns\":"); + arrayStr(sb, r.columns); + sb.append(",\"rows\":"); + rows(sb, r.rows); + sb.append(",\"affectedRows\":").append(r.affectedRows == null ? "null" : r.affectedRows.toString()); + sb.append(",\"ts\":").append(r.ts == null ? "null" : r.ts.toString()); + sb.append(",\"entity\":"); + value(sb, r.entity); + sb.append('}'); + return sb.toString(); + } + + private static void field(StringBuilder sb, String key, String val, boolean first) { + if (!first) { + sb.append(','); + } + sb.append('"').append(key).append("\":"); + value(sb, val); + } + + @SuppressWarnings("rawtypes") + private static void rows(StringBuilder sb, List> rows) { + sb.append('['); + for (int i = 0; i < rows.size(); i++) { + if (i > 0) { + sb.append(','); + } + Map m = rows.get(i); + sb.append('{'); + Iterator it = m.entrySet().iterator(); + boolean f = true; + while (it.hasNext()) { + Map.Entry e = (Map.Entry) it.next(); + if (!f) { + sb.append(','); + } + f = false; + sb.append('"').append(esc(String.valueOf(e.getKey()))).append("\":"); + value(sb, e.getValue()); + } + sb.append('}'); + } + sb.append(']'); + } + + private static void array(StringBuilder sb, List list) { + sb.append('['); + for (int i = 0; i < list.size(); i++) { + if (i > 0) { + sb.append(','); + } + value(sb, list.get(i)); + } + sb.append(']'); + } + + private static void arrayStr(StringBuilder sb, List list) { + sb.append('['); + for (int i = 0; i < list.size(); i++) { + if (i > 0) { + sb.append(','); + } + value(sb, list.get(i)); + } + sb.append(']'); + } + + private static void value(StringBuilder sb, Object v) { + if (v == null) { + sb.append("null"); + } else if (v instanceof Number || v instanceof Boolean) { + sb.append(v.toString()); + } else { + sb.append('"').append(esc(v.toString())).append('"'); + } + } + + private static String esc(String s) { + StringBuilder b = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + switch (c) { + case '"': b.append("\\\""); break; + case '\\': b.append("\\\\"); break; + case '\n': b.append("\\n"); break; + case '\r': b.append("\\r"); break; + case '\t': b.append("\\t"); break; + default: b.append(c); + } + } + return b.toString(); + } +} diff --git a/platform-parity/src/main/resources/recorder/SqlRecord.java.txt b/platform-parity/src/main/resources/recorder/SqlRecord.java.txt new file mode 100644 index 0000000..9306242 --- /dev/null +++ b/platform-parity/src/main/resources/recorder/SqlRecord.java.txt @@ -0,0 +1,31 @@ +/* + * SqlRecord —— 旧系统侧录制单元(JDK6 兼容,交付物,非本工程编译单元)。 + * 字段与新工程 com.eip4.platform.parity.replay.SqlRecord 完全对齐, + * 保证 JSONL 在录制端(旧系统)与重放端(新引擎)之间无损往返。 + */ +package com.dsasp.parity.recorder; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class SqlRecord { + + public String op; // query / update / execute + public String sql; + public List params = new ArrayList(); + public List columns = new ArrayList(); + public List> rows = new ArrayList>(); + public Long affectedRows; + public Long ts; + public String entity; + + public SqlRecord() { + } + + public SqlRecord(String op, String sql) { + this.op = op; + this.sql = sql; + } +} diff --git a/platform-parity/src/main/resources/recorder/SqlRecordingAspect.java.txt b/platform-parity/src/main/resources/recorder/SqlRecordingAspect.java.txt new file mode 100644 index 0000000..dfe7fbd --- /dev/null +++ b/platform-parity/src/main/resources/recorder/SqlRecordingAspect.java.txt @@ -0,0 +1,219 @@ +/* + * ============================================================================ + * SqlRecordingAspect —— 旧系统侧 SQL 录制切面(交付物,非本工程编译单元) + * ---------------------------------------------------------------------------- + * 部署目标:正在运行的旧系统(Spring 3.1 + JDK 6 + OSGi)。因旧系统环境本机不可得, + * 本文件以「源码 + XML 配置」形式交付,在旧系统侧编译部署。 + * + * 作用:拦截旧系统 JdbcTemplate 的 query* / update / execute 调用,把 + * SQL 文本 + 参数 + 结果集(行列值)逐条写成 JSONL,作为 C1 逐行对拍的 + * "基线侧"录制流量。重放端见 com.eip4.platform.parity.replay.ReplayRunner, + * diff 内核见 com.eip4.platform.parity.diff.RowDiff。 + * + * 约束(适配旧栈): + * - 仅用 JDK 6 + Spring 3.1 AOP(无 lambda、无 try-with-resources、无 java.time); + * - 无第三方 JSON 依赖时,手写极简 JSON 串行化(见 JsonOut),避免给旧系统引依赖; + * - 切面只读不改:绝不修改 SQL、参数或返回值,零侵入旧行为; + * - 输出 JSONL 与 SqlRecord 字段对齐(op/sql/params/columns/rows/affectedRows/ts/entity)。 + * + * 装配:用 Spring 3.1 AOP XML( + ),见同目录《集成说明.md》。 + * ============================================================================ + */ +package com.dsasp.parity.recorder; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.Writer; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.util.ArrayList; +import java.util.List; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.springframework.jdbc.core.RowMapper; + +/** + * 环绕通知:对 JdbcTemplate 的查询/更新/执行方法录制。 + * 用 around advice 包住目标方法:query* 录结果集,update/execute 录影响行数。 + */ +public class SqlRecordingAspect { + + /** 录制文件路径(JSONL,每行一条),由 XML setter 注入。 */ + private String outputFile = "/var/log/eip-legacy/sql-record.jsonl"; + + /** 同一进程并发写需同步(旧系统 JdbcTemplate 多线程)。 */ + private final Object writeLock = new Object(); + + public void setOutputFile(String outputFile) { + this.outputFile = outputFile; + } + + /** + * 环绕 JdbcTemplate.query*(...) —— 录 SQL + 参数 + 结果集行列值。 + * 切点表达式见 XML;此方法签名匹配 around advice。 + */ + public Object aroundQuery(ProceedingJoinPoint pjp) throws Throwable { + long start = System.currentTimeMillis(); + Object[] args = pjp.getArgs(); + String sql = firstString(args); + Object result = pjp.proceed(); // 不改返回值 + try { + SqlRecord rec = new SqlRecord("query", sql); + rec.ts = start; + rec.entity = guessEntity(sql); + extractParams(args, rec.params); + // result 可能是 List / List / SqlRowSet,按类型尽力转录 + captureRows(result, rec); + write(rec); + } catch (Throwable recErr) { + // 录制失败绝不影响业务:吞掉并继续 + System.err.println("[SqlRecordingAspect] record query failed: " + recErr.getMessage()); + } + return result; + } + + /** 环绕 JdbcTemplate.update(...) / execute(...) —— 录 SQL + 参数 + 影响行数。 */ + public Object aroundWrite(ProceedingJoinPoint pjp) throws Throwable { + long start = System.currentTimeMillis(); + Object[] args = pjp.getArgs(); + String sql = firstString(args); + Object result = pjp.proceed(); + try { + String op = pjp.getSignature().getName().startsWith("execute") ? "execute" : "update"; + SqlRecord rec = new SqlRecord(op, sql); + rec.ts = start; + rec.entity = guessEntity(sql); + extractParams(args, rec.params); + if (result instanceof Integer) { + rec.affectedRows = ((Integer) result).longValue(); + } + write(rec); + } catch (Throwable recErr) { + System.err.println("[SqlRecordingAspect] record write failed: " + recErr.getMessage()); + } + return result; + } + + // ---- 结果集转录(尽力按运行期类型)---- + + @SuppressWarnings({"unchecked", "rawtypes"}) + private void captureRows(Object result, SqlRecord rec) { + if (result instanceof List) { + List list = (List) result; + for (int i = 0; i < list.size(); i++) { + Object row = list.get(i); + if (row instanceof java.util.Map) { + java.util.Map m = (java.util.Map) row; + if (rec.columns.isEmpty()) { + rec.columns.addAll(upper(m.keySet())); + } + java.util.Map norm = new java.util.LinkedHashMap(); + java.util.Iterator it = m.entrySet().iterator(); + while (it.hasNext()) { + java.util.Map.Entry e = (java.util.Map.Entry) it.next(); + norm.put(String.valueOf(e.getKey()).toUpperCase(), e.getValue()); + } + rec.rows.add(norm); + } + } + } + // 若旧系统返回 SqlRowSet,可在此分支用 SqlRowSetMetaData 取列后逐行 getObject; + // 旧系统 POJOService 已物化为 List,推荐在更高层(POJOService.listSelect + // 返回值)挂另一切面,把 GeneralPOJO.toRow() 平面 map 录入,语义与新引擎 EntityController 完全对齐。 + } + + /** RowMapper 形式查询的列名兜底(部分 query 重载传 RowMapper)。 */ + private List mapperColumns(RowMapper mapper) { + return new ArrayList(); + } + + // ---- 工具 ---- + + private static String firstString(Object[] args) { + if (args != null) { + for (int i = 0; i < args.length; i++) { + if (args[i] instanceof String) { + return (String) args[i]; + } + } + } + return null; + } + + private static void extractParams(Object[] args, List out) { + if (args == null) { + return; + } + for (int i = 0; i < args.length; i++) { + Object a = args[i]; + if (a instanceof Object[]) { + Object[] arr = (Object[]) a; + for (int j = 0; j < arr.length; j++) { + out.add(arr[j]); + } + } else if (a != null && !(a instanceof String) && !(a instanceof RowMapper) + && !(a instanceof Class)) { + // 跳过 SQL 串、RowMapper、返回类型;其余视为绑定参数 + out.add(a); + } + } + } + + /** 从 SQL 粗解析主表名(FROM xxx),仅用于报告分组,非语义关键。 */ + private static String guessEntity(String sql) { + if (sql == null) { + return null; + } + String s = sql.toUpperCase(); + int from = s.indexOf(" FROM "); + if (from < 0) { + int into = s.indexOf(" INTO "); + int upd = s.indexOf("UPDATE "); + if (into >= 0) { + return token(s, into + 6); + } + if (upd >= 0) { + return token(s, upd + 7); + } + return null; + } + return token(s, from + 6); + } + + private static String token(String s, int start) { + int i = start; + while (i < s.length() && s.charAt(i) == ' ') { + i++; + } + int j = i; + while (j < s.length() && (Character.isLetterOrDigit(s.charAt(j)) || s.charAt(j) == '_')) { + j++; + } + return j > i ? s.substring(i, j) : null; + } + + private static List upper(java.util.Set keys) { + List out = new ArrayList(); + java.util.Iterator it = keys.iterator(); + while (it.hasNext()) { + out.add(it.next().toUpperCase()); + } + return out; + } + + private void write(SqlRecord rec) throws Exception { + String line = JsonOut.toJson(rec); + synchronized (writeLock) { + Writer w = null; + try { + w = new BufferedWriter(new FileWriter(outputFile, true)); + w.write(line); + w.write("\n"); + } finally { + if (w != null) { + w.close(); + } + } + } + } +} diff --git a/platform-parity/src/main/resources/recorder/集成说明.md b/platform-parity/src/main/resources/recorder/集成说明.md new file mode 100644 index 0000000..ef44d12 --- /dev/null +++ b/platform-parity/src/main/resources/recorder/集成说明.md @@ -0,0 +1,103 @@ +# 旧系统侧 SQL 录制切面 —— 集成说明 + +> 交付物:`SqlRecordingAspect.java.txt` + `SqlRecord.java.txt` + `JsonOut.java.txt`。 +> 因旧系统(Spring 3.1 / JDK 6 / OSGi)运行环境本机不可得,以源码形式交付,在旧系统侧编译部署。 +> 录制产物(JSONL)喂给新工程 `com.eip4.platform.parity.replay.ReplayRunner` 做逐行对拍。 + +## 一、定位 + +逐行对拍需要一个"基线侧"真值流量。最高保真的基线是**旧系统真实执行的 SQL 与其结果集**。 +本切面在旧系统 `JdbcTemplate` 调用处旁路录制(只读不改),产出与新引擎可对齐的 JSONL。 + +录制端字段 ↔ 重放端 `SqlRecord` 完全一致:`op / sql / params / columns / rows / affectedRows / ts / entity`。 + +## 二、编译与放置 + +1. 将三个 `.java.txt` 去掉 `.txt` 后缀,放入旧系统某个能被其类加载器看到的 bundle/源码目录, + 包名 `com.dsasp.parity.recorder`(与旧系统 `com.dsasp.*` 同根,避免 OSGi 包可见性问题)。 +2. 旧系统若已有 JSON 库(Jackson/Gson),可删除 `JsonOut`,在 `SqlRecordingAspect.write()` + 改用既有库;字段名不要改。 +3. 依赖:仅 `spring-aop` + `aspectjweaver`(Spring 3.1 自带),无新增第三方依赖。 + +## 三、Spring 3.1 AOP XML 装配 + +在旧系统装配 `JdbcTemplate` 的 Spring XML(通常是 dataDic bundle 的 `service.xml` 或 +`applicationContext.xml`)中加入: + +```xml + + + + + + + + + + + + + + + +``` + +> 提示:旧系统的 `JdbcTemplate` 多为 prototype/单例混用;若 AOP 织入不到(final 方法/CGLIB +> 限制),改在更高层 `com.dsasp.server.dataDic.service.POJOService.listSelect(..)` 返回处挂切面, +> 录 `GeneralPOJO.toRow()` 平面 map —— 这一层与新引擎 `EntityController.toRow()` 语义逐字节对齐, +> 是更稳的对拍点(推荐)。 + +## 四、推荐录制点(POJOService 层,优于裸 JdbcTemplate) + +新引擎读路径出口是 `POJOCRUDService.listSelect → GeneralPOJO.toRow()`(平面 map)。 +在旧系统对**同一出口**挂切面,两侧录制的 map 结构天然一致,无需关心底层 SQL 文本差异: + +```xml + + +``` + +`aroundQuery` 的 `captureRows` 已对 `List` 做转录;若返回 `List`, +在旧系统侧加一行:对每个 `GeneralPOJO` 调 `toRow()`/逐列 `get_Value(i)` 展平后入 `rec.rows` +(新引擎 `EntityController.toRow()` 即此法)。 + +## 五、生产流量旁路录制方案(C1 报批备选) + +直接在生产 JVM 织入切面有性能与稳定性顾虑(写盘 IO、并发锁)。报批时提供两档方案: + +### 方案 A:进程内异步旁路(轻量,默认推荐) +- 切面只把 `SqlRecord` 投递到**有界队列**(`ArrayBlockingQueue`,满则丢弃并计数), + 单独后台线程批量刷盘。业务线程零阻塞,丢弃率进监控。 +- 采样:按 `entity` 或 SQL 指纹哈希取模采样(如 1/100),控制录制量。 +- 开关:`outputFile` 为空或系统属性 `eip.sql.record=off` 时整体短路,生产可热关。 + +### 方案 B:旁路镜像库重放(零生产侵入,稳妥) +- 不在生产织入。用 PG 逻辑复制/`pg_dump` 把生产副本同步到镜像库, + 在镜像库前置一个开了切面的旧系统实例,用**回放生产访问日志/慢查询日志**驱动其执行, + 在镜像侧录制。生产环境完全不动。 +- 代价:需要一份旧系统可运行镜像 + 访问日志;保真度略低于 A(时序/会话态差异)。 + +### 报批决策建议 +- **对拍验收期(M0/P1)**:用方案 B(镜像)跑全量历史 SQL 集 + 方案 A 采样补充热点路径, + 既不碰生产又拿到高覆盖基线。 +- **切流灰度期**:方案 A 低采样常开,作为线上"对拍探针",新旧路由分发时实时比对差异告警 + (对接架构文档第四节"检测兜底"层 + Nginx 流量阀门)。 +- **统一禁用**:任何环境通过系统属性 `eip.sql.record=off` 一键关断,确保可回退。 + +## 六、与重放端对接 + +录制产出 `sql-record.jsonl` 拷到新环境后: + +```bash +# 重放(写操作默认 dry-run,不改对拍目标库) +java -cp platform-parity.jar com.eip4.platform.parity.replay.ReplayRunner \ + # 经程序调用;或在 selfcheck CLI 里组合 +``` + +`ReplayRunner` 对每条 query 在新引擎数据源执行同 SQL,把结果集与录制 `rows` 过 +`RowDiff`(同一归一化规则:char(36) 尾空格 / numeric scale / bigint 时间戳 / NULL-vs-缺列)。 +差异输出结构化报告(JSON + Markdown)。