fix(table): repair restored PostgreSQL table identity

Closes #10474
This commit is contained in:
t8y2 authored and t8y2 committed 2026-09-29 00:47:45 +08:00
1 parent d49f3739bf
commit 4d85afd173
3 files changed
+87 -10

No files matched your search

+30 -1
View File
@@ -1,4 +1,5 @@
import type { QueryTab, ColumnInfo } from "@/types/database"; import { queryResultSourceNameParts } from "@/lib/sql/queryResultSource";
import type { QueryTab, ColumnInfo, DatabaseType } from "@/types/database";
export type DataTabTableMeta = NonNullable<QueryTab["tableMeta"]>; export type DataTabTableMeta = NonNullable<QueryTab["tableMeta"]>;
@@ -15,6 +16,34 @@ function titleTableName(tab: QueryTab): string {
return title; return title;
} }
/**
* Repair the one legacy identity shape known to lose its schema during tab
* restoration. PostgreSQL permits dots inside quoted identifiers, so the tab
* title alone is deliberately not split. The persisted SELECT must independently
* parse to the same schema-qualified source before the identity is migrated.
*/
export function repairRestoredDataTabTableIdentity(tab: QueryTab, databaseType: DatabaseType | undefined): boolean {
if (databaseType !== "postgres" || tab.mode !== "data" || tab.schema?.trim() || tab.tableMeta?.schema?.trim() || tab.tableMeta?.columns.length) return false;
const sourceSql = tab.resultBaseSql?.trim() || tab.lastExecutedSql?.trim() || tab.sql.trim();
const source = queryResultSourceNameParts(sourceSql, { databaseType });
const title = tab.title.trim();
if (!source?.qualifier || title !== `${source.qualifier}.${source.name}`) return false;
const persistedTableName = tab.tableMeta?.tableName.trim();
if (persistedTableName && persistedTableName !== title && persistedTableName !== source.name) return false;
tab.schema = source.qualifier;
tab.tableMeta = {
...tab.tableMeta,
schema: source.qualifier,
tableName: source.name,
columns: tab.tableMeta?.columns ?? [],
primaryKeys: tab.tableMeta?.primaryKeys ?? [],
};
return true;
}
function fallbackColumnInfo(name: string): ColumnInfo { function fallbackColumnInfo(name: string): ColumnInfo {
return { return {
name, name,
+2 -1
View File
@@ -66,7 +66,7 @@ import { usesAgentCursorForQuery, usesAgentCursorForTableData } from "@/lib/data
import { connectionIsDorisFamilyCatalogCapable, defaultAutoCommitForDbType, supportsClearableQuerySchema, supportsTransaction, usesOracleStickyTransactionState, usesProvenReadOnlyStickyTransactionState } from "@/lib/database/databaseFeatureSupport"; import { connectionIsDorisFamilyCatalogCapable, defaultAutoCommitForDbType, supportsClearableQuerySchema, supportsTransaction, usesOracleStickyTransactionState, usesProvenReadOnlyStickyTransactionState } from "@/lib/database/databaseFeatureSupport";
import { canInsertTableRows, canUseKeylessRowPredicate, DBX_ROWID_COLUMN, editablePrimaryKeys, shouldIncludeSyntheticRowId, usesSyntheticRowIdKey } from "@/lib/table/tableEditing"; import { canInsertTableRows, canUseKeylessRowPredicate, DBX_ROWID_COLUMN, editablePrimaryKeys, shouldIncludeSyntheticRowId, usesSyntheticRowIdKey } from "@/lib/table/tableEditing";
import { TABLE_DATA_EXPORT_PAGE_SIZE } from "@/lib/table/tableDataExport"; import { TABLE_DATA_EXPORT_PAGE_SIZE } from "@/lib/table/tableDataExport";
import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta"; import { repairRestoredDataTabTableIdentity, tableMetaForDataTab } from "@/lib/table/tableDataTabMeta";
import { isDataTabMetadataLifecycleStale } from "@/lib/sidebar/dataTabOpenPolicy"; import { isDataTabMetadataLifecycleStale } from "@/lib/sidebar/dataTabOpenPolicy";
import type { SqlInsertMode } from "@/lib/export/sqlInsertMode"; import type { SqlInsertMode } from "@/lib/export/sqlInsertMode";
import { csvNullLiteralForMode } from "@/lib/export/csvNullMode"; import { csvNullLiteralForMode } from "@/lib/export/csvNullMode";
@@ -2426,6 +2426,7 @@ export const useQueryStore = defineStore("query", () => {
tab.autoCommit = defaultAutoCommitForDbTypeWithSetting(connection?.db_type); tab.autoCommit = defaultAutoCommitForDbTypeWithSetting(connection?.db_type);
} else if (tab.mode === "data" && connection) { } else if (tab.mode === "data" && connection) {
tab.schema = connectionObjectTreeNodeSchema(connection, tab.database, tab.schema); tab.schema = connectionObjectTreeNodeSchema(connection, tab.database, tab.schema);
repairRestoredDataTabTableIdentity(tab, effectiveDatabaseTypeForConnection(connection));
} }
} }
restored.tabs.forEach(initializeResultAutoSave); restored.tabs.forEach(initializeResultAutoSave);
+55 -8
View File
@@ -1,6 +1,6 @@
import { strict as assert } from "node:assert"; import { strict as assert } from "node:assert";
import { test } from "vitest"; import { test } from "vitest";
import { tableMetaForDataTab } from "../../apps/desktop/src/lib/table/tableDataTabMeta.ts"; import { repairRestoredDataTabTableIdentity, tableMetaForDataTab } from "../../apps/desktop/src/lib/table/tableDataTabMeta.ts";
import type { QueryTab } from "../../apps/desktop/src/types/database.ts"; import type { QueryTab } from "../../apps/desktop/src/types/database.ts";
function tab(overrides: Partial<QueryTab> = {}): QueryTab { function tab(overrides: Partial<QueryTab> = {}): QueryTab {
@@ -21,7 +21,6 @@ function tab(overrides: Partial<QueryTab> = {}): QueryTab {
test("returns persisted table metadata for a data tab", () => { test("returns persisted table metadata for a data tab", () => {
const tableMeta = { const tableMeta = {
schema: "public",
tableName: "users", tableName: "users",
columns: [ columns: [
{ {
@@ -36,7 +35,10 @@ test("returns persisted table metadata for a data tab", () => {
primaryKeys: ["id"], primaryKeys: ["id"],
}; };
assert.equal(tableMetaForDataTab(tab({ tableMeta })), tableMeta); const restored = tab({ title: "public.users", schema: undefined, sql: 'SELECT * FROM "public"."users"', tableMeta });
assert.equal(repairRestoredDataTabTableIdentity(restored, "postgres"), false);
assert.equal(tableMetaForDataTab(restored), tableMeta);
}); });
test("builds fallback metadata from a data tab when column metadata is unavailable", () => { test("builds fallback metadata from a data tab when column metadata is unavailable", () => {
@@ -122,7 +124,7 @@ test("uses result columns when persisted table metadata has no columns", () => {
); );
}); });
test("prefers the tableMeta table name over a schema-qualified tab title", () => { test("preserves SQL Server tableMeta instead of qualifying its title twice (#3613)", () => {
// Data tabs opened from the object browser are titled "<schema>.<table>"; // Data tabs opened from the object browser are titled "<schema>.<table>";
// rebuilding SQL from the title would qualify the table twice (issue #3613). // rebuilding SQL from the title would qualify the table twice (issue #3613).
const tableMeta = { const tableMeta = {
@@ -132,17 +134,23 @@ test("prefers the tableMeta table name over a schema-qualified tab title", () =>
primaryKeys: [], primaryKeys: [],
}; };
const meta = tableMetaForDataTab(tab({ title: "dbo.wcs_dispatch_task", schema: "dbo", tableMeta })); const restored = tab({ title: "dbo.wcs_dispatch_task", schema: "dbo", tableMeta });
assert.equal(repairRestoredDataTabTableIdentity(restored, "sqlserver"), false);
const meta = tableMetaForDataTab(restored);
assert.equal(meta?.tableName, "wcs_dispatch_task"); assert.equal(meta?.tableName, "wcs_dispatch_task");
assert.equal(meta?.schema, "dbo"); assert.equal(meta?.schema, "dbo");
}); });
test("strips the schema prefix from the tab title when no tableMeta exists", () => { test("strips the schema prefix from the tab title when no tableMeta exists", () => {
const meta = tableMetaForDataTab(tab({ title: "dbo.wcs_dispatch_task", schema: "dbo" })); const restored = tab({ title: "public.users", schema: "public", sql: "SELECT * FROM public.users" });
assert.equal(meta?.tableName, "wcs_dispatch_task"); assert.equal(repairRestoredDataTabTableIdentity(restored, "postgres"), false);
assert.equal(meta?.schema, "dbo"); const meta = tableMetaForDataTab(restored);
assert.equal(meta?.tableName, "users");
assert.equal(meta?.schema, "public");
}); });
test("keeps a dotted tab title intact when it does not start with the schema", () => { test("keeps a dotted tab title intact when it does not start with the schema", () => {
@@ -151,6 +159,45 @@ test("keeps a dotted tab title intact when it does not start with the schema", (
assert.equal(meta?.tableName, "audit.2024_log"); assert.equal(meta?.tableName, "audit.2024_log");
}); });
test("repairs a restored PostgreSQL qualified title only when its SELECT confirms the identity", () => {
const restored = tab({
title: "term.MSS_CHECK_SALES_ITEM",
schema: undefined,
sql: 'SELECT * FROM "term"."MSS_CHECK_SALES_ITEM"',
result: {
columns: ["ITEM_ID", "STATUS"],
rows: [],
affected_rows: 0,
execution_time_ms: 1,
},
});
assert.equal(repairRestoredDataTabTableIdentity(restored, "postgres"), true);
const meta = tableMetaForDataTab(restored);
assert.equal(meta?.schema, "term");
assert.equal(meta?.tableName, "MSS_CHECK_SALES_ITEM");
assert.deepEqual(
meta?.columns.map((column) => column.name),
["ITEM_ID", "STATUS"],
);
assert.deepEqual(meta?.primaryKeys, []);
});
test("does not split a restored PostgreSQL title for a literal dotted table name", () => {
const restored = tab({
title: "audit.2024_log",
schema: undefined,
sql: 'SELECT * FROM "audit.2024_log"',
});
assert.equal(repairRestoredDataTabTableIdentity(restored, "postgres"), false);
const meta = tableMetaForDataTab(restored);
assert.equal(meta?.schema, undefined);
assert.equal(meta?.tableName, "audit.2024_log");
});
test("does not infer table metadata for query tabs", () => { test("does not infer table metadata for query tabs", () => {
assert.equal(tableMetaForDataTab(tab({ mode: "query" })), undefined); assert.equal(tableMetaForDataTab(tab({ mode: "query" })), undefined);
}); });