fix(sql): restore SQL file schema for sidebar locate
This commit is contained in:
1 parent
440c41f5b9
commit
e9a90173a6
11 files changed
+175
-53
No files matched your search
@@ -427,6 +427,41 @@ describe("object tree node schema", () => {
|
||||
expect(metadataSchemaForConnection(connection, resourcePath, resourcePath)).toBe("");
|
||||
});
|
||||
|
||||
it("never uses the database name as the schema when a schema-tree engine has no schema yet", () => {
|
||||
// Query tabs can reach locate/metadata paths before a schema is picked (a
|
||||
// toolbar "new query" tab, or an external .sql file reopened from disk).
|
||||
// PostgreSQL and its relatives keep tables under a separate schema level, so
|
||||
// `schema || database` sends "dbx_test" as the schema and matches nothing —
|
||||
// locate in the sidebar silently does nothing (issue #7648). The blank schema
|
||||
// lets the backend resolve the session default instead.
|
||||
for (const dbType of ["postgres", "gaussdb", "opengauss", "kingbase", "redshift", "duckdb"] as const) {
|
||||
expect(connectionObjectTreeQuerySchema({ db_type: dbType }, "dbx_test")).toBe("");
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: dbType }, "dbx_test")).toBeUndefined();
|
||||
expect(metadataSchemaForConnection({ db_type: dbType }, "dbx_test")).toBe("");
|
||||
// An explicit schema still wins, and the database name is a legitimate
|
||||
// schema name when the user really did select it.
|
||||
expect(connectionObjectTreeQuerySchema({ db_type: dbType }, "dbx_test", "public")).toBe("public");
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: dbType }, "dbx_test", "public")).toBe("public");
|
||||
}
|
||||
// SQL Server keeps its own dbo default for metadata lookups.
|
||||
expect(connectionObjectTreeQuerySchema({ db_type: "sqlserver" }, "dbx_test")).toBe("");
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: "sqlserver" }, "dbx_test")).toBeUndefined();
|
||||
expect(metadataSchemaForConnection({ db_type: "sqlserver" }, "dbx_test")).toBe("dbo");
|
||||
});
|
||||
|
||||
it("keeps the database-as-schema fallback for engines whose database is the schema", () => {
|
||||
// Oracle, Dameng and the Hive family address objects as schema.table with no
|
||||
// separate schema level in the tree, so the database name is the schema there
|
||||
// and must survive the fix above.
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: "oracle" }, "DBX_TEST")).toBe("DBX_TEST");
|
||||
expect(connectionObjectTreeQuerySchema({ db_type: "oracle" }, "DBX_TEST")).toBe("DBX_TEST");
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: "dameng" }, "DBX_TEST")).toBe("DBX_TEST");
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: "hive" }, "CS")).toBe("CS");
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: "spark" }, "CS")).toBe("CS");
|
||||
// Flat engines keep returning no tree schema at all.
|
||||
expect(connectionObjectTreeNodeSchema({ db_type: "mysql" }, "shop")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps the database-as-schema fallback for flat engines and blanks it for Cloud Spanner", () => {
|
||||
// Editor completion paths send the database name as the metadata schema when a
|
||||
// connection has no schema level; only Spanner must send a blank schema instead.
|
||||
|
||||
@@ -26,6 +26,26 @@ describe("external SQL file targets", () => {
|
||||
expect(resolveExternalSqlFileTarget("/work/iceberg.sql", () => true, unassociatedExternalSqlFileTarget()).catalog).toBe("iceberg");
|
||||
});
|
||||
|
||||
it("restores the schema the file was saved from", () => {
|
||||
// Reopening a .sql file used to drop the schema, leaving the tab on the
|
||||
// connection default: sidebar locate then looked for the table in the wrong
|
||||
// namespace and silently did nothing (issue #7648).
|
||||
rememberExternalSqlFileTarget("/work/report.sql", { connectionId: "saved-connection", database: "dbx_test", schema: "analytics" });
|
||||
|
||||
expect(resolveExternalSqlFileTarget("/work/report.sql", () => true, unassociatedExternalSqlFileTarget())).toEqual({
|
||||
connectionId: "saved-connection",
|
||||
database: "dbx_test",
|
||||
catalog: undefined,
|
||||
schema: "analytics",
|
||||
});
|
||||
});
|
||||
|
||||
it("treats historical records without a schema as unset", () => {
|
||||
localStorage.setItem(EXTERNAL_SQL_FILE_TARGETS_STORAGE_KEY, JSON.stringify([{ path: "/work/legacy.sql", connectionId: "saved-connection", database: "sales", updatedAt: 1 }]));
|
||||
|
||||
expect(resolveExternalSqlFileTarget("/work/legacy.sql", () => true, unassociatedExternalSqlFileTarget()).schema).toBeUndefined();
|
||||
});
|
||||
|
||||
it("treats historical records without catalog as the default catalog", () => {
|
||||
localStorage.setItem(EXTERNAL_SQL_FILE_TARGETS_STORAGE_KEY, JSON.stringify([{ path: "/work/legacy.sql", connectionId: "saved-connection", database: "sales", updatedAt: 1 }]));
|
||||
|
||||
|
||||
@@ -237,12 +237,28 @@ export function connectionQueryExecutionSchema(connection: JdbcDialectConnection
|
||||
return type && DATABASE_AS_EXECUTION_SCHEMA_TYPES.has(type) ? database || undefined : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the database name is a wrong guess for an unknown schema. Engines that keep tables under a
|
||||
* dedicated schema level in the tree (PostgreSQL and relatives, SQL Server, DB2, Trino, generic JDBC)
|
||||
* address objects as database.schema.table, so the database name matches no schema there. Oracle,
|
||||
* Dameng and the Hive family are schema-aware without that level — their database *is* the schema —
|
||||
* and must keep the `schema || database` fallback.
|
||||
*/
|
||||
function databaseNameIsNotASchema(type: DatabaseType | undefined): boolean {
|
||||
return isSchemaAware(type) && usesTreeSchemaMode(type);
|
||||
}
|
||||
|
||||
export function connectionObjectTreeQuerySchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string {
|
||||
if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database;
|
||||
if (connectionUsesDatabaseObjectTreeMode(connection)) return "";
|
||||
const type = effectiveDatabaseTypeForConnection(connection);
|
||||
if (type === "informix") return schema || "";
|
||||
if (type === "spanner") return spannerObjectTreeSchema(schema);
|
||||
// A query tab that never picked a schema (toolbar "new query", a reopened .sql
|
||||
// file) would otherwise send the database name and match no objects at all, so
|
||||
// sidebar locate silently did nothing (issue #7648). The blank schema is the
|
||||
// established "resolve it on the backend" value used by the completion paths.
|
||||
if (!schema && databaseNameIsNotASchema(type)) return "";
|
||||
return schema || database;
|
||||
}
|
||||
|
||||
@@ -272,6 +288,7 @@ export function connectionObjectTreeNodeSchema(connection: JdbcDialectConnection
|
||||
if (type === "sqlite") return schema || database;
|
||||
if (type === "spanner") return spannerObjectTreeSchema(schema);
|
||||
if (!type) return schema;
|
||||
if (!schema && databaseNameIsNotASchema(type)) return undefined;
|
||||
return isSchemaAware(type) ? schema || database : undefined;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,5 +7,6 @@ export function savedSqlImportTarget(sourceTarget: ExternalSqlFileTarget, folder
|
||||
connectionId: folder.connectionId,
|
||||
database: sourceTarget.connectionId === folder.connectionId ? sourceTarget.database : "",
|
||||
catalog: sourceTarget.connectionId === folder.connectionId ? sourceTarget.catalog : undefined,
|
||||
schema: sourceTarget.connectionId === folder.connectionId ? sourceTarget.schema : undefined,
|
||||
};
|
||||
}
|
||||
@@ -7,10 +7,11 @@ export interface ExternalSqlFileTarget {
|
||||
connectionId: string;
|
||||
database: string;
|
||||
catalog?: string;
|
||||
schema?: string;
|
||||
}
|
||||
|
||||
export function unassociatedExternalSqlFileTarget(): ExternalSqlFileTarget {
|
||||
return { connectionId: "", database: "", catalog: undefined };
|
||||
return { connectionId: "", database: "", catalog: undefined, schema: undefined };
|
||||
}
|
||||
|
||||
interface StoredExternalSqlFileTarget extends ExternalSqlFileTarget {
|
||||
@@ -23,8 +24,18 @@ function loadExternalSqlFileTargets(): StoredExternalSqlFileTarget[] {
|
||||
const parsed = JSON.parse(localStorage.getItem(EXTERNAL_SQL_FILE_TARGETS_STORAGE_KEY) || "[]");
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed
|
||||
.filter((item) => typeof item?.path === "string" && typeof item?.connectionId === "string" && typeof item?.database === "string" && (item?.catalog === undefined || typeof item.catalog === "string") && typeof item?.updatedAt === "number")
|
||||
.map((item) => ({ ...item, catalog: item.catalog })) as StoredExternalSqlFileTarget[];
|
||||
.filter(
|
||||
(item) =>
|
||||
typeof item?.path === "string" &&
|
||||
typeof item?.connectionId === "string" &&
|
||||
typeof item?.database === "string" &&
|
||||
(item?.catalog === undefined || typeof item.catalog === "string") &&
|
||||
// Targets written before schemas were remembered carry no schema key at
|
||||
// all, so an absent schema stays valid and simply restores as undefined.
|
||||
(item?.schema === undefined || typeof item.schema === "string") &&
|
||||
typeof item?.updatedAt === "number",
|
||||
)
|
||||
.map((item) => ({ ...item, catalog: item.catalog, schema: item.schema })) as StoredExternalSqlFileTarget[];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
@@ -71,5 +82,5 @@ export function resolveExternalSqlFileTarget(path: string, connectionExists: (co
|
||||
const normalizedPath = normalizeExternalSqlPath(path);
|
||||
const saved = loadExternalSqlFileTargets().find((item) => item.path === normalizedPath);
|
||||
if (!saved || !connectionExists(saved.connectionId)) return fallback;
|
||||
return { connectionId: saved.connectionId, database: saved.database, catalog: saved.catalog };
|
||||
return { connectionId: saved.connectionId, database: saved.database, catalog: saved.catalog, schema: saved.schema };
|
||||
}
|
||||
Reference in new issue
Block a user