fix(postgres): resolve result metadata through search_path

This commit is contained in:
zipg authored and GitHub committed 2026-08-20 21:38:20 +08:00
1 parent 8a9b02fa07
commit be9f8e72e4
4 files changed
+105 -16

No files matched your search

@@ -402,6 +402,39 @@ describe("queryStore multi-source result column comments", () => {
expect(tab.queryEditabilityReason).toBeUndefined();
});
it("resolves unqualified PostgreSQL metadata through search_path when no schema is selected", async () => {
getConnectionConfig.mockReturnValue({ id: "pg-1", name: "PostgreSQL", db_type: "postgres", database: "app", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({
editable: true,
analysis: {
schema: undefined,
tableName: "people",
selectStar: true,
columns: [],
},
});
getColumns.mockResolvedValue([column("name", "姓名"), column("email", "邮箱")]);
executeMulti.mockResolvedValue([
{
columns: ["name", "email"],
rows: [["Alice", "alice@example.com"]],
affected_rows: 0,
execution_time_ms: 1,
},
]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("pg-1", "app", "Query");
await store.executeTabSql(tabId, "SELECT * FROM people");
const tab = store.tabs.find((item) => item.id === tabId)!;
await vi.waitFor(() => expect(tab.resultColumnComments).toEqual(["姓名", "邮箱"]));
expect(getColumns).toHaveBeenCalledWith("pg-1", "app", "", "people", undefined);
expect(listIndexes).toHaveBeenCalledWith("pg-1", "app", "", "people", undefined);
});
it("does not add explicit result comments to an adjacent keyed PostgreSQL query", async () => {
getConnectionConfig.mockReturnValue({ id: "pg-1", name: "PostgreSQL", db_type: "postgres", database: "app", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({
+6 -6
View File
@@ -3582,11 +3582,7 @@ export const useQueryStore = defineStore("query", () => {
// unqualified object reference. Resolve metadata through the login's
// default schema (with the driver's dbo fallback) so metadata and writes
// target the same object as the original SELECT.
let schema = source.schema || (dbType === "sqlserver" ? "" : tab.schema);
if (!schema) {
if (dbType === "postgres" || dbType === "kwdb") schema = "public";
else schema = "";
}
const schema = source.schema || (dbType === "sqlserver" ? "" : tab.schema) || "";
// Oracle-family connection databases are service names, not schemas. When
// the query does not qualify a schema, let the driver resolve the current
// login user's schema instead of looking up metadata under the service name.
@@ -3594,7 +3590,11 @@ export const useQueryStore = defineStore("query", () => {
// search_path. Do not reinterpret the selected database as a schema; the
// agent resolves the visible relation's actual namespace with the columns.
const useCurrentVastbaseSchema = dbType === "vastbase" && !source.schema && !tab.schema;
const resolvedSchema = (dbType === "sqlserver" && !source.schema) || (ORACLE_LIKE_METADATA_TYPES.has(dbType) && !schema) || useCurrentVastbaseSchema ? "" : metadataSchemaForConnection(conn, metadataDatabase, schema || undefined);
// PostgreSQL-compatible unqualified names also resolve through the
// connection's search_path. Keep the metadata request unqualified when no
// schema was selected instead of assuming public (or the database name).
const useCurrentPostgresSchema = (dbType === "postgres" || dbType === "kwdb") && !source.schema && !tab.schema;
const resolvedSchema = (dbType === "sqlserver" && !source.schema) || (ORACLE_LIKE_METADATA_TYPES.has(dbType) && !schema) || useCurrentVastbaseSchema || useCurrentPostgresSchema ? "" : metadataSchemaForConnection(conn, metadataDatabase, schema || undefined);
const metadataSchema = normalizeUppercaseFoldedMetadataIdentifier(dbType, resolvedSchema || undefined, source.schema ? source.schemaQuoted : false) || "";
const metadataTableName = normalizeUppercaseFoldedMetadataIdentifier(dbType, source.tableName, source.tableNameQuoted)!;
const metadataCatalog = normalizeUppercaseFoldedMetadataIdentifier(dbType, source.catalog, source.catalogQuoted);