feat(sql-editor): reuse active connection for opened files

Closes #9263
This commit is contained in:
t8y2 committed 2026-09-27 14:19:10 +00:00
1 parent 0fd1d6108f
commit 5d13c5ecfc
12 files changed
+332 -40

No files matched your search

@@ -46,6 +46,8 @@ it("hands prepared Web previews to the dialog, clears them on close, and preserv
expect(mocks.store.sqlFileSource).toBeNull();
dialogs.showSqlFileDialog.value = false;
await nextTick();
expect(dialogs.sqlFilePrefillConnectionId.value).toBe("");
expect(dialogs.sqlFilePrefillDatabase.value).toBe("");
expect(dialogs.sqlFilePrefillPreview.value).toBeUndefined();
mocks.store.sqlFileSource = { connectionId: "mysql", database: "app", filePath: "/local/backup.sql" };
await nextTick();
@@ -0,0 +1,90 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
connectionStore: {
activeConnectionId: null as string | null,
connections: [] as Array<{ id: string; db_type: string }>,
getConfig: vi.fn(),
},
queryStore: {
tabs: [] as Array<{ id: string; connectionId: string; database: string; catalog?: string; schema?: string }>,
activeTabId: null as string | null,
createTab: vi.fn(() => "opened-tab"),
updateSql: vi.fn(),
openExternalSqlFile: vi.fn(),
},
readBrowserSqlFile: vi.fn(async () => "select 1"),
toast: vi.fn(),
}));
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => mocks.connectionStore }));
vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => mocks.queryStore }));
vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => ({ editorSettings: { externalSqlEditorMaxMb: 64 } }) }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
vi.mock("@/composables/useLargeSqlFileFallback", () => ({ useLargeSqlFileStreamingFallback: () => ({ openInStreamingExecutorOnTooLarge: vi.fn(() => false) }) }));
vi.mock("@/lib/sql/sqlFileOpen", async (importOriginal) => ({
...(await importOriginal<typeof import("@/lib/sql/sqlFileOpen")>()),
readBrowserSqlFile: mocks.readBrowserSqlFile,
}));
import { useFileDrop } from "@/composables/useFileDrop";
let app: ReturnType<typeof createApp> | undefined;
beforeEach(() => {
vi.clearAllMocks();
mocks.connectionStore.activeConnectionId = null;
mocks.connectionStore.connections = [];
mocks.connectionStore.getConfig.mockReset();
mocks.queryStore.tabs = [];
mocks.queryStore.activeTabId = null;
mocks.queryStore.createTab.mockReturnValue("opened-tab");
});
afterEach(() => {
app?.unmount();
app = undefined;
});
describe("useFileDrop SQL target", () => {
it("uses only an active SQL tab for browser-dropped files", async () => {
let setupFileDrop!: ReturnType<typeof useFileDrop>["setupFileDrop"];
app = createApp(
defineComponent({
setup() {
({ setupFileDrop } = useFileDrop());
return () => h("div");
},
}),
);
app.mount(document.createElement("div"));
await setupFileDrop();
mocks.queryStore.tabs = [{ id: "sql-tab", connectionId: "postgres-1", database: "analytics", catalog: "hive", schema: "reporting" }];
mocks.queryStore.activeTabId = "sql-tab";
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => (connectionId === "postgres-1" ? { id: connectionId, db_type: "postgres" } : undefined));
dispatchSqlDrop("active.sql");
await vi.waitFor(() => expect(mocks.queryStore.createTab).toHaveBeenCalledTimes(1));
expect(mocks.queryStore.createTab).toHaveBeenNthCalledWith(1, "postgres-1", "analytics", "active.sql", "query", "reporting", undefined, "hive");
mocks.queryStore.tabs = [{ id: "mq-tab", connectionId: "kafka-1", database: "invalid", schema: "invalid" }];
mocks.queryStore.activeTabId = "mq-tab";
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => (connectionId === "kafka-1" ? { id: connectionId, db_type: "mq" } : undefined));
dispatchSqlDrop("non-sql.sql");
await vi.waitFor(() => expect(mocks.queryStore.createTab).toHaveBeenCalledTimes(2));
expect(mocks.queryStore.createTab).toHaveBeenNthCalledWith(2, "", "", "non-sql.sql", "query", undefined, undefined, undefined);
});
});
function dispatchSqlDrop(name: string) {
const event = new Event("drop", { bubbles: true, cancelable: true });
Object.defineProperty(event, "dataTransfer", { value: { files: [new File(["select 1"], name)] } });
document.dispatchEvent(event);
}
@@ -8,13 +8,18 @@ import { ExternalSqlFileTooLargeError } from "@/lib/sql/sqlFileOpen";
const mocks = vi.hoisted(() => ({
toast: vi.fn(),
store: {
connections: [] as { id: string }[],
connections: [] as { id: string; db_type: string }[],
sqlFileSource: null as { connectionId: string; database: string; filePath?: string } | null,
getConfig: vi.fn(),
},
queryStore: {
tabs: [] as Array<{ id: string; connectionId: string; database: string; catalog?: string; schema?: string }>,
activeTabId: null as string | null,
},
}));
vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => mocks.store }));
vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => mocks.queryStore }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
import { useLargeSqlFileStreamingFallback } from "@/composables/useLargeSqlFileFallback";
@@ -25,6 +30,8 @@ beforeEach(() => {
mocks.store.connections = [];
mocks.store.sqlFileSource = null;
mocks.store.getConfig.mockReset().mockReturnValue(undefined);
mocks.queryStore.tabs = [];
mocks.queryStore.activeTabId = null;
mocks.toast.mockReset();
});
@@ -67,6 +74,21 @@ describe("useLargeSqlFileStreamingFallback", () => {
expect(String(mocks.toast.mock.calls[0][0])).toContain("100.0 MB");
});
it("passes the active SQL tab target to the streaming executor", () => {
mocks.queryStore.tabs = [{ id: "tab-1", connectionId: "postgres-1", database: "app", schema: "reporting" }];
mocks.queryStore.activeTabId = "tab-1";
mocks.store.getConfig.mockImplementation((connectionId: string) => (connectionId === "postgres-1" ? { id: connectionId, db_type: "postgres" } : undefined));
const outcome = withComposable((fallback) => fallback.openInStreamingExecutorOnTooLarge("/tmp/dbx_export.sql", new ExternalSqlFileTooLargeError(100 * 1024 * 1024, 64 * 1024 * 1024)));
expect(outcome).toBe(true);
expect(mocks.store.sqlFileSource).toEqual({
connectionId: "postgres-1",
database: "app",
filePath: "/tmp/dbx_export.sql",
});
});
it("keeps unrelated open failures as errors", () => {
const outcome = withComposable((fallback) => fallback.openInStreamingExecutorOnTooLarge("/tmp/dbx_export.sql", new Error("disk failure")));