fix(sql-file): preserve target database when selecting SQL file
This commit is contained in:
1 parent
dee12449d9
commit
c2d9610d1c
2 files changed
+143
-8
No files matched your search
+108
-3
@@ -148,8 +148,21 @@ vi.mock("@/components/ui/select", () => ({
|
|||||||
Select: defineComponent({
|
Select: defineComponent({
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
props: ["modelValue"],
|
props: ["modelValue"],
|
||||||
setup(props, { attrs, slots }) {
|
emits: ["update:modelValue"],
|
||||||
return () => h("div", { ...attrs, "data-select-value": props.modelValue ?? "" }, slots.default?.());
|
setup(props, { attrs, slots, emit }) {
|
||||||
|
return () =>
|
||||||
|
h(
|
||||||
|
"div",
|
||||||
|
{
|
||||||
|
...attrs,
|
||||||
|
"data-select-value": props.modelValue ?? "",
|
||||||
|
onClick: (e: MouseEvent) => {
|
||||||
|
const target = (e.target as HTMLElement)?.closest?.("[data-select-item]") as HTMLElement | null;
|
||||||
|
if (target?.dataset?.selectItem) emit("update:modelValue", target.dataset.selectItem);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
slots.default?.(),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
SelectContent: passthrough("div"),
|
SelectContent: passthrough("div"),
|
||||||
@@ -167,7 +180,7 @@ vi.mock("@/components/icons/DatabaseIcon.vue", () => ({ default: passthrough("sp
|
|||||||
vi.mock("@/components/connection/ConnectionGroupBadge.vue", () => ({ default: passthrough("span") }));
|
vi.mock("@/components/connection/ConnectionGroupBadge.vue", () => ({ default: passthrough("span") }));
|
||||||
|
|
||||||
import SqlFileExecutionDialog from "./SqlFileExecutionDialog.vue";
|
import SqlFileExecutionDialog from "./SqlFileExecutionDialog.vue";
|
||||||
import { rememberExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
import { rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
||||||
|
|
||||||
let app: ReturnType<typeof createApp> | undefined;
|
let app: ReturnType<typeof createApp> | undefined;
|
||||||
let root: HTMLDivElement | undefined;
|
let root: HTMLDivElement | undefined;
|
||||||
@@ -1036,3 +1049,95 @@ describe("SqlFileExecutionDialog selected-table restore", () => {
|
|||||||
expect(input.value).toBe("/tmp/missing.sql");
|
expect(input.value).toBe("/tmp/missing.sql");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("SqlFileExecutionDialog target preservation (#4844)", () => {
|
||||||
|
it("preserves prefilled database when browsing for a new unassociated SQL file", async () => {
|
||||||
|
mocks.connections = [{ id: "mysql-1", name: "MySQL", db_type: "mysql", driver_profile: "mysql", database: "" }];
|
||||||
|
mocks.queryStore.tabs = [{ id: "tab-1", connectionId: "mysql-1", database: "first_db" }];
|
||||||
|
mocks.queryStore.activeTabId = "tab-1";
|
||||||
|
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["first_db", "target_db"]);
|
||||||
|
mocks.openFileDialog.mockResolvedValueOnce(["/tmp/unassociated.sql"]);
|
||||||
|
|
||||||
|
root = document.createElement("div");
|
||||||
|
document.body.append(root);
|
||||||
|
app = createApp(SqlFileExecutionDialog, { open: true, prefillConnectionId: "mysql-1", prefillDatabase: "target_db" });
|
||||||
|
app.mount(root);
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(mocks.fetchSqlFileTargetOptions).toHaveBeenCalledWith("mysql-1", expect.anything()));
|
||||||
|
expect(root.querySelector('[data-select-value="target_db"]')).not.toBeNull();
|
||||||
|
|
||||||
|
findButton("sqlFile.browse").click();
|
||||||
|
await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/unassociated.sql"));
|
||||||
|
|
||||||
|
// Target database MUST NOT have been overwritten by activeTab database ("first_db")
|
||||||
|
expect(root.querySelector('[data-select-value="target_db"]')).not.toBeNull();
|
||||||
|
|
||||||
|
mocks.executeSqlFiles.mockImplementationOnce(async (request) => {
|
||||||
|
mocks.progressHandler?.(progress(request.executionId, "done"));
|
||||||
|
});
|
||||||
|
findButton("sqlFile.execute").click();
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(mocks.executeSqlFiles).toHaveBeenCalledWith(expect.objectContaining({ connectionId: "mysql-1", database: "target_db" }), ["/tmp/unassociated.sql"]));
|
||||||
|
expect(resolveExternalSqlFileTarget("/tmp/unassociated.sql", () => true, unassociatedExternalSqlFileTarget())).toMatchObject({
|
||||||
|
connectionId: "mysql-1",
|
||||||
|
database: "target_db",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not overwrite prefilled connection and database even if the selected file has a saved target", async () => {
|
||||||
|
mocks.connections = [
|
||||||
|
{ id: "mysql-1", name: "MySQL", db_type: "mysql", driver_profile: "mysql", database: "" },
|
||||||
|
{ id: "mysql-2", name: "MySQL 2", db_type: "mysql", driver_profile: "mysql", database: "" },
|
||||||
|
];
|
||||||
|
rememberExternalSqlFileTarget("/tmp/saved_other.sql", { connectionId: "mysql-2", database: "other_db" });
|
||||||
|
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["first_db", "explicit_prefill_db"]);
|
||||||
|
mocks.openFileDialog.mockResolvedValueOnce(["/tmp/saved_other.sql"]);
|
||||||
|
|
||||||
|
root = document.createElement("div");
|
||||||
|
document.body.append(root);
|
||||||
|
app = createApp(SqlFileExecutionDialog, { open: true, prefillConnectionId: "mysql-1", prefillDatabase: "explicit_prefill_db" });
|
||||||
|
app.mount(root);
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(mocks.fetchSqlFileTargetOptions).toHaveBeenCalledWith("mysql-1", expect.anything()));
|
||||||
|
findButton("sqlFile.browse").click();
|
||||||
|
await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/saved_other.sql"));
|
||||||
|
|
||||||
|
expect(root.querySelector('[data-select-value="mysql-1"]')).not.toBeNull();
|
||||||
|
expect(root.querySelector('[data-select-value="explicit_prefill_db"]')).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves user-selected database in the dialog when browsing an unassociated file", async () => {
|
||||||
|
mocks.connections = [{ id: "mysql-1", name: "MySQL", db_type: "mysql", driver_profile: "mysql", database: "" }];
|
||||||
|
mocks.queryStore.tabs = [{ id: "tab-1", connectionId: "mysql-1", database: "first_db" }];
|
||||||
|
mocks.queryStore.activeTabId = "tab-1";
|
||||||
|
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["first_db", "second_db", "chosen_db"]);
|
||||||
|
mocks.openFileDialog.mockResolvedValueOnce(["/tmp/brand_new.sql"]);
|
||||||
|
|
||||||
|
root = document.createElement("div");
|
||||||
|
document.body.append(root);
|
||||||
|
app = createApp(SqlFileExecutionDialog, { open: true });
|
||||||
|
app.mount(root);
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(mocks.fetchSqlFileTargetOptions).toHaveBeenCalledWith("mysql-1", expect.anything()));
|
||||||
|
|
||||||
|
// User selects "chosen_db" from the database dropdown
|
||||||
|
const chosenDbItem = root.querySelector('[data-select-item="chosen_db"]') as HTMLElement;
|
||||||
|
expect(chosenDbItem).not.toBeNull();
|
||||||
|
chosenDbItem.click();
|
||||||
|
await nextTick();
|
||||||
|
expect(root.querySelector('[data-select-value="chosen_db"]')).not.toBeNull();
|
||||||
|
|
||||||
|
findButton("sqlFile.browse").click();
|
||||||
|
await vi.waitFor(() => expect(mocks.previewSqlFile).toHaveBeenCalledWith("/tmp/brand_new.sql"));
|
||||||
|
|
||||||
|
// Selection MUST be preserved, not overwritten with "first_db" from activeTab
|
||||||
|
expect(root.querySelector('[data-select-value="chosen_db"]')).not.toBeNull();
|
||||||
|
|
||||||
|
mocks.executeSqlFiles.mockImplementationOnce(async (request) => {
|
||||||
|
mocks.progressHandler?.(progress(request.executionId, "done"));
|
||||||
|
});
|
||||||
|
findButton("sqlFile.execute").click();
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(mocks.executeSqlFiles).toHaveBeenCalledWith(expect.objectContaining({ connectionId: "mysql-1", database: "chosen_db" }), ["/tmp/brand_new.sql"]));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -24,7 +24,7 @@ import { formatError, isManualTransactionSessionExpired } from "@/lib/backend/er
|
|||||||
import { fetchSqlFileTargetOptions, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions";
|
import { fetchSqlFileTargetOptions, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions";
|
||||||
import { requiresSqlFileTargetDatabaseSelection, supportsConnectionLevelDatabaseBootstrap } from "@/lib/connection/connectionLevelDatabaseBootstrap";
|
import { requiresSqlFileTargetDatabaseSelection, supportsConnectionLevelDatabaseBootstrap } from "@/lib/connection/connectionLevelDatabaseBootstrap";
|
||||||
import { beginManualTransaction, commitManualTransaction, rollbackManualTransaction, cancelSqlFileExecution, executeSqlFiles, inspectSqlFileTables, listenSqlFileProgress, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus, type SqlFileTable } from "@/lib/backend/api";
|
import { beginManualTransaction, commitManualTransaction, rollbackManualTransaction, cancelSqlFileExecution, executeSqlFiles, inspectSqlFileTables, listenSqlFileProgress, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus, type SqlFileTable } from "@/lib/backend/api";
|
||||||
import { activeTabExternalSqlFileTarget, resolveExternalSqlFileTargetForActiveTab, type ExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
import { activeTabExternalSqlFileTarget, rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget, type ExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
|
||||||
import { isSqlFilePath } from "@/lib/sql/sqlFileOpen";
|
import { isSqlFilePath } from "@/lib/sql/sqlFileOpen";
|
||||||
import { buildDisplayFileNames, tooltipText as computeTooltipText } from "./sqlFilePreviewLabel";
|
import { buildDisplayFileNames, tooltipText as computeTooltipText } from "./sqlFilePreviewLabel";
|
||||||
import { parseSqlFilePathInput } from "./sqlFilePathInput";
|
import { parseSqlFilePathInput } from "./sqlFilePathInput";
|
||||||
@@ -427,6 +427,8 @@ function chooseNamespace(names: string[], id: string) {
|
|||||||
const preferred = preferredTarget.value?.connectionId === id ? preferredTarget.value : undefined;
|
const preferred = preferredTarget.value?.connectionId === id ? preferredTarget.value : undefined;
|
||||||
const preferredNamespace = preferred ? (optionsAreSchemas ? preferred.schema : preferred.database) : optionsAreSchemas ? (id === props.prefillConnectionId ? props.prefillSchema : undefined) : props.prefillDatabase;
|
const preferredNamespace = preferred ? (optionsAreSchemas ? preferred.schema : preferred.database) : optionsAreSchemas ? (id === props.prefillConnectionId ? props.prefillSchema : undefined) : props.prefillDatabase;
|
||||||
const configuredNamespace = optionsAreSchemas ? (connection?.default_schema ?? "") : (connection?.database ?? "");
|
const configuredNamespace = optionsAreSchemas ? (connection?.default_schema ?? "") : (connection?.database ?? "");
|
||||||
|
const currentNamespace = targetNamespace.value.trim();
|
||||||
|
if (currentNamespace && connectionId.value === id && names.includes(currentNamespace)) return currentNamespace;
|
||||||
if (names.length > 0) {
|
if (names.length > 0) {
|
||||||
if (preferredNamespace && names.includes(preferredNamespace)) return preferredNamespace;
|
if (preferredNamespace && names.includes(preferredNamespace)) return preferredNamespace;
|
||||||
if (configuredNamespace && names.includes(configuredNamespace)) return configuredNamespace;
|
if (configuredNamespace && names.includes(configuredNamespace)) return configuredNamespace;
|
||||||
@@ -435,6 +437,17 @@ function chooseNamespace(names: string[], id: string) {
|
|||||||
return preferredNamespace ?? configuredNamespace;
|
return preferredNamespace ?? configuredNamespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
watch(targetNamespace, (namespace) => {
|
||||||
|
if (!connectionId.value) return;
|
||||||
|
const connection = store.getConfig(connectionId.value);
|
||||||
|
const optionsAreSchemas = namespaceOptionsAreSchemas(connection);
|
||||||
|
preferredTarget.value = {
|
||||||
|
connectionId: connectionId.value,
|
||||||
|
database: optionsAreSchemas ? connection?.database?.trim() || "" : namespace.trim(),
|
||||||
|
...(optionsAreSchemas ? { schema: namespace.trim() } : {}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
function sqlFileExecutionTarget(): SqlFileExecutionTarget {
|
function sqlFileExecutionTarget(): SqlFileExecutionTarget {
|
||||||
const namespace = targetNamespace.value.trim();
|
const namespace = targetNamespace.value.trim();
|
||||||
const connection = store.getConfig(connectionId.value);
|
const connection = store.getConfig(connectionId.value);
|
||||||
@@ -534,10 +547,14 @@ async function loadPreviews(filesOrPaths: Array<string | File>, resolveSelectedF
|
|||||||
nextPreviews.push(await previewSelectedSqlFile(fileOrPath));
|
nextPreviews.push(await previewSelectedSqlFile(fileOrPath));
|
||||||
}
|
}
|
||||||
previews.value = nextPreviews;
|
previews.value = nextPreviews;
|
||||||
if (resolveSelectedFileTarget) {
|
if (resolveSelectedFileTarget && !props.prefillConnectionId) {
|
||||||
const firstPath = typeof filesOrPaths[0] === "string" && isSqlFilePath(filesOrPaths[0]) ? filesOrPaths[0] : undefined;
|
const firstPath = typeof filesOrPaths[0] === "string" && isSqlFilePath(filesOrPaths[0]) ? filesOrPaths[0] : undefined;
|
||||||
const target = firstPath ? resolveExternalSqlFileTargetForActiveTab(firstPath, queryStore.tabs, queryStore.activeTabId, (connectionId) => store.getConfig(connectionId)) : activeTabExternalSqlFileTarget(queryStore.tabs, queryStore.activeTabId, (connectionId) => store.getConfig(connectionId));
|
const remembered = firstPath ? resolveExternalSqlFileTarget(firstPath, (id) => sqlConnections.value.some((c) => c.id === id), unassociatedExternalSqlFileTarget()) : undefined;
|
||||||
applyTarget(target);
|
if (remembered?.connectionId) {
|
||||||
|
applyTarget(remembered);
|
||||||
|
} else if (!connectionId.value) {
|
||||||
|
applyTarget(activeTabExternalSqlFileTarget(queryStore.tabs, queryStore.activeTabId, (connectionId) => store.getConfig(connectionId)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
toast(e?.message || String(e), 5000);
|
toast(e?.message || String(e), 5000);
|
||||||
@@ -771,7 +788,20 @@ async function startExecution() {
|
|||||||
unlisten();
|
unlisten();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (completedSuccessfully && !txnSessionId.value) await refreshTargetAfterImport(target);
|
if (completedSuccessfully) {
|
||||||
|
if (isDesktopRuntime) {
|
||||||
|
for (const item of previews.value) {
|
||||||
|
if (item.filePath) {
|
||||||
|
rememberExternalSqlFileTarget(item.filePath, {
|
||||||
|
connectionId: connectionId.value,
|
||||||
|
database: target.database,
|
||||||
|
schema: target.schema,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!txnSessionId.value) await refreshTargetAfterImport(target);
|
||||||
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
terminalStatus.value = cancelRequested.value ? "cancelled" : "error";
|
terminalStatus.value = cancelRequested.value ? "cancelled" : "error";
|
||||||
terminalError.value = e?.message || String(e);
|
terminalError.value = e?.message || String(e);
|
||||||
|
|||||||
Reference in new issue
Block a user