fix(xugu): place public synonyms after schemas

This commit is contained in:
Elias authored and GitHub committed 2026-08-15 21:55:54 +08:00
1 parent 868502326c
commit 5bfb07eef0
4 files changed
+81 -12

No files matched your search

@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { isXuguPublicSynonymScope, isXuguPublicSynonymTreeNode, XUGU_PUBLIC_SYNONYM_SCOPE, xuguSchemaDisplayName } from "@/lib/sidebar/xuguPublicSynonyms";
import { isXuguPublicSynonymScope, isXuguPublicSynonymTreeNode, sortXuguSchemaInfos, XUGU_PUBLIC_SYNONYM_SCOPE, xuguSchemaDisplayName } from "@/lib/sidebar/xuguPublicSynonyms";
import { compareSidebarNames } from "@/lib/database/databaseTree";
describe("Xugu public synonym scope", () => {
it("uses an independent protocol identity", () => {
@@ -22,4 +23,20 @@ describe("Xugu public synonym scope", () => {
expect(isXuguPublicSynonymTreeNode("postgres", "schema", XUGU_PUBLIC_SYNONYM_SCOPE)).toBe(false);
expect(isXuguPublicSynonymTreeNode("xugu", "database", XUGU_PUBLIC_SYNONYM_SCOPE)).toBe(false);
});
it("keeps the public synonym scope after every real schema", () => {
const schemas = [
{ name: XUGU_PUBLIC_SYNONYM_SCOPE, comment: null },
{ name: "SYSDBA", comment: null },
{ name: "AppSchema", comment: null },
{ name: "GUEST", comment: null },
];
expect(sortXuguSchemaInfos(schemas, compareSidebarNames).map((schema) => schema.name)).toEqual(["AppSchema", "GUEST", "SYSDBA", XUGU_PUBLIC_SYNONYM_SCOPE]);
});
it("does not treat the real GUEST schema as a public-synonym scope", () => {
const schemas = [{ name: "GUEST" }, { name: XUGU_PUBLIC_SYNONYM_SCOPE }];
expect(sortXuguSchemaInfos(schemas, compareSidebarNames).map((schema) => schema.name)).toEqual(["GUEST", XUGU_PUBLIC_SYNONYM_SCOPE]);
});
});
@@ -6,6 +6,27 @@
export const XUGU_PUBLIC_SYNONYM_SCOPE = "\u0000DBX_XUGU_PUBLIC_SYNONYMS";
export const XUGU_PUBLIC_SYNONYM_SCOPE_LABEL = "Public synonyms";
/**
* Keep the synthetic public-synonym scope visually separate from real
* schemas. The scope is deliberately sorted after every real schema so a
* database-global namespace cannot be mistaken for an ordinary owner/schema.
*/
export function sortXuguSchemaInfos<T extends { name: string }>(schemas: readonly T[], compareNames: (left: string, right: string) => number): T[] {
const realSchemas: T[] = [];
const publicSynonymScopes: T[] = [];
for (const schema of schemas) {
if (isXuguPublicSynonymScope(schema.name)) {
publicSynonymScopes.push(schema);
} else {
realSchemas.push(schema);
}
}
realSchemas.sort((left, right) => compareNames(left.name, right.name));
return [...realSchemas, ...publicSynonymScopes];
}
export function isXuguPublicSynonymScope(schema: string | null | undefined): boolean {
return schema === XUGU_PUBLIC_SYNONYM_SCOPE;
}