fix(meta): 引用列 DDL 外键指向目标表而非自表(DECISION-1)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -144,7 +144,7 @@ public class MetaController {
|
|||||||
|
|
||||||
// 动态建表前先清刚保存实体/属性带来的 POInfo 缓存,保证后续读写立即拿到新字段。
|
// 动态建表前先清刚保存实体/属性带来的 POInfo 缓存,保证后续读写立即拿到新字段。
|
||||||
cacheInvalidator.evictByEntityId(entityId, table);
|
cacheInvalidator.evictByEntityId(entityId, table);
|
||||||
String ddl = createTableDdl(table, created);
|
String ddl = createTableDdl(table, created, refTargets);
|
||||||
dataDicService.executeCreateSql(ddl);
|
dataDicService.executeCreateSql(ddl);
|
||||||
|
|
||||||
// P1-5:建表后按元数据补索引(外键 uuid 列 + UPDATETIME),引擎只建表不建索引
|
// P1-5:建表后按元数据补索引(外键 uuid 列 + UPDATETIME),引擎只建表不建索引
|
||||||
@@ -332,17 +332,18 @@ public class MetaController {
|
|||||||
return refId;
|
return refId;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String createTableDdl(String table, List<GeneralPOJO> attrs) {
|
static String createTableDdl(String table, List<GeneralPOJO> attrs, Map<String, String> refTargets) {
|
||||||
if (attrs == null || attrs.isEmpty()) {
|
if (attrs == null || attrs.isEmpty()) {
|
||||||
throw new IllegalStateException("实体属性为空,无法建表: " + table);
|
throw new IllegalStateException("实体属性为空,无法建表: " + table);
|
||||||
}
|
}
|
||||||
|
Map<String, String> refs = refTargets == null ? Map.of() : refTargets;
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
StringBuilder unique = new StringBuilder();
|
StringBuilder unique = new StringBuilder();
|
||||||
StringBuilder combUnique = new StringBuilder();
|
StringBuilder combUnique = new StringBuilder();
|
||||||
sql.append("CREATE TABLE ").append(table).append("\n");
|
sql.append("CREATE TABLE ").append(table).append("\n");
|
||||||
sql.append("(\n");
|
sql.append("(\n");
|
||||||
for (GeneralPOJO attr : attrs) {
|
for (GeneralPOJO attr : attrs) {
|
||||||
appendColumnDdl(table, attr, sql, unique, combUnique);
|
appendColumnDdl(table, attr, sql, unique, combUnique, refs);
|
||||||
}
|
}
|
||||||
sql.append(unique);
|
sql.append(unique);
|
||||||
if (!combUnique.isEmpty()) {
|
if (!combUnique.isEmpty()) {
|
||||||
@@ -354,7 +355,8 @@ public class MetaController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void appendColumnDdl(String table, GeneralPOJO attr, StringBuilder sql,
|
private static void appendColumnDdl(String table, GeneralPOJO attr, StringBuilder sql,
|
||||||
StringBuilder unique, StringBuilder combUnique) {
|
StringBuilder unique, StringBuilder combUnique,
|
||||||
|
Map<String, String> refTargets) {
|
||||||
if (attr.get_ValueAsBoolean("ISREADONLY")) {
|
if (attr.get_ValueAsBoolean("ISREADONLY")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -380,7 +382,9 @@ public class MetaController {
|
|||||||
} else if ("CHAR".equals(ref)) {
|
} else if ("CHAR".equals(ref)) {
|
||||||
sql.append(col).append(" character(36) ");
|
sql.append(col).append(" character(36) ");
|
||||||
} else {
|
} else {
|
||||||
sql.append(col).append(" character(36) REFERENCES ").append(table);
|
String target = refTargets.get(col);
|
||||||
|
sql.append(col).append(" character(36) REFERENCES ")
|
||||||
|
.append(target != null && !target.isBlank() ? target : table);
|
||||||
}
|
}
|
||||||
if (attr.get_ValueAsBoolean("ISMANDATORY")) {
|
if (attr.get_ValueAsBoolean("ISMANDATORY")) {
|
||||||
sql.append(" NOT NULL ");
|
sql.append(" NOT NULL ");
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package com.eip4.platform.metadata.web;
|
package com.eip4.platform.metadata.web;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -20,7 +22,7 @@ class MetaControllerDdlTest {
|
|||||||
attr("EIP4AI_RT_DEMO_ID", "CHAR", null, true),
|
attr("EIP4AI_RT_DEMO_ID", "CHAR", null, true),
|
||||||
attr("NAME", "NVARCHAR", 100, true),
|
attr("NAME", "NVARCHAR", 100, true),
|
||||||
attr("ENABLED", "BOOLEAN", null, false)
|
attr("ENABLED", "BOOLEAN", null, false)
|
||||||
));
|
), Map.of());
|
||||||
|
|
||||||
assertTrue(ddl.contains("CREATE TABLE EIP4AI_RT_DEMO"));
|
assertTrue(ddl.contains("CREATE TABLE EIP4AI_RT_DEMO"));
|
||||||
assertTrue(ddl.contains("EIP4AI_RT_DEMO_ID character(36) NOT NULL"));
|
assertTrue(ddl.contains("EIP4AI_RT_DEMO_ID character(36) NOT NULL"));
|
||||||
@@ -29,6 +31,21 @@ class MetaControllerDdlTest {
|
|||||||
assertTrue(ddl.contains("primary key (EIP4AI_RT_DEMO_ID)"));
|
assertTrue(ddl.contains("primary key (EIP4AI_RT_DEMO_ID)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("引用列(36位 uuid REFERENCEID)的外键指向 refTargets 里的目标表,而非自表")
|
||||||
|
void referenceColumnPointsAtTargetTable() {
|
||||||
|
String refUuid = "11111111-1111-1111-1111-111111111111";
|
||||||
|
String ddl = MetaController.createTableDdl("EIP4_ORDER", List.of(
|
||||||
|
attr("EIP4_ORDER_ID", "CHAR", null, true),
|
||||||
|
attr("CUSTOMERID", refUuid, null, false)
|
||||||
|
), Map.of("CUSTOMERID", "EIP4_CUSTOMER"));
|
||||||
|
|
||||||
|
assertTrue(ddl.contains("CUSTOMERID character(36) REFERENCES EIP4_CUSTOMER"),
|
||||||
|
"应指向目标表 EIP4_CUSTOMER, 实际: " + ddl);
|
||||||
|
assertFalse(ddl.contains("REFERENCES EIP4_ORDER"),
|
||||||
|
"外键不得自指 EIP4_ORDER");
|
||||||
|
}
|
||||||
|
|
||||||
private static GeneralPOJO attr(String column, String referenceId, Integer length, boolean mandatory) {
|
private static GeneralPOJO attr(String column, String referenceId, Integer length, boolean mandatory) {
|
||||||
GeneralPOJO po = mock(GeneralPOJO.class);
|
GeneralPOJO po = mock(GeneralPOJO.class);
|
||||||
when(po.get_Value("COLUMNNAME")).thenReturn(column);
|
when(po.get_Value("COLUMNNAME")).thenReturn(column);
|
||||||
|
|||||||
Reference in New Issue
Block a user