feat(sql-editor): reuse active connection for opened files
Closes #9263
This commit is contained in:
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")));
|
||||
|
||||
|
||||
@@ -209,13 +209,13 @@ export function useDialogSources() {
|
||||
},
|
||||
);
|
||||
|
||||
// Clear the pre-filled file path once the dialog closes so a later open
|
||||
// via the toolbar (which doesn't go through sqlFileSource) doesn't re-load
|
||||
// the previously previewed file. prefillConnectionId/database are harmless
|
||||
// when stale (they only preselect dropdowns), but a stale path triggers an
|
||||
// async file read + preview render — a visible side effect.
|
||||
// Clear the complete prefill once the dialog closes. A later toolbar open
|
||||
// derives its target from the then-active SQL tab, so neither the old path
|
||||
// nor its connection context may leak into that session.
|
||||
watch(showSqlFileDialog, (open) => {
|
||||
if (!open) {
|
||||
sqlFilePrefillConnectionId.value = "";
|
||||
sqlFilePrefillDatabase.value = "";
|
||||
sqlFilePrefillFilePath.value = "";
|
||||
sqlFilePrefillPreview.value = undefined;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import * as api from "@/lib/backend/api";
|
||||
import type { ConnectionConfig, ExternalSqlFileVersion } from "@/types/database";
|
||||
import { detectDatabaseFileType } from "@/lib/database/databaseFileDetection";
|
||||
import { externalSqlEditorMaxBytes, externalSqlFileOpenErrorMessage, readBrowserSqlFile } from "@/lib/sql/sqlFileOpen";
|
||||
import { resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
||||
import { activeTabExternalSqlFileTarget, resolveExternalSqlFileTargetForActiveTab } from "@/lib/sql/externalSqlFileTarget";
|
||||
|
||||
function isSqlFilePath(path: string): boolean {
|
||||
return /\.sql$/i.test(path);
|
||||
@@ -30,13 +30,11 @@ export function useFileDrop() {
|
||||
|
||||
async function openDroppedSqlFile(name: string, content: string, path?: string, version?: ExternalSqlFileVersion) {
|
||||
if (path) {
|
||||
const target = resolveExternalSqlFileTarget(path, (savedConnectionId) => !!connectionStore.getConfig(savedConnectionId), unassociatedExternalSqlFileTarget());
|
||||
const target = resolveExternalSqlFileTargetForActiveTab(path, queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId));
|
||||
queryStore.openExternalSqlFile(target.connectionId, target.database, path, content, version, target.catalog, target.schema);
|
||||
} else {
|
||||
const connectionId = connectionStore.activeConnectionId || connectionStore.connections[0]?.id || "";
|
||||
const connection = connectionId ? connectionStore.getConfig(connectionId) : undefined;
|
||||
const database = connection?.database || "";
|
||||
const tabId = queryStore.createTab(connectionId, database, name, "query");
|
||||
const target = activeTabExternalSqlFileTarget(queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId));
|
||||
const tabId = queryStore.createTab(target.connectionId, target.database, name, "query", target.schema, undefined, target.catalog);
|
||||
queryStore.updateSql(tabId, content);
|
||||
}
|
||||
toast(t("welcome.fileOpened", { name }));
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { useConnectionStore } from "@/stores/connectionStore";
|
||||
import { useQueryStore } from "@/stores/queryStore";
|
||||
import { useToast } from "@/composables/useToast";
|
||||
import { resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
||||
import { resolveExternalSqlFileTargetForActiveTab } from "@/lib/sql/externalSqlFileTarget";
|
||||
import { formatSqlFileSize, isExternalSqlFileTooLargeError, isSqlFilePath } from "@/lib/sql/sqlFileOpen";
|
||||
|
||||
/**
|
||||
@@ -12,10 +13,11 @@ import { formatSqlFileSize, isExternalSqlFileTooLargeError, isSqlFilePath } from
|
||||
export function useLargeSqlFileStreamingFallback() {
|
||||
const { t } = useI18n();
|
||||
const connectionStore = useConnectionStore();
|
||||
const queryStore = useQueryStore();
|
||||
const { toast } = useToast();
|
||||
|
||||
function openInStreamingExecutor(path: string, sizeBytes: number) {
|
||||
const target = resolveExternalSqlFileTarget(path, (savedConnectionId) => !!connectionStore.getConfig(savedConnectionId), unassociatedExternalSqlFileTarget());
|
||||
const target = resolveExternalSqlFileTargetForActiveTab(path, queryStore.tabs, queryStore.activeTabId, (connectionId) => connectionStore.getConfig(connectionId));
|
||||
connectionStore.sqlFileSource = {
|
||||
connectionId: target.connectionId,
|
||||
database: target.database,
|
||||
|
||||
Reference in new issue
Block a user