Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added import option to overwrite connections #432

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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", () => {
Expand Down