Skip to content

Commit

Permalink
Fixed RE of indexes and functions from PostgreSQL:10
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii4as committed Jan 26, 2022
1 parent b04051b commit 2c74008
Show file tree
Hide file tree
Showing 14 changed files with 1,596 additions and 1,537 deletions.
420 changes: 210 additions & 210 deletions reverse_engineering/api.js

Large diffs are not rendered by default.

214 changes: 107 additions & 107 deletions reverse_engineering/helpers/connectionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,125 @@ const ssh = require('tunnel-ssh');
const pg = require('pg');

const getSshConfig = info => {
const config = {
username: info.ssh_user,
host: info.ssh_host,
port: info.ssh_port,
dstHost: info.host,
dstPort: info.port,
localHost: '127.0.0.1',
localPort: info.port,
keepAlive: true,
};

if (info.ssh_method === 'privateKey') {
return Object.assign({}, config, {
privateKey: fs.readFileSync(info.ssh_key_file),
passphrase: info.ssh_key_passphrase,
});
} else {
return Object.assign({}, config, {
password: info.ssh_password,
});
}
const config = {
username: info.ssh_user,
host: info.ssh_host,
port: info.ssh_port,
dstHost: info.host,
dstPort: info.port,
localHost: '127.0.0.1',
localPort: info.port,
keepAlive: true,
};

if (info.ssh_method === 'privateKey') {
return Object.assign({}, config, {
privateKey: fs.readFileSync(info.ssh_key_file),
passphrase: info.ssh_key_passphrase,
});
} else {
return Object.assign({}, config, {
password: info.ssh_password,
});
}
};

const connectViaSsh = info =>
new Promise((resolve, reject) => {
ssh(getSshConfig(info), (err, tunnel) => {
if (err) {
reject(err);
} else {
resolve({
tunnel,
info: Object.assign({}, info, {
host: '127.0.0.1',
}),
});
}
});
});
new Promise((resolve, reject) => {
ssh(getSshConfig(info), (err, tunnel) => {
if (err) {
reject(err);
} else {
resolve({
tunnel,
info: Object.assign({}, info, {
host: '127.0.0.1',
}),
});
}
});
});

const getSslOptions = (connectionInfo, logger) => {
const sslType = mapSslType(connectionInfo.sslType);

if (!sslType || sslType === 'disable') {
return false;
}

if (sslType === 'allow') {
return true;
}

let sslOptions = {
checkServerIdentity(hostname, cert) {
logger.info('Certificate', {
hostname,
cert: {
subject: cert.subject,
issuer: cert.issuer,
valid_from: cert.valid_from,
valid_to: cert.valid_to,
},
});
}
};

if (fs.existsSync(connectionInfo.certAuthority)) {
sslOptions.ca = fs.readFileSync(connectionInfo.certAuthority).toString();
}

if (fs.existsSync(connectionInfo.clientCert)) {
sslOptions.cert = fs.readFileSync(connectionInfo.clientCert).toString();
}

if (fs.existsSync(connectionInfo.clientPrivateKey)) {
sslOptions.key = fs.readFileSync(connectionInfo.clientPrivateKey).toString();
}

return sslOptions;
const sslType = mapSslType(connectionInfo.sslType);

if (!sslType || sslType === 'disable') {
return false;
}

if (sslType === 'allow') {
return true;
}

let sslOptions = {
checkServerIdentity(hostname, cert) {
logger.info('Certificate', {
hostname,
cert: {
subject: cert.subject,
issuer: cert.issuer,
valid_from: cert.valid_from,
valid_to: cert.valid_to,
},
});
},
};

if (fs.existsSync(connectionInfo.certAuthority)) {
sslOptions.ca = fs.readFileSync(connectionInfo.certAuthority).toString();
}

if (fs.existsSync(connectionInfo.clientCert)) {
sslOptions.cert = fs.readFileSync(connectionInfo.clientCert).toString();
}

if (fs.existsSync(connectionInfo.clientPrivateKey)) {
sslOptions.key = fs.readFileSync(connectionInfo.clientPrivateKey).toString();
}

return sslOptions;
};

const mapSslType = sslType => {
const oldToNewSslType = {
Off: 'disable',
TRUST_ALL_CERTIFICATES: 'allow',
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES: 'prefer',
TRUST_SERVER_CLIENT_CERTIFICATES: 'verify-full',
};

return oldToNewSslType[sslType] || sslType;
const oldToNewSslType = {
Off: 'disable',
TRUST_ALL_CERTIFICATES: 'allow',
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES: 'prefer',
TRUST_SERVER_CLIENT_CERTIFICATES: 'verify-full',
};

return oldToNewSslType[sslType] || sslType;
};

