fix(structure): load indexes when tab becomes visible

This commit is contained in:
t8y2 committed 2026-08-03 14:49:35 +08:00
1 parent 3e3e6d04b8
commit 5325c42637
5 files changed
+106 -76

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { shouldLoadTableStructureTriggers, visibleTableStructureRefreshScope } from "@/lib/table/tableStructureMetadataLoading";
import { hasTableStructureRefreshWork, unloadedTableStructureRefreshScope, visibleTableStructureRefreshScope } from "@/lib/table/tableStructureMetadataLoading";
describe("table structure metadata loading", () => {
it.each([
@@ -12,28 +12,16 @@ describe("table structure metadata loading", () => {
expect(visibleTableStructureRefreshScope(tab)).toEqual(expected);
});
it("loads trigger metadata once when the trigger tab becomes visible", () => {
const base = {
activeTab: "triggers" as const,
isCreateMode: false,
supported: true,
loading: false,
structureLoading: false,
};
it("requests only index metadata after columns and comments are already loaded", () => {
const scope = unloadedTableStructureRefreshScope("indexes", new Set(["columns", "comment"]));
expect(shouldLoadTableStructureTriggers({ ...base, loaded: false })).toBe(true);
expect(shouldLoadTableStructureTriggers({ ...base, loaded: true })).toBe(false);
expect(scope).toEqual({ columns: false, indexes: true, foreignKeys: false, triggers: false, tableComment: false });
expect(hasTableStructureRefreshWork(scope)).toBe(true);
expect(hasTableStructureRefreshWork(unloadedTableStructureRefreshScope("indexes", new Set(["columns", "indexes", "comment"])))).toBe(false);
});
it("waits for the initial structure load and skips create mode", () => {
const base = {
activeTab: "triggers" as const,
supported: true,
loaded: false,
loading: false,
};
expect(shouldLoadTableStructureTriggers({ ...base, isCreateMode: false, structureLoading: true })).toBe(false);
expect(shouldLoadTableStructureTriggers({ ...base, isCreateMode: true, structureLoading: false })).toBe(false);
it("requests trigger metadata only until that facet is loaded", () => {
expect(unloadedTableStructureRefreshScope("triggers", new Set(["comment"]))).toEqual({ columns: false, indexes: false, foreignKeys: false, triggers: true, tableComment: false });
expect(hasTableStructureRefreshWork(unloadedTableStructureRefreshScope("triggers", new Set(["triggers", "comment"])))).toBe(false);
});
});
@@ -1,4 +1,5 @@
import type { TableInfoTab } from "@/types/database";
import type { ObjectMetadataFacet } from "@/lib/metadata/objectMetadataCache";
export interface TableStructureRefreshScope {
columns: boolean;
@@ -23,14 +24,17 @@ export function visibleTableStructureRefreshScope(activeTab: TableInfoTab): Tabl
}
}
export const TRIGGERS_ONLY_REFRESH_SCOPE: TableStructureRefreshScope = {
columns: false,
indexes: false,
foreignKeys: false,
triggers: true,
tableComment: false,
};
export function unloadedTableStructureRefreshScope(activeTab: TableInfoTab, loadedFacets: ReadonlySet<ObjectMetadataFacet>): TableStructureRefreshScope {
const visibleScope = visibleTableStructureRefreshScope(activeTab);
return {
columns: visibleScope.columns && !loadedFacets.has("columns"),
indexes: visibleScope.indexes && !loadedFacets.has("indexes"),
foreignKeys: visibleScope.foreignKeys && !loadedFacets.has("foreign-keys"),
triggers: visibleScope.triggers && !loadedFacets.has("triggers"),
tableComment: visibleScope.tableComment && !loadedFacets.has("comment"),
};
}
export function shouldLoadTableStructureTriggers(options: { activeTab: TableInfoTab; isCreateMode: boolean; supported: boolean; loaded: boolean; loading: boolean; structureLoading: boolean }): boolean {
return options.activeTab === "triggers" && !options.isCreateMode && options.supported && !options.loaded && !options.loading && !options.structureLoading;
export function hasTableStructureRefreshWork(scope: TableStructureRefreshScope): boolean {
return scope.columns || scope.indexes || scope.foreignKeys || scope.triggers || scope.tableComment;
}