feat(postgres): list user-defined types with details and DDL for PG-family databases

This commit is contained in:
amwps290 authored and GitHub committed 2026-08-11 15:00:24 +08:00
1 parent 0c6c1fada2
commit 154c460657
59 files changed
+6538 -150

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { normalizeSidebarObjectKind, sidebarObjectKindsForDatabase, supportsPackageMemberExpansion } from "@/lib/database/databaseObjectCapabilities";
import { customTypeCapabilities, databaseObjectCapabilities, normalizeSidebarObjectKind, sidebarObjectKindsForDatabase, supportsPackageMemberExpansion, supportsTypeObjectSource } from "@/lib/database/databaseObjectCapabilities";
describe("databaseObjectCapabilities", () => {
it("exposes supported programmable objects for Dameng", () => {
@@ -39,4 +39,41 @@ describe("databaseObjectCapabilities", () => {
it("normalizes space separated materialized view types", () => {
expect(normalizeSidebarObjectKind("MATERIALIZED VIEW")).toBe("MATERIALIZED_VIEW");
});
it("exposes TYPE for verified PostgreSQL-family databases only", () => {
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase"] as const) {
expect(sidebarObjectKindsForDatabase(dbType), dbType).toContain("TYPE");
}
// Unverified PG-like databases must not advertise TYPE this cycle.
for (const dbType of ["highgo", "uxdb", "redshift", "kwdb"] as const) {
expect(sidebarObjectKindsForDatabase(dbType), dbType).not.toContain("TYPE");
}
});
it("only Xugu TYPE nodes can open object source", () => {
expect(supportsTypeObjectSource("xugu")).toBe(true);
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase", undefined] as const) {
expect(supportsTypeObjectSource(dbType), String(dbType)).toBe(false);
}
});
it("keeps sourceReadable in sync with supportsTypeObjectSource", () => {
expect(databaseObjectCapabilities("xugu").sourceReadable).toContain("TYPE");
expect(databaseObjectCapabilities("xugu").sourceReadable).toContain("TYPE_BODY");
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase"] as const) {
expect(databaseObjectCapabilities(dbType).sourceReadable, dbType).not.toContain("TYPE");
expect(databaseObjectCapabilities(dbType).sourceReadable, dbType).not.toContain("TYPE_BODY");
// Non-type programmable objects stay source-readable on PG-family.
expect(databaseObjectCapabilities(dbType).sourceReadable, dbType).toContain("FUNCTION");
}
});
it("enables custom type details only for verified PG-family databases", () => {
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase"] as const) {
expect(customTypeCapabilities(dbType), dbType).toEqual({ details: true, members: true, ddl: true });
}
for (const dbType of ["xugu", "highgo", "uxdb", "redshift", "mysql", undefined] as const) {
expect(customTypeCapabilities(dbType), String(dbType)).toEqual({ details: false, members: false, ddl: false });
}
});
});
@@ -0,0 +1,92 @@
import { describe, expect, it } from "vitest";
import { buildCustomTypeTreeChildren } from "@/lib/sidebar/customTypeTree";
import type { CustomTypeDetails, TreeNode } from "@/types/database";
const parent: TreeNode = {
id: "c:db:app:types:status",
label: "status",
type: "type",
connectionId: "c",
database: "db",
schema: "app",
};
function details(overrides: Partial<CustomTypeDetails>): CustomTypeDetails {
return {
name: "status",
schema: "app",
kind: "enum",
members: [],
properties: { domainConstraints: [] },
...overrides,
};
}
describe("custom type sidebar tree", () => {
it("builds composite fields with their data types", () => {
const children = buildCustomTypeTreeChildren(parent, details({ kind: "composite", members: [{ name: "city", dataType: "text", ordinal: 1 }] }));
expect(children).toHaveLength(1);
expect(children[0]).toMatchObject({ id: "c:db:app:types:status:type-member:field:city", label: "city", type: "type-member", meta: { kind: "field", displayValue: "text", ordinal: 1 } });
});
it("builds enum values in backend order", () => {
const children = buildCustomTypeTreeChildren(
parent,
details({
members: [
{ name: "", dataType: "", ordinal: 1, enumValue: "draft" },
{ name: "", dataType: "", ordinal: 2, enumValue: "published" },
],
}),
);
expect(children.map((child) => child.label)).toEqual(["draft", "published"]);
expect(children.map((child) => child.id)).toEqual(["c:db:app:types:status:type-member:enum-value:draft", "c:db:app:types:status:type-member:enum-value:published"]);
});
it("keeps existing member IDs stable when a new member is inserted", () => {
const before = buildCustomTypeTreeChildren(
parent,
details({
members: [
{ name: "", dataType: "", ordinal: 1, enumValue: "draft" },
{ name: "", dataType: "", ordinal: 2, enumValue: "published" },
],
}),
);
const after = buildCustomTypeTreeChildren(
parent,
details({
members: [
{ name: "", dataType: "", ordinal: 1, enumValue: "new" },
{ name: "", dataType: "", ordinal: 2, enumValue: "draft" },
{ name: "", dataType: "", ordinal: 3, enumValue: "published" },
],
}),
);
expect(after.find((child) => child.label === "draft")?.id).toBe(before.find((child) => child.label === "draft")?.id);
expect(after.find((child) => child.label === "published")?.id).toBe(before.find((child) => child.label === "published")?.id);
});
it("encodes member identity characters in IDs", () => {
const children = buildCustomTypeTreeChildren(parent, details({ members: [{ name: "", dataType: "text", ordinal: 1, enumValue: "a:b/c" }] }));
expect(children[0].id).toContain("a%3Ab%2Fc");
});
it.each(["domain", "range", "multirange", "base"] as const)("does not add property children for %s types", (kind) => {
expect(
buildCustomTypeTreeChildren(
parent,
details({
kind,
properties: {
baseType: "text",
rangeSubtype: "numeric",
domainConstraints: [{ name: "value_check", definition: "CHECK (VALUE IS NOT NULL)" }],
},
}),
),
).toEqual([]);
});
});
@@ -40,6 +40,13 @@ describe("sidebar tree item layout", () => {
expect(canTreeNodeShowExpander({ type: "type-body", childCount: 0, explicitContainer: true })).toBe(false);
});
it("shows an expander only while a custom type may still load children", () => {
expect(canTreeNodeShowExpander({ type: "type", childCount: undefined })).toBe(true);
expect(canTreeNodeShowExpander({ type: "type", childCount: 2 })).toBe(true);
expect(canTreeNodeShowExpander({ type: "type", childCount: 0 })).toBe(false);
expect(canTreeNodeShowExpander({ type: "type-member", childCount: undefined })).toBe(false);
});
it("aligns comments to the longest sibling name without crossing parent groups", () => {
const widths = alignedSidebarCommentLabelWidths([
{ id: "tables", depth: 1, alignable: false, hasComment: false, labelWidth: 0 },
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { isDirectNavigationTreeNode, objectSourceKindForTreeNode, objectSourceTargetForTreeNode, shouldActivateTreeNodeOnSingleClick, treeNodeRowAction } from "@/lib/sidebar/treeNodeClick";
import { isDirectNavigationTreeNode, objectSourceKindForTreeNode, objectSourceTargetForTreeNode, shouldActivateTreeNodeOnSingleClick, treeNodeRowAction, treeNodeRowDoubleClickAction } from "@/lib/sidebar/treeNodeClick";
describe("treeNodeClick", () => {
it("opens synonym nodes as synonym source", () => {
@@ -7,6 +7,32 @@ describe("treeNodeClick", () => {
expect(treeNodeRowAction("synonym", false)).toBe("open-source");
});
it("only Xugu type nodes open source on single click", () => {
expect(treeNodeRowAction("type", false, "single", "xugu")).toBe("open-source");
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase"] as const) {
expect(treeNodeRowAction("type", true, "single", dbType), String(dbType)).toBe("toggle");
expect(treeNodeRowAction("type", false, "single", dbType), String(dbType)).toBe("none");
}
// Unknown connection type keeps the conservative no-action behavior.
expect(treeNodeRowAction("type", false, "single", undefined)).toBe("none");
});
it("only Xugu type nodes open source on double click", () => {
expect(treeNodeRowDoubleClickAction("type", false, "double", false, "xugu")).toBe("open-source");
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase"] as const) {
expect(treeNodeRowDoubleClickAction("type", false, "double", true, dbType), String(dbType)).toBe("toggle");
expect(treeNodeRowDoubleClickAction("type", false, "double", false, dbType), String(dbType)).toBe("none");
}
expect(treeNodeRowDoubleClickAction("type", false, "double", false, undefined)).toBe("none");
});
it("keeps source actions for non-type source nodes on PG-family databases", () => {
for (const type of ["procedure", "function", "trigger", "sequence", "package", "package-body"] as const) {
expect(treeNodeRowAction(type, false, "single", "postgres"), type).toBe("open-source");
}
expect(treeNodeRowAction("type-body", false, "single", "xugu")).toBe("open-source");
});
it("opens Consul navigation entries on one click regardless of object activation preference", () => {
expect(isDirectNavigationTreeNode("consul-root")).toBe(true);
expect(isDirectNavigationTreeNode("consul-overview")).toBe(true);
@@ -23,9 +49,9 @@ describe("treeNodeClick", () => {
expect(treeNodeRowAction("package", false)).toBe("open-source");
});
it("toggles an expandable type node before opening its source", () => {
expect(treeNodeRowAction("type", true)).toBe("toggle");
expect(treeNodeRowAction("type", false)).toBe("open-source");
it("toggles an expandable Xugu type node before opening its source", () => {
expect(treeNodeRowAction("type", true, "single", "xugu")).toBe("toggle");
expect(treeNodeRowAction("type", false, "single", "xugu")).toBe("open-source");
});
it("routes package members to their owning package body", () => {
@@ -39,8 +39,30 @@ describe("singleClickRowAction", () => {
expect(singleClickRowAction(row("PACKAGE_BODY", "pkg_body_test"))).toBe("open-source");
});
it.each(["TRIGGER", "TYPE", "TYPE_BODY"] as const)("returns open-source for %s", (type) => {
expect(singleClickRowAction(row(type, "programmable_test"))).toBe("open-source");
it.each(["TRIGGER", "TYPE", "TYPE_BODY"] as const)("returns open-source for %s on Xugu", (type) => {
expect(singleClickRowAction(row(type, "programmable_test"), "xugu")).toBe("open-source");
});
it("only Xugu TYPE rows open source", () => {
expect(singleClickRowAction(row("TYPE", "app_status"), "xugu")).toBe("open-source");
expect(doubleClickRowAction(row("TYPE", "app_status"), "xugu")).toBe("open-source");
expect(singleClickRowAction(row("TYPE_BODY", "app_status"), "xugu")).toBe("open-source");
for (const dbType of ["postgres", "opengauss", "gaussdb", "kingbase", "vastbase"] as const) {
// Verified PG-family TYPE rows open the read-only details panel.
expect(singleClickRowAction(row("TYPE", "app_status"), dbType), String(dbType)).toBe("type-info");
expect(doubleClickRowAction(row("TYPE", "app_status"), dbType), String(dbType)).toBe("type-info");
// TYPE_BODY has no backend getter on these databases.
expect(singleClickRowAction(row("TYPE_BODY", "app_status"), dbType), String(dbType)).toBe("none");
}
// Unknown connection type keeps the conservative no-action behavior.
expect(singleClickRowAction(row("TYPE", "app_status"), undefined)).toBe("none");
expect(doubleClickRowAction(row("TYPE", "app_status"), undefined)).toBe("none");
});
it("keeps source actions for non-type rows on PG-family databases", () => {
for (const type of ["VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE", "PACKAGE", "PACKAGE_BODY"] as const) {
expect(singleClickRowAction(row(type), "postgres"), type).toBe("open-source");
}
});
it("returns none for null/undefined", () => {
+37
View File
@@ -95,6 +95,27 @@ describe("PostgreSQL overloaded routines", () => {
});
});
describe("PostgreSQL custom type metadata", () => {
it("preserves type kind and member capability in simple and grouped trees", () => {
const objects: ObjectInfo[] = [
{ name: "address", object_type: "TYPE", schema: "public", custom_type_kind: "composite", has_members: true },
{ name: "email", object_type: "TYPE", schema: "public", custom_type_kind: "domain", has_members: false },
];
const simple = buildSimpleObjectTreeNodes({ ...context, schema: "public", objects });
expect(simple.map((node) => ({ name: node.label, kind: node.customTypeKind, hasMembers: node.hasMembers }))).toEqual([
{ name: "address", kind: "composite", hasMembers: true },
{ name: "email", kind: "domain", hasMembers: false },
]);
const grouped = buildGroupedObjectTreeNodes({ ...context, schema: "public", objects });
expect(grouped.find((node) => node.type === "group-types")?.children?.map((node) => ({ name: node.label, kind: node.customTypeKind, hasMembers: node.hasMembers }))).toEqual([
{ name: "address", kind: "composite", hasMembers: true },
{ name: "email", kind: "domain", hasMembers: false },
]);
});
});
describe("programmable database objects", () => {
it("keeps Xugu trigger/type nodes distinct and preserves an invalid status", () => {
const objects: ObjectInfo[] = [
@@ -156,6 +177,22 @@ describe("programmable database objects", () => {
expect(synonymGroup).toEqual(expect.objectContaining({ objectCount: 1, label: "tree.synonyms" }));
expect(synonymGroup?.children).toEqual([expect.objectContaining({ type: "synonym", objectName: "SYN_SHOP_USERS", valid: true })]);
});
it("groups user-defined types with an object count", () => {
const objects: ObjectInfo[] = [
{ name: "status", object_type: "TYPE", schema: "app", comment: "order status" },
{ name: "email", object_type: "TYPE", schema: "app" },
];
const groups = buildGroupedObjectTreeNodes({ ...context, schema: "app", objects });
const typeGroup = groups.find((node) => node.type === "group-types");
expect(typeGroup).toEqual(expect.objectContaining({ objectCount: 2, label: "tree.types" }));
expect(typeGroup?.children?.map((node) => node.type)).toEqual(["type", "type"]);
expect(typeGroup?.children?.map((node) => node.objectName)).toEqual(["email", "status"]);
expect(typeGroup?.children?.[0]?.comment).toBeUndefined();
expect(typeGroup?.children?.[1]?.comment).toBe("order status");
});
});
describe("PostgreSQL table hierarchy", () => {