From 68d76c213e2a70eeaae50097abfb21d109107139 Mon Sep 17 00:00:00 2001 From: ecmel Date: Tue, 1 Oct 2024 21:30:52 +0300 Subject: [PATCH 1/2] added import option to overwrite connections --- src/commands/serverCommand.ts | 81 ++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/src/commands/serverCommand.ts b/src/commands/serverCommand.ts index 571d563c..3faba8dc 100644 --- a/src/commands/serverCommand.ts +++ b/src/commands/serverCommand.ts @@ -594,37 +594,96 @@ export async function addImportedConnections( const connMangService = new ConnectionManagementService(); const existingAliases = connMangService.retrieveListOfConnectionsNames(); const localAlreadyExists = existingAliases.has("local"); + + const hasDuplicates = + importedConnections.connections.Insights.some((connection) => + existingAliases.has( + connMangService.checkConnAlias(connection.alias, true), + ), + ) || + importedConnections.connections.KDB.some((connection) => + existingAliases.has( + connMangService.checkConnAlias( + connection.serverAlias, + false, + localAlreadyExists, + ), + ), + ); + + let res: "Duplicate" | "Overwrite" | "Cancel" | undefined = "Duplicate"; + if (hasDuplicates) { + res = await window.showInformationMessage( + "You are importing connections with the same name. Would you like to duplicate, overwrite or cancel the import?", + "Duplicate", + "Overwrite", + "Cancel", + ); + if (!res || res === "Cancel") { + return; + } + } + let counter = 1; + const insights: InsightDetails[] = []; for (const connection of importedConnections.connections.Insights) { let alias = connMangService.checkConnAlias(connection.alias, true); - while (existingAliases.has(alias)) { - alias = `${connection.alias}-${counter}`; - counter++; + if (res === "Overwrite") { + insights.push(connection); + } else { + while (existingAliases.has(alias)) { + alias = `${connection.alias}-${counter}`; + counter++; + } + connection.alias = alias; + await addInsightsConnection(connection); } - connection.alias = alias; - await addInsightsConnection(connection); existingAliases.add(alias); counter = 1; } + const servers: ServerDetails[] = []; for (const connection of importedConnections.connections.KDB) { let alias = connMangService.checkConnAlias( connection.serverAlias, false, localAlreadyExists, ); - while (existingAliases.has(alias)) { - alias = `${connection.serverAlias}-${counter}`; - counter++; + + if (res === "Overwrite") { + servers.push(connection); + } else { + while (existingAliases.has(alias)) { + alias = `${connection.serverAlias}-${counter}`; + counter++; + } + connection.serverAlias = alias; + const isManaged = alias === "local"; + await addKdbConnection(connection, isManaged); } - const isManaged = alias === "local"; - connection.serverAlias = alias; - await addKdbConnection(connection, isManaged); existingAliases.add(alias); counter = 1; } + if (insights.length > 0) { + const config = insights.reduce((config, connection) => { + config[connection.alias] = connection; + return config; + }, getInsights() || {}); + await updateInsights(config); + ext.serverProvider.refreshInsights(config); + } + + if (servers.length > 0) { + const config = servers.reduce((config, connection) => { + config[connection.serverAlias] = connection; + return config; + }, getServers() || {}); + await updateServers(config); + ext.serverProvider.refresh(config); + } + kdbOutputLog("[IMPORT CONNECTION]Connections imported successfully", "INFO"); window.showInformationMessage("Connections imported successfully"); } From 3d31acb0fccab66401e7fcffa11e564b79d5a0b9 Mon Sep 17 00:00:00 2001 From: ecmel Date: Wed, 2 Oct 2024 07:32:16 +0300 Subject: [PATCH 2/2] added test --- test/suite/commands.test.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/suite/commands.test.ts b/test/suite/commands.test.ts index 46471e04..c4bfbe8e 100644 --- a/test/suite/commands.test.ts +++ b/test/suite/commands.test.ts @@ -1283,6 +1283,35 @@ describe("serverCommand", () => { sinon.assert.notCalled(addInsightsConnectionStub); }); + + it("should overwrite connections", async () => { + ext.connectionsList.push(insightsNodeImport1, kdbNodeImport1); + const importedConnections: ExportedConnections = { + connections: { + Insights: [ + { + alias: "testInsight", + server: "testInsight", + auth: false, + }, + ], + KDB: [ + { + serverName: "testKdb", + serverAlias: "testKdb", + serverPort: "1818", + auth: false, + managed: false, + tls: false, + }, + ], + }, + }; + + showInformationMessageStub.returns("Overwrite"); + await serverCommand.addImportedConnections(importedConnections); + sinon.assert.notCalled(addInsightsConnectionStub); + }); }); describe("writeQueryResultsToView", () => {