fix(sidebar): preserve case-sensitive object identities

This commit is contained in:
zipg authored and GitHub committed 2026-07-27 16:52:35 +08:00
1 parent 0aced0012c
commit bd693d7b98
4 files changed
+188 -39

No files matched your search

+77 -31
View File
@@ -90,7 +90,7 @@ function makeTableTreeEntry({
if (normalizedParentName) node.partitionParentName = normalizedParentName;
return {
key: objectIdentityKey(objectType, schema, name),
key: exactObjectIdentityKey(objectType, schema, name),
objectType,
schema,
parentSchema: normalizedParentSchema,
@@ -99,7 +99,11 @@ function makeTableTreeEntry({
};
}
function objectIdentityKey(objectType: string, schema: string | undefined, name: string) {
function exactObjectIdentityKey(objectType: string, schema: string | undefined, name: string, signature = "") {
return `${objectType}\0${schema || ""}\0${name}\0${signature}`;
}
function foldedPartitionObjectLookupKey(objectType: string, schema: string | undefined, name: string) {
return `${objectType}\0${(schema || "").toLowerCase()}\0${name.toLowerCase()}`;
}
@@ -223,7 +227,7 @@ export function mergeTableInfosIntoObjects(objects: readonly ObjectInfo[], table
merged.map((obj) => {
const name = normalizeDatabaseObjectName(obj.name);
const objectSchema = obj.schema ? normalizeDatabaseObjectName(obj.schema) : schema || "";
return `${normalizeObjectType(obj.object_type)}\0${objectSchema.toLowerCase()}\0${name.toLowerCase()}`;
return exactObjectIdentityKey(normalizeObjectType(obj.object_type), objectSchema, name);
}),
);
@@ -234,11 +238,11 @@ export function mergeTableInfosIntoObjects(objects: readonly ObjectInfo[], table
if (!name) continue;
const matchingObject = objects.find((obj) => {
const objName = normalizeDatabaseObjectName(obj.name);
if (objName.toLowerCase() !== name.toLowerCase()) return false;
if (objName !== name) return false;
return normalizeObjectType(obj.object_type) === objectType;
});
const tableSchema = schema ?? (matchingObject?.schema ? normalizeDatabaseObjectName(matchingObject.schema) : undefined);
const key = `${objectType}\0${(tableSchema || "").toLowerCase()}\0${name.toLowerCase()}`;
const key = exactObjectIdentityKey(objectType, tableSchema, name);
if (seen.has(key)) {
// Table already in objects — merge comment if missing
if (matchingObject && table.comment && !matchingObject.comment) {
@@ -274,8 +278,15 @@ export function filterSimpleSidebarSupplementalObjects(objects: readonly ObjectI
function buildPartitionTree(entries: TableTreeEntry[], connectionId: string, database: string): TreeNode[] {
const orderedEntries = sortDatabaseObjectsByName(entries, (entry) => entry.node.label);
const byKey = new Map<string, TableTreeEntry>();
const uniqueByFoldedKey = new Map<string, TableTreeEntry | null>();
for (const entry of orderedEntries) {
byKey.set(entry.key, entry);
const foldedKey = foldedPartitionObjectLookupKey(entry.objectType, entry.schema, entry.node.label);
if (!uniqueByFoldedKey.has(foldedKey)) {
uniqueByFoldedKey.set(foldedKey, entry);
} else if (uniqueByFoldedKey.get(foldedKey)?.key !== entry.key) {
uniqueByFoldedKey.set(foldedKey, null);
}
}
const childrenByParent = new Map<string, TableTreeEntry[]>();
@@ -283,8 +294,8 @@ function buildPartitionTree(entries: TableTreeEntry[], connectionId: string, dat
for (const entry of orderedEntries) {
if (entry.objectType !== "TABLE" || !entry.parentName) continue;
const parentSchema = entry.parentSchema || entry.schema;
const parentKey = objectIdentityKey("TABLE", parentSchema, entry.parentName);
const parent = byKey.get(parentKey);
const parentKey = exactObjectIdentityKey("TABLE", parentSchema, entry.parentName);
const parent = byKey.get(parentKey) ?? uniqueByFoldedKey.get(foldedPartitionObjectLookupKey("TABLE", parentSchema, entry.parentName));
if (!parent || parent.key === entry.key) continue;
const children = childrenByParent.get(parent.key) ?? [];
children.push(entry);
@@ -345,16 +356,32 @@ export type TableTreeLoadMoreParent = {
};
function findTableTreeNode(nodes: readonly TreeNode[], parent: TableTreeLoadMoreParent): TreeNode | undefined {
const candidates: TreeNode[] = [];
for (const node of nodes) {
const sameSchema = !parent.schema || (node.schema || "").toLowerCase() === parent.schema.toLowerCase();
if (node.type === "table" && sameSchema && node.label.toLowerCase() === parent.name.toLowerCase()) return node;
const child = findTableTreeNode(node.children ?? [], parent);
if (child) return child;
const hiddenChild = findTableTreeNode(node.hiddenChildren?.filter((item) => !(node.children ?? []).includes(item)) ?? [], parent);
if (hiddenChild) return hiddenChild;
if (node.type === "table") candidates.push(node);
candidates.push(...collectTableTreeNodes(node.children ?? []));
candidates.push(...collectTableTreeNodes(node.hiddenChildren?.filter((item) => !(node.children ?? []).includes(item)) ?? []));
}
return undefined;
const exactMatches = candidates.filter((node) => (!parent.schema || (node.schema || "") === parent.schema) && node.label === parent.name);
if (exactMatches.length === 1) return exactMatches[0];
if (exactMatches.length > 1) return undefined;
const foldedMatches = candidates.filter((node) => {
const sameSchema = !parent.schema || (node.schema || "").toLowerCase() === parent.schema.toLowerCase();
return sameSchema && node.label.toLowerCase() === parent.name.toLowerCase();
});
return foldedMatches.length === 1 ? foldedMatches[0] : undefined;
}
function collectTableTreeNodes(nodes: readonly TreeNode[]): TreeNode[] {
const tables: TreeNode[] = [];
for (const node of nodes) {
if (node.type === "table") tables.push(node);
tables.push(...collectTableTreeNodes(node.children ?? []));
tables.push(...collectTableTreeNodes(node.hiddenChildren?.filter((item) => !(node.children ?? []).includes(item)) ?? []));
}
return tables;
}
export function appendTableTreeLoadMoreNode(children: TreeNode[], loadMoreNode: TreeNode, parent?: TableTreeLoadMoreParent): TreeNode[] {
@@ -384,7 +411,7 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChil
const nodesByKey = new Map<string, TreeNode>();
const rootKeys = new Set<string>();
const nodeKey = (node: TreeNode) => objectIdentityKey("TABLE", node.schema, node.label);
const nodeKey = (node: TreeNode) => exactObjectIdentityKey("TABLE", node.schema, node.label);
const collect = (nodes: readonly TreeNode[]) => {
for (const node of nodes) {
if (node.type === "table") {
@@ -400,6 +427,32 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChil
if (node.type === "table") rootKeys.add(nodeKey(node));
}
const flattenIncomingTables = (node: TreeNode): TreeNode[] => {
if (node.type !== "table") return [node];
const descendants = partitionGroupChildren(node)
.flatMap((group) => group.children ?? [])
.flatMap(flattenIncomingTables);
node.children = (node.children ?? []).filter((child) => child.type !== "group-partitions");
node.hiddenChildren = (node.hiddenChildren ?? []).filter((child) => child.type !== "group-partitions");
return [node, ...descendants];
};
const incomingNodes = pageChildren.flatMap(flattenIncomingTables);
const allNodesByKey = new Map(nodesByKey);
for (const node of incomingNodes) {
if (node.type === "table" && !allNodesByKey.has(nodeKey(node))) {
allNodesByKey.set(nodeKey(node), node);
}
}
const uniqueByFoldedKey = new Map<string, TreeNode | null>();
for (const node of allNodesByKey.values()) {
const foldedKey = foldedPartitionObjectLookupKey("TABLE", node.schema, node.label);
if (!uniqueByFoldedKey.has(foldedKey)) {
uniqueByFoldedKey.set(foldedKey, node);
} else if (uniqueByFoldedKey.get(foldedKey) !== node) {
uniqueByFoldedKey.set(foldedKey, null);
}
}
const ensurePartitionGroup = (parent: TreeNode): TreeNode => {
const existing = partitionGroupChildren(parent)[0];
if (existing) return existing;
@@ -440,8 +493,11 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChil
const parentName = node.partitionParentName;
const parentSchema = node.partitionParentSchema || node.schema;
const parentKey = parentName ? objectIdentityKey("TABLE", parentSchema, parentName) : "";
const parent = parentKey ? nodesByKey.get(parentKey) : undefined;
let parent: TreeNode | null | undefined;
if (parentName) {
const parentKey = exactObjectIdentityKey("TABLE", parentSchema, parentName);
parent = allNodesByKey.get(parentKey) ?? uniqueByFoldedKey.get(foldedPartitionObjectLookupKey("TABLE", parentSchema, parentName));
}
nodesByKey.set(key, node);
if (parent && parent !== node) {
addToParent(parent, node);
@@ -454,17 +510,7 @@ export function mergeTableTreePageChildren(currentChildren: TreeNode[], pageChil
}
};
const flattenIncomingTables = (node: TreeNode): TreeNode[] => {
if (node.type !== "table") return [node];
const descendants = partitionGroupChildren(node)
.flatMap((group) => group.children ?? [])
.flatMap(flattenIncomingTables);
node.children = (node.children ?? []).filter((child) => child.type !== "group-partitions");
node.hiddenChildren = (node.hiddenChildren ?? []).filter((child) => child.type !== "group-partitions");
return [node, ...descendants];
};
for (const node of pageChildren.flatMap(flattenIncomingTables)) {
for (const node of incomingNodes) {
addNode(node);
}
@@ -513,7 +559,7 @@ export function buildSimpleObjectTreeNodes({ nodeId, connectionId, database, sch
const childSchema = obj.schema ? normalizeDatabaseObjectName(obj.schema) : schema;
const signature = obj.signature?.trim() || "";
const dedupeKey = `${objectType}\0${(childSchema || "").toLowerCase()}\0${name.toLowerCase()}\0${signature.toLowerCase()}`;
const dedupeKey = exactObjectIdentityKey(objectType, childSchema, name, signature);
if (seen.has(dedupeKey)) continue;
seen.add(dedupeKey);
@@ -669,7 +715,7 @@ export function buildGroupedObjectTreeNodes({ nodeId, connectionId, database, sc
const t = normalizeObjectType(obj.object_type);
const objectSchema = obj.schema ? normalizeDatabaseObjectName(obj.schema) : schema || "";
const signature = (obj.signature ?? "").trim();
const key = `${t}\0${objectSchema.toLowerCase()}\0${name.toLowerCase()}\0${signature.toLowerCase()}`;
const key = exactObjectIdentityKey(t, objectSchema, name, signature);
if (seen.has(key)) continue;
seen.add(key);
const arr = buckets.get(t) ?? [];