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

+55 -8
View File
@@ -1,6 +1,6 @@
import { strict as assert } from "node:assert";
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";
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", () => {
const tableMeta = {
schema: "public",
tableName: "users",
columns: [
{
@@ -36,7 +35,10 @@ test("returns persisted table metadata for a data tab", () => {
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", () => {
@@ -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>";
// rebuilding SQL from the title would qualify the table twice (issue #3613).
const tableMeta = {
@@ -132,17 +134,23 @@ test("prefers the tableMeta table name over a schema-qualified tab title", () =>
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?.schema, "dbo");
});
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(meta?.schema, "dbo");
assert.equal(repairRestoredDataTabTableIdentity(restored, "postgres"), false);
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", () => {
@@ -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");
});
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", () => {
assert.equal(tableMetaForDataTab(tab({ mode: "query" })), undefined);
});