Skip to content

Commit

Permalink
Merge pull request #432 from KxSystems/KXI-53789
Browse files Browse the repository at this point in the history
Added import option to overwrite connections
  • Loading branch information
ecmel authored Oct 2, 2024
2 parents 3e2e279 + 3d31acb commit 5a5932d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 11 deletions.
81 changes: 70 additions & 11 deletions src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
29 changes: 29 additions & 0 deletions test/suite/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,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", () => {
Expand Down

0 comments on commit 5a5932d

Please sign in to comment.