fix(mysql): refresh tables after proxy detection

Closes #6721
This commit is contained in:
t8y2 committed 2026-08-19 18:19:04 +00:00
1 parent c2b7e9f269
commit 34c8afeb58
3 files changed
+186 -14

No files matched your search

@@ -159,6 +159,133 @@ describe("connectionStore database info", () => {
await vi.waitFor(() => expect(store.getConfig(config.id)?.database_info?.productVersion).toBe("8.0.34"));
});
it("refreshes physical table metadata after delayed ShardingSphere detection", async () => {
const config = mysqlConnection();
let resolveDatabaseInfo!: (value: { productName: string; productVersion: string }) => void;
const databaseInfo = new Promise<{ productName: string; productVersion: string }>((resolve) => {
resolveDatabaseInfo = resolve;
});
let useLogicalTables = false;
const listTables = vi.fn().mockImplementation(async () => [
{
name: useLogicalTables ? "async_task" : "async_task_0",
table_type: "BASE TABLE",
comment: null,
},
]);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
connectDb: vi.fn().mockResolvedValue(config.id),
connectionDatabaseInfo: vi.fn(() => databaseInfo),
connectionIdentifierQuote: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
listDatabases: vi.fn().mockResolvedValue([{ name: "app" }]),
listObjects: vi.fn().mockResolvedValue([]),
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveConnectionDatabaseInfo: vi.fn().mockImplementation(async () => {
useLogicalTables = true;
}),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useConnectionStore();
useSettingsStore().editorSettings.sidebarObjectDisplay = "simple";
await store.addConnection(config);
await store.connect(config);
await store.loadDatabases(config.id, { force: true });
const databaseNode = store.treeNodes[0]?.children?.find((node) => node.type === "database" && node.database === "app");
expect(databaseNode).toBeDefined();
await store.loadTreeNodeChildren(databaseNode!, { force: true });
expect(databaseNode?.children?.filter((node) => node.type === "table").map((node) => node.label)).toEqual(["async_task_0"]);
resolveDatabaseInfo({ productName: "MySQL", productVersion: "8.0.27-ShardingSphere-Proxy 5.5.2" });
await vi.waitFor(() => {
const refreshedDatabaseNode = store.treeNodes[0]?.children?.find((node) => node.type === "database" && node.database === "app");
expect(refreshedDatabaseNode?.children?.filter((node) => node.type === "table").map((node) => node.label)).toEqual(["async_task"]);
});
expect(listTables).toHaveBeenCalledTimes(2);
});
it("does not let a pre-detection table request overwrite the refreshed logical tables", async () => {
const config = mysqlConnection();
let resolveDatabaseInfo!: (value: { productName: string; productVersion: string }) => void;
const databaseInfo = new Promise<{ productName: string; productVersion: string }>((resolve) => {
resolveDatabaseInfo = resolve;
});
let resolvePhysicalTables!: (value: Array<{ name: string; table_type: string; comment: null }>) => void;
const physicalTables = new Promise<Array<{ name: string; table_type: string; comment: null }>>((resolve) => {
resolvePhysicalTables = resolve;
});
let useLogicalTables = false;
const listTables = vi
.fn()
.mockImplementationOnce(() => physicalTables)
.mockResolvedValue([
{
name: "async_task",
table_type: "BASE TABLE",
comment: null,
},
]);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
connectDb: vi.fn().mockResolvedValue(config.id),
connectionDatabaseInfo: vi.fn(() => databaseInfo),
connectionIdentifierQuote: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
listDatabases: vi.fn().mockResolvedValue([{ name: "app" }]),
listObjects: vi.fn().mockResolvedValue([]),
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveConnectionDatabaseInfo: vi.fn().mockImplementation(async () => {
useLogicalTables = true;
}),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useConnectionStore();
useSettingsStore().editorSettings.sidebarObjectDisplay = "simple";
await store.addConnection(config);
await store.connect(config);
await store.loadDatabases(config.id, { force: true });
const databaseNode = store.treeNodes[0]?.children?.find((node) => node.type === "database" && node.database === "app");
expect(databaseNode).toBeDefined();
databaseNode!.isExpanded = true;
const physicalLoad = store.loadTreeNodeChildren(databaseNode!, { force: true });
await vi.waitFor(() => expect(listTables).toHaveBeenCalledTimes(1));
resolveDatabaseInfo({ productName: "MySQL", productVersion: "8.0.27-ShardingSphere-Proxy 5.5.2" });
await vi.waitFor(() => expect(listTables).toHaveBeenCalledTimes(2));
resolvePhysicalTables([
{
name: "async_task_0",
table_type: "BASE TABLE",
comment: null,
},
]);
await physicalLoad;
await vi.waitFor(() => {
const refreshedDatabaseNode = store.treeNodes[0]?.children?.find((node) => node.type === "database" && node.database === "app");
expect(refreshedDatabaseNode?.children?.filter((node) => node.type === "table").map((node) => node.label)).toEqual(["async_task"]);
});
expect(useLogicalTables).toBe(true);
});
it("keeps a successful connection when optional metadata refresh fails", async () => {
const config = mysqlConnection();