feat(config): support selective connection import and export
This commit is contained in:
1 parent
89e0f1734b
commit
e4577abeed
21 files changed
+1171
-175
No files matched your search
@@ -0,0 +1,87 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, nextTick, type App } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
toast: vi.fn(),
|
||||
store: {
|
||||
connections: [] as ConnectionConfig[],
|
||||
transferSource: null,
|
||||
schemaDiffSource: null,
|
||||
dataCompareSource: null,
|
||||
sqlFileSource: null,
|
||||
diagramSource: null,
|
||||
docsSource: null,
|
||||
tableImportSource: null,
|
||||
tableDataGenerateSource: null,
|
||||
fieldLineageSource: null,
|
||||
databaseSearchSource: null,
|
||||
databaseExportSource: null,
|
||||
readImportFile: vi.fn(),
|
||||
parseConnectionsImport: vi.fn(),
|
||||
applyConnectionsImport: vi.fn(),
|
||||
applyDataGripKeychainPasswords: vi.fn(),
|
||||
exportConnectionsToFile: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => mocks.store }));
|
||||
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
|
||||
|
||||
import { useDialogSources } from "@/composables/useDialogSources";
|
||||
|
||||
const mountedApps: App[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const app of mountedApps.splice(0)) app.unmount();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
function conn(id: string): ConnectionConfig {
|
||||
return { id, name: "Imported", db_type: "mysql", host: "127.0.0.1", port: 3306, username: "root", password: "secret" };
|
||||
}
|
||||
|
||||
async function mountDialogs() {
|
||||
let dialogs!: ReturnType<typeof useDialogSources>;
|
||||
const container = document.createElement("div");
|
||||
document.body.append(container);
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
dialogs = useDialogSources();
|
||||
return () => h("div");
|
||||
},
|
||||
}),
|
||||
);
|
||||
mountedApps.push(app);
|
||||
app.use(i18n);
|
||||
app.mount(container);
|
||||
await nextTick();
|
||||
return dialogs;
|
||||
}
|
||||
|
||||
describe("useDialogSources", () => {
|
||||
it("runs the final import confirmation as a single flight", async () => {
|
||||
let resolveApply!: (value: { count: number }) => void;
|
||||
const applyPromise = new Promise<{ count: number }>((resolve) => {
|
||||
resolveApply = resolve;
|
||||
});
|
||||
mocks.store.readImportFile.mockResolvedValue({ content: "{}", encrypted: false });
|
||||
mocks.store.parseConnectionsImport.mockResolvedValue({ connections: [conn("imported")] });
|
||||
mocks.store.applyConnectionsImport.mockReturnValue(applyPromise);
|
||||
|
||||
const dialogs = await mountDialogs();
|
||||
await dialogs.onImportClick("dbx");
|
||||
dialogs.onConfigConnectionSelectConfirm(["imported"]);
|
||||
dialogs.onConfigConnectionSelectConfirm(["imported"]);
|
||||
|
||||
expect(mocks.store.applyConnectionsImport).toHaveBeenCalledTimes(1);
|
||||
|
||||
resolveApply({ count: 1 });
|
||||
await applyPromise;
|
||||
await nextTick();
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,8 @@ import { useI18n } from "vue-i18n";
|
||||
import { useConnectionStore } from "@/stores/connectionStore";
|
||||
import { useToast } from "@/composables/useToast";
|
||||
import { hasSidebarLayoutEntries } from "@/lib/sidebar/sidebarLayout";
|
||||
import type { SidebarLayout } from "@/types/database";
|
||||
import type { ConnectionConfigBundle } from "@/lib/connection/connectionConfigTransfer";
|
||||
import type { ConnectionConfig, SidebarLayout } from "@/types/database";
|
||||
|
||||
const showTransferDialog = ref(false);
|
||||
const showSchemaDiffDialog = ref(false);
|
||||
@@ -22,6 +23,13 @@ const showConfigPassphraseDialog = ref(false);
|
||||
const configPassphraseMode = ref<"export" | "import">("export");
|
||||
const configPassphraseError = ref("");
|
||||
const pendingImportContent = ref("");
|
||||
const showConfigConnectionSelectDialog = ref(false);
|
||||
const configConnectionSelectMode = ref<"export" | "import">("export");
|
||||
const configConnectionSelectList = ref<ConnectionConfig[]>([]);
|
||||
const pendingExportConnectionIds = ref<string[]>([]);
|
||||
const pendingImportPreview = ref<ConnectionConfigBundle | null>(null);
|
||||
const pendingImportSource = ref<"dbx" | "navicat" | "dbeaver" | "datagrip">("dbx");
|
||||
const applyingImportSelection = ref(false);
|
||||
|
||||
const transferPrefillConnectionId = ref("");
|
||||
const transferPrefillDatabase = ref("");
|
||||
@@ -266,8 +274,56 @@ export function useDialogSources() {
|
||||
);
|
||||
} // end watchersRegistered
|
||||
|
||||
function clearPendingImportState() {
|
||||
pendingImportContent.value = "";
|
||||
pendingImportPreview.value = null;
|
||||
pendingImportSource.value = "dbx";
|
||||
configConnectionSelectList.value = [];
|
||||
configPassphraseError.value = "";
|
||||
}
|
||||
|
||||
function clearPendingExportState() {
|
||||
pendingExportConnectionIds.value = [];
|
||||
configConnectionSelectList.value = [];
|
||||
configPassphraseError.value = "";
|
||||
}
|
||||
|
||||
function openConnectionSelect(mode: "export" | "import", connections: ConnectionConfig[]) {
|
||||
configConnectionSelectMode.value = mode;
|
||||
configConnectionSelectList.value = connections;
|
||||
showConfigConnectionSelectDialog.value = true;
|
||||
}
|
||||
|
||||
function importSuccessMessage(source: "dbx" | "navicat" | "dbeaver" | "datagrip", count: number, keychainFilled = 0) {
|
||||
if (count <= 0) return t("configExport.importNone");
|
||||
if (source === "navicat") return t("configExport.importNavicatSuccess", { count });
|
||||
if (source === "dbeaver") return t("configExport.importDbeaverSuccess", { count });
|
||||
if (source === "datagrip") return t("configExport.importDatagripSuccess", { count, filled: keychainFilled });
|
||||
return t("configExport.importSuccess", { count });
|
||||
}
|
||||
|
||||
async function finishImport(source: "dbx" | "navicat" | "dbeaver" | "datagrip", count: number, layout?: SidebarLayout) {
|
||||
let keychainFilled = 0;
|
||||
if (source === "datagrip" && count > 0) {
|
||||
keychainFilled = await connectionStore.applyDataGripKeychainPasswords();
|
||||
}
|
||||
toast(importSuccessMessage(source, count, keychainFilled), source === "dbx" ? 2000 : 4000);
|
||||
if (hasSidebarLayoutEntries(layout)) {
|
||||
pendingImportLayout.value = layout;
|
||||
showImportLayoutConfirm.value = true;
|
||||
}
|
||||
clearPendingImportState();
|
||||
}
|
||||
|
||||
// Config export/import helpers
|
||||
function onExportClick() {
|
||||
clearPendingExportState();
|
||||
openConnectionSelect("export", connectionStore.connections);
|
||||
}
|
||||
|
||||
function onExportConnectionsSelected(connectionIds: string[]) {
|
||||
pendingExportConnectionIds.value = connectionIds;
|
||||
showConfigConnectionSelectDialog.value = false;
|
||||
configPassphraseMode.value = "export";
|
||||
configPassphraseError.value = "";
|
||||
showConfigPassphraseDialog.value = true;
|
||||
@@ -275,8 +331,9 @@ export function useDialogSources() {
|
||||
|
||||
async function onExportConfirm(passphrase: string) {
|
||||
try {
|
||||
await connectionStore.exportConnectionsToFile(passphrase);
|
||||
await connectionStore.exportConnectionsToFile(passphrase, pendingExportConnectionIds.value);
|
||||
showConfigPassphraseDialog.value = false;
|
||||
clearPendingExportState();
|
||||
toast(t("configExport.exportSuccess"), 2000);
|
||||
} catch (e: any) {
|
||||
configPassphraseError.value = e?.message === "crypto_unavailable" ? t("configExport.cryptoUnavailable") : e?.message || String(e);
|
||||
@@ -287,54 +344,80 @@ export function useDialogSources() {
|
||||
try {
|
||||
const result = await connectionStore.readImportFile(source);
|
||||
if (!result) return;
|
||||
clearPendingImportState();
|
||||
pendingImportContent.value = result.content;
|
||||
pendingImportSource.value = source;
|
||||
if (result.encrypted) {
|
||||
configPassphraseMode.value = "import";
|
||||
configPassphraseError.value = "";
|
||||
showConfigPassphraseDialog.value = true;
|
||||
} else {
|
||||
const { count, layout } = await connectionStore.importConnectionsFromFile(result.content, null);
|
||||
// For DataGrip imports, read Keychain passwords
|
||||
let keychainFilled = 0;
|
||||
if (source === "datagrip" && count > 0) {
|
||||
keychainFilled = await connectionStore.applyDataGripKeychainPasswords();
|
||||
}
|
||||
toast(
|
||||
count > 0
|
||||
? source === "navicat"
|
||||
? t("configExport.importNavicatSuccess", { count })
|
||||
: source === "dbeaver"
|
||||
? t("configExport.importDbeaverSuccess", { count })
|
||||
: source === "datagrip"
|
||||
? t("configExport.importDatagripSuccess", { count: count, filled: keychainFilled })
|
||||
: t("configExport.importSuccess", { count })
|
||||
: t("configExport.importNone"),
|
||||
4000,
|
||||
);
|
||||
if (hasSidebarLayoutEntries(layout)) {
|
||||
pendingImportLayout.value = layout;
|
||||
showImportLayoutConfirm.value = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
const preview = await connectionStore.parseConnectionsImport(result.content, null);
|
||||
pendingImportPreview.value = preview;
|
||||
if (source === "dbx") {
|
||||
openConnectionSelect("import", preview.connections);
|
||||
return;
|
||||
}
|
||||
const { count, layout } = await connectionStore.applyConnectionsImport(preview);
|
||||
await finishImport(source, count, layout);
|
||||
} catch (e: any) {
|
||||
clearPendingImportState();
|
||||
toast(e?.message || String(e), 4000);
|
||||
}
|
||||
}
|
||||
|
||||
async function onImportConfirm(passphrase: string) {
|
||||
try {
|
||||
const { count, layout } = await connectionStore.importConnectionsFromFile(pendingImportContent.value, passphrase);
|
||||
const preview = await connectionStore.parseConnectionsImport(pendingImportContent.value, passphrase);
|
||||
pendingImportPreview.value = preview;
|
||||
showConfigPassphraseDialog.value = false;
|
||||
toast(count > 0 ? t("configExport.importSuccess", { count }) : t("configExport.importNone"), 2000);
|
||||
if (hasSidebarLayoutEntries(layout)) {
|
||||
pendingImportLayout.value = layout;
|
||||
showImportLayoutConfirm.value = true;
|
||||
}
|
||||
configPassphraseError.value = "";
|
||||
openConnectionSelect("import", preview.connections);
|
||||
} catch (e: any) {
|
||||
configPassphraseError.value = e?.message === "wrong_passphrase" ? t("configExport.wrongPassphrase") : e?.message === "crypto_unavailable" ? t("configExport.cryptoUnavailable") : e?.message || String(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function onImportConnectionsSelected(connectionIds: string[]) {
|
||||
const preview = pendingImportPreview.value;
|
||||
if (!preview) return;
|
||||
if (applyingImportSelection.value) return;
|
||||
applyingImportSelection.value = true;
|
||||
try {
|
||||
const { count, layout } = await connectionStore.applyConnectionsImport(preview, connectionIds);
|
||||
showConfigConnectionSelectDialog.value = false;
|
||||
await finishImport(pendingImportSource.value, count, layout);
|
||||
} catch (e: any) {
|
||||
toast(e?.message || String(e), 4000);
|
||||
} finally {
|
||||
applyingImportSelection.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onConfigConnectionSelectConfirm(connectionIds: string[]) {
|
||||
if (configConnectionSelectMode.value === "export") {
|
||||
onExportConnectionsSelected(connectionIds);
|
||||
return;
|
||||
}
|
||||
void onImportConnectionsSelected(connectionIds);
|
||||
}
|
||||
|
||||
function onConfigConnectionSelectOpenChange(open: boolean) {
|
||||
if (!open && configConnectionSelectMode.value === "import" && applyingImportSelection.value) return;
|
||||
showConfigConnectionSelectDialog.value = open;
|
||||
// Export selection closing to open the passphrase dialog must keep the
|
||||
// chosen ids. Only import preview is discarded when the user cancels.
|
||||
if (!open && configConnectionSelectMode.value === "import") clearPendingImportState();
|
||||
}
|
||||
|
||||
function onConfigPassphraseOpenChange(open: boolean) {
|
||||
showConfigPassphraseDialog.value = open;
|
||||
if (open) return;
|
||||
if (configPassphraseMode.value === "export") clearPendingExportState();
|
||||
else if (!pendingImportPreview.value) clearPendingImportState();
|
||||
}
|
||||
|
||||
return {
|
||||
showTransferDialog,
|
||||
showSchemaDiffDialog,
|
||||
@@ -353,6 +436,10 @@ export function useDialogSources() {
|
||||
configPassphraseMode,
|
||||
configPassphraseError,
|
||||
pendingImportContent,
|
||||
showConfigConnectionSelectDialog,
|
||||
applyingImportSelection,
|
||||
configConnectionSelectMode,
|
||||
configConnectionSelectList,
|
||||
transferPrefillConnectionId,
|
||||
transferPrefillDatabase,
|
||||
transferPrefillCatalog,
|
||||
@@ -404,5 +491,8 @@ export function useDialogSources() {
|
||||
onExportConfirm,
|
||||
onImportClick,
|
||||
onImportConfirm,
|
||||
onConfigConnectionSelectConfirm,
|
||||
onConfigConnectionSelectOpenChange,
|
||||
onConfigPassphraseOpenChange,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user