fix(dameng): use ALTER TABLE constraints for unique indexes
This commit is contained in:
1 parent
a19a77c6df
commit
124819e1fa
27 files changed
+466
-10
No files matched your search
@@ -1818,16 +1818,24 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
||||
}
|
||||
return unchecked(() -> {
|
||||
List<IndexInfo> result = new ArrayList<>();
|
||||
// The LEFT JOIN also matches 'U': a UNIQUE constraint owns its backing index the same
|
||||
// way a primary key does, and neither can be altered with index-level DDL (#7959).
|
||||
// The constraint type therefore drives two flags — IS_PK (only 'P') and
|
||||
// CONSTRAINT_BACKED ('P' or 'U'), the same split `independentIndexes` already uses to
|
||||
// keep constraint-backed indexes out of the generated table DDL. A unique index
|
||||
// created with CREATE UNIQUE INDEX has no ALL_CONSTRAINTS row, so it stays false and
|
||||
// keeps the index-level DDL path.
|
||||
String sql = """
|
||||
SELECT /*+ PARALLEL(1) */ i.INDEX_NAME,
|
||||
LISTAGG(ic.COLUMN_NAME, ',') WITHIN GROUP (ORDER BY ic.COLUMN_POSITION) AS COLUMNS,
|
||||
i.UNIQUENESS,
|
||||
CASE WHEN c.CONSTRAINT_TYPE = 'P' THEN 1 ELSE 0 END AS IS_PK,
|
||||
i.INDEX_TYPE
|
||||
i.INDEX_TYPE,
|
||||
CASE WHEN c.CONSTRAINT_TYPE IN ('P', 'U') THEN 1 ELSE 0 END AS CONSTRAINT_BACKED
|
||||
FROM ALL_INDEXES i
|
||||
JOIN ALL_IND_COLUMNS ic ON i.INDEX_NAME = ic.INDEX_NAME AND i.OWNER = ic.INDEX_OWNER AND i.TABLE_OWNER = ic.TABLE_OWNER
|
||||
LEFT JOIN ALL_CONSTRAINTS c ON i.INDEX_NAME = c.INDEX_NAME AND i.TABLE_OWNER = c.OWNER
|
||||
AND c.CONSTRAINT_TYPE = 'P'
|
||||
AND c.CONSTRAINT_TYPE IN ('P', 'U')
|
||||
WHERE i.TABLE_OWNER = ? AND i.TABLE_NAME = ?
|
||||
GROUP BY i.INDEX_NAME, i.UNIQUENESS, c.CONSTRAINT_TYPE, i.INDEX_TYPE
|
||||
ORDER BY i.INDEX_NAME
|
||||
@@ -1837,7 +1845,7 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(new IndexInfo(
|
||||
IndexInfo index = new IndexInfo(
|
||||
rs.getString(1),
|
||||
splitNonEmpty(coalesce(rs.getString(2)), ","),
|
||||
"UNIQUE".equals(rs.getString(3)),
|
||||
@@ -1846,7 +1854,9 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
||||
rs.getString(5),
|
||||
null,
|
||||
null
|
||||
));
|
||||
);
|
||||
index.setConstraint_backed("1".equals(rs.getString(6)));
|
||||
result.add(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.dbx.agent.dameng;
|
||||
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.DatabaseInfo;
|
||||
import com.dbx.agent.IndexInfo;
|
||||
import com.dbx.agent.MetadataListConstraints;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
@@ -81,6 +82,53 @@ class DamengAgentMetadataTest {
|
||||
Assertions.assertTrue(indexesSql.startsWith("SELECT /*+ PARALLEL(1) */"), indexesSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
void identifiesUniqueConstraintBackingIndexesInIndexMetadataQuery() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
TestSupport.setPrivateConnection(agent, JdbcMetadataSqlFake.connection());
|
||||
|
||||
agent.listIndexes("APP", "USERS");
|
||||
|
||||
String indexesSql = JdbcMetadataSqlFake.statements.stream()
|
||||
.filter(sql -> sql.contains("ALL_INDEXES") && sql.contains("ALL_CONSTRAINTS"))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
Assertions.assertTrue(indexesSql.contains("AND c.CONSTRAINT_TYPE IN ('P', 'U')"), indexesSql);
|
||||
Assertions.assertTrue(indexesSql.contains("AS CONSTRAINT_BACKED"), indexesSql);
|
||||
Assertions.assertTrue(indexesSql.contains("CASE WHEN c.CONSTRAINT_TYPE = 'P' THEN 1 ELSE 0 END AS IS_PK"), indexesSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
void marksOnlyConstraintBackedIndexesAsConstraintBacked() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
TestSupport.setPrivateConnection(agent, indexMetadataConnection(List.of(
|
||||
// UNIQUE constraint (虚索引) / standalone CREATE UNIQUE INDEX (实索引) / plain index.
|
||||
Arrays.asList("UX_USERS_EMAIL", "EMAIL", "UNIQUE", "0", "NORMAL", "1"),
|
||||
Arrays.asList("UX_USERS_CODE", "CODE", "UNIQUE", "0", "NORMAL", "0"),
|
||||
Arrays.asList("IDX_USERS_NAME", "NAME", "NONUNIQUE", "0", "NORMAL", "0"),
|
||||
Arrays.asList("PK_USERS", "ID", "UNIQUE", "1", "NORMAL", "1")
|
||||
)));
|
||||
|
||||
List<IndexInfo> indexes = agent.listIndexes("APP", "USERS");
|
||||
|
||||
Assertions.assertEquals(
|
||||
List.of("UX_USERS_EMAIL", "UX_USERS_CODE", "IDX_USERS_NAME", "PK_USERS"),
|
||||
indexes.stream().map(IndexInfo::getName).toList()
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
List.of(true, false, false, true),
|
||||
indexes.stream().map(IndexInfo::getConstraint_backed).toList()
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
List.of(false, false, false, true),
|
||||
indexes.stream().map(IndexInfo::getIs_primary).toList()
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
List.of(true, true, false, true),
|
||||
indexes.stream().map(IndexInfo::getIs_unique).toList()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void usesTableCommentsMetadataQuery() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
@@ -1428,6 +1476,24 @@ class DamengAgentMetadataTest {
|
||||
));
|
||||
}
|
||||
|
||||
// Serves every metadata query with the same positional rows; the index metadata query is
|
||||
// the only one these tests run against it.
|
||||
private static Connection indexMetadataConnection(List<List<Object>> rows) {
|
||||
return proxy(Connection.class, (method, args) -> {
|
||||
String name = method.getName();
|
||||
if ("prepareStatement".equals(name)) {
|
||||
return metadataStatement(rows);
|
||||
}
|
||||
if ("close".equals(name)) {
|
||||
return null;
|
||||
}
|
||||
if ("isClosed".equals(name)) {
|
||||
return false;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
}
|
||||
|
||||
private static List<Object> indexRow(String name, String columns, String uniqueness, String indexType) {
|
||||
return List.of(name, columns, uniqueness, indexType);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user