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

HCK-8816: queries optimization #120

Merged
merged 16 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module.exports = {
await getExternalBrowserUrl(connectionInfo, logger, callback, app);
} else {
const client = await connect(connectionInfo, logger, () => {}, app);
await logDatabaseVersion(client, logger);
await logDatabaseVersion({ client, logger });
}
callback(null);
} catch (error) {
Expand Down
37 changes: 20 additions & 17 deletions reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const crypto = require('crypto');
const randomstring = require('randomstring');
const base64url = require('base64url');
const { getClient, setClient, clearClient } = require('./connectionState');
const { clientManager } = require('./clientManager');
const { getObjectsFromDatabase, getDatabaseCollationOption } = require('./databaseService/databaseService');
const {
reverseCollectionsToJSON,
Expand All @@ -21,19 +21,21 @@ const { prepareError } = require('./databaseService/helpers/errorService');

module.exports = {
async connect(connectionInfo, logger, callback, app) {
const client = getClient();
const sshService = app.require('@hackolade/ssh-service');
const client = clientManager.getClient();

if (!client) {
await setClient(connectionInfo, sshService, 0, logger);
return getClient();
return await clientManager.initClient({
connectionInfo,
logger,
sshService: app.require('@hackolade/ssh-service'),
chulanovskyi-bs marked this conversation as resolved.
Show resolved Hide resolved
});
}

return client;
},

disconnect(connectionInfo, logger, callback, app) {
const sshService = app.require('@hackolade/ssh-service');
clearClient(sshService);
disconnect(connectionInfo, logger, callback) {
clientManager.clearClient();
callback();
},

Expand All @@ -44,9 +46,9 @@ module.exports = {
await this.getExternalBrowserUrl(connectionInfo, logger, callback, app);
} else {
const client = await this.connect(connectionInfo, logger, () => {}, app);
await logDatabaseVersion(client, logger);
await logDatabaseVersion({ client, logger });
}
callback(null);
callback();
} catch (error) {
const errorWithUpdatedInfo = prepareError({ error });
logger.log(
Expand Down Expand Up @@ -88,16 +90,17 @@ module.exports = {
async getDbCollectionsNames(connectionInfo, logger, callback, app) {
try {
logInfo('Retrieving databases and tables information', connectionInfo, logger);

const client = await this.connect(connectionInfo, logger, () => {}, app);
if (!client.config.database) {
throw new Error('No database specified');
}

await logDatabaseVersion(client, logger);
await logDatabaseVersion({ client, logger });

const objects = await getObjectsFromDatabase(client);
const dbName = client.config.database;
const collationData = (await getDatabaseCollationOption(client, dbName, logger)) || [];
const collationData = (await getDatabaseCollationOption({ client, dbName, logger })) || [];
logger.log('info', { collation: collationData[0] }, 'Database collation');
callback(null, objects);
} catch (error) {
Expand All @@ -120,16 +123,16 @@ module.exports = {
logger.log('info', collectionsInfo, 'Retrieving schema', collectionsInfo.hiddenKeys);
logger.progress({ message: 'Start reverse-engineering process', containerName: '', entityName: '' });
const { collections } = collectionsInfo.collectionData;
const client = getClient();
const dbName = client.config.database;
if (!dbName) {
const client = clientManager.getClient();
const dbName = client?.config.database;
if (!client || !dbName) {
throw new Error('No database specified');
}

const reverseEngineeringOptions = getOptionsFromConnectionInfo(collectionsInfo);
const [jsonSchemas, relationships] = await Promise.all([
await reverseCollectionsToJSON(logger)(client, collections, reverseEngineeringOptions),
await getCollectionsRelationships(logger)(client, collections),
await reverseCollectionsToJSON({ client, tablesInfo: collections, reverseEngineeringOptions, logger }),
await getCollectionsRelationships({ client, tablesInfo: collections, logger }),
]);

const jsonSchemasWithDescriptionComments = await getJsonSchemasWithInjectedDescriptionComments({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
const { getConnectionClient } = require('./databaseService/databaseService');

const stateInstance = {
_client: null,
_isSshTunnel: false,
getClient: () => this._client,
setClient: async (connectionInfo, sshService, attempts = 0, logger) => {
if (connectionInfo.ssh && !this._isSshTunnel) {
class ClientManager {
#client = null;
#sshService = null;
#isSshTunnel = false;

getClient() {
return this.#client;
}

async initClient({ connectionInfo, sshService, attempts = 0, logger }) {
if (!this.#sshService) {
this.#sshService = sshService;
}

let connectionParams = { ...connectionInfo };

if (connectionInfo.ssh && !this.#isSshTunnel) {
const { options } = await sshService.openTunnel({
sshAuthMethod: connectionInfo.ssh_method === 'privateKey' ? 'IDENTITY_FILE' : 'USER_PASSWORD',
sshTunnelHostname: connectionInfo.ssh_host,
Expand All @@ -18,44 +29,51 @@ const stateInstance = {
port: connectionInfo.port,
});

this._isSshTunnel = true;
connectionInfo = {
this.#isSshTunnel = true;

connectionParams = {
...connectionInfo,
...options,
};
}

try {
this._client = await getConnectionClient(connectionInfo, logger);
this.#client = await getConnectionClient({ connectionInfo: connectionParams, logger });

return this.#client;
} catch (error) {
const encryptConnection =
connectionInfo.encryptConnection === undefined || Boolean(connectionInfo.encryptConnection);
connectionParams.encryptConnection === undefined || Boolean(connectionParams.encryptConnection);

const isEncryptedConnectionToLocalInstance =
error.message.includes('self signed certificate') && encryptConnection;

if (isEncryptedConnectionToLocalInstance && attempts <= 0) {
return stateInstance.setClient(
{
...connectionInfo,
return this.initClient({
connectionInfo: {
...connectionParams,
encryptConnection: false,
},
sshService,
attempts + 1,
attempts: attempts + 1,
logger,
);
});
}

throw error;
}
},
clearClient: async sshService => {
this._client = null;
}

clearClient() {
this.#client = null;

if (this._isSshTunnel) {
await sshService.closeConsumer();
this._isSshTunnel = false;
if (this.#isSshTunnel && this.#sshService) {
this.#sshService.closeConsumer();
this.#isSshTunnel = false;
}
},
};
}
}

module.exports = stateInstance;
module.exports = {
clientManager: new ClientManager(),
};
Loading
Loading