const createClient = async (connectionInfo, logger) => {
let sshTunnel = null;

if (connectionInfo.ssh) {
const { info, tunnel } = await connectViaSsh(connectionInfo);
sshTunnel = tunnel;
connectionInfo = info;
}

const config = {
host: connectionInfo.host,
user: connectionInfo.userName,
password: connectionInfo.userPassword,
port: connectionInfo.port,
keepAlive: true,
ssl: getSslOptions(connectionInfo, logger),
connectionTimeoutMillis: Number(connectionInfo.queryRequestTimeout) || 60000,
query_timeout: Number(connectionInfo.queryRequestTimeout) || 60000,
statement_timeout: Number(connectionInfo.queryRequestTimeout) || 60000,
database: connectionInfo.database || connectionInfo.maintenanceDatabase,
application_name: 'Hackolade',
};

const client = new pg.Client(config);
await client.connect();

return { client, sshTunnel };
let sshTunnel = null;

if (connectionInfo.ssh) {
const { info, tunnel } = await connectViaSsh(connectionInfo);
sshTunnel = tunnel;
connectionInfo = info;
}

const config = {
host: connectionInfo.host,
user: connectionInfo.userName,
password: connectionInfo.userPassword,
port: connectionInfo.port,
keepAlive: true,
ssl: getSslOptions(connectionInfo, logger),
connectionTimeoutMillis: Number(connectionInfo.queryRequestTimeout) || 60000,
query_timeout: Number(connectionInfo.queryRequestTimeout) || 60000,
statement_timeout: Number(connectionInfo.queryRequestTimeout) || 60000,
database: connectionInfo.database || connectionInfo.maintenanceDatabase,
application_name: 'Hackolade',
};

const client = new pg.Client(config);
await client.connect();

return { client, sshTunnel };
};

module.exports = {
createClient,
createClient,
};
78 changes: 39 additions & 39 deletions reverse_engineering/helpers/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,56 @@ let client = null;
let logger = null;

module.exports = {
initializeClient(newClient, newLogger) {
client = newClient;
logger = newLogger;
initializeClient(newClient, newLogger) {
client = newClient;
logger = newLogger;

client.on('error', error => newLogger.error(error));
},
client.on('error', error => newLogger.error(error));
},

isClientInitialized() {
return Boolean(client);
},
isClientInitialized() {
return Boolean(client);
},

releaseClient() {
if (client) {
return new Promise(resolve => {
client.end(() => {
client = null;
resolve();
});
});
}
releaseClient() {
if (client) {
return new Promise(resolve => {
client.end(() => {
client = null;
resolve();
});
});
}

return Promise.resolve();
},
return Promise.resolve();
},

async query(query, params, firstRow = false) {
const queryName = queryConstants.getQueryName(query);
async query(query, params, firstRow = false) {
const queryName = queryConstants.getQueryName(query);

logger.info('Execute query', { queryName, params });
logger.info('Execute query', { queryName, params });

const start = Date.now();
const result = await client.query(query, params);
const duration = Date.now() - start;
const start = Date.now();
const result = await client.query(query, params);
const duration = Date.now() - start;

logger.info('Query executed', { queryName, params, duration, rowsCount: result.rowCount });
logger.info('Query executed', { queryName, params, duration, rowsCount: result.rowCount });

const rows = result.rows || [];
const rows = result.rows || [];

return firstRow ? rows[0] : rows;
},
return firstRow ? rows[0] : rows;
},

async queryTolerant(query, params, firstRow = false) {
try {
return await this.query(query, params, firstRow);
} catch (error) {
error.query = query;
error.params = params;
async queryTolerant(query, params, firstRow = false) {
try {
return await this.query(query, params, firstRow);
} catch (error) {
error.query = query;
error.params = params;

logger.error(error);
logger.error(error);

return null;
}
},
return null;
}
},
};
40 changes: 20 additions & 20 deletions reverse_engineering/helpers/getJsonSchema.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const getJsonSchema = columns => {
const properties = columns.reduce((properties, column) => {
if (column.properties) {
return {
...properties,
[column.name]: {
...column,
...getJsonSchema(column.properties),
},
};
}
const properties = columns.reduce((properties, column) => {
if (column.properties) {
return {
...properties,
[column.name]: {
...column,
...getJsonSchema(column.properties),
},
};
}

return {
...properties,
[column.name]: column,
};
}, {});
return {
...properties,
[column.name]: column,
};
}, {});

const required = Object.entries(properties)
.filter(([filedName, field]) => field.required)
.map(([fieldName]) => fieldName);
const required = Object.entries(properties)
.filter(([filedName, field]) => field.required)
.map(([fieldName]) => fieldName);

return { properties, required };
return { properties, required };
};

module.exports = {
getJsonSchema,
getJsonSchema,
};
Loading

0 comments on commit 2c74008

Please sign in to comment.