Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 19 additions & 21 deletions reverse_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const redshiftHelper = require('./helpers/redshiftHelper');
const _ = require('lodash');

// The Redshift Data API throttles bursts of parallel requests (ThrottlingException: Rate exceeded)
const PARALLEL_REQUESTS_LIMIT = 5;

// Delete once v7.8.3 is released
const getHiddenKeys = hiddenKeys => [...hiddenKeys, 'password'];

Expand Down Expand Up @@ -31,16 +34,16 @@ const testConnection = async (connectionInfo, logger, cb) => {
}
};

const getDbCollectionsNames = async (connectionInfo, loggerInstance, cb) => {
const getDbCollectionsNames = async (connectionInfo, loggerInstance, cb, app) => {
const async = app.require('async');
const logger = createLogger('Retrieving databases and tables information', loggerInstance);
await connect(connectionInfo, loggerInstance, cb);
try {
const redshiftSchemaNames = await redshiftHelper.getSchemaNames();
logger.info(`Schema names: ${JSON.stringify(redshiftSchemaNames)}`);
const dbCollectionNamePromises = redshiftSchemaNames.map(schemaName =>
const dbCollectionNames = await mapItems(async, redshiftSchemaNames, PARALLEL_REQUESTS_LIMIT, schemaName =>
redshiftHelper.getSchemaCollectionNames(schemaName, logger),
);
const dbCollectionNames = await Promise.all(dbCollectionNamePromises);
cb(null, dbCollectionNames);
} catch (err) {
logger.error(err);
Expand All @@ -67,37 +70,32 @@ const getDbCollectionsData = async (data, loggerInstance, cb, app) => {
logger.info(`Getting schema information: ${schema}`);
const containerData = await redshiftHelper.getContainerData(schema);

const tablesPackages = await mapItems(async, entities.tables, 100, async table => {
const fullTableName = `"${schema}"."${table}"`;

const tablesPackages = await mapItems(async, entities.tables, PARALLEL_REQUESTS_LIMIT, async table => {
logger.info(`Start getting data from table: "${schema}"."${table}"`);
logger.progress({ message: `Start getting data from table`, containerName: schema, entityName: table });

logger.info(`Getting DDL: "${schema}"."${table}"`);
const ddl = await redshiftHelper.getTableDDL(schema, table);

logger.info(`Getting column types: "${schema}"."${table}"`);
const columns = await redshiftHelper.describeTable({ schemaName: schema, tableName: table, logger });

logger.info(`Getting documents: "${schema}"."${table}"`);
const quantity = await redshiftHelper.getRowsCount(fullTableName);
logger.info(`Found ${quantity} documents: "${schema}"."${table}"`);
const documents = await redshiftHelper.getDocuments(
schema,
table,
quantity,
data.recordSamplingSettings,
);
const documents = await redshiftHelper.getDocuments({
schemaName: schema,
tableName: table,
columns,
recordSamplingSettings: data.recordSamplingSettings,
logger,
});

logger.info(`Fetching record for JSON schema inference: "${schema}"."${table}"`);
logger.progress({
message: `Fetching record for JSON schema inference`,
containerName: schema,
entityName: table,
});
const jsonSchema = await redshiftHelper.getJsonSchema({
documents,
schemaName: schema,
tableName: table,
logger,
});
const jsonSchema = redshiftHelper.getJsonSchema({ documents, columns });

logger.info(`Schema inference: "${schema}"."${table}"`);
logger.progress({ message: `Schema inference`, containerName: schema, entityName: table });
Expand Down Expand Up @@ -127,7 +125,7 @@ const getDbCollectionsData = async (data, loggerInstance, cb, app) => {
};
});

const views = await mapItems(async, entities.views, 100, async view => {
const views = await mapItems(async, entities.views, PARALLEL_REQUESTS_LIMIT, async view => {
logger.info(`Start getting data from view: "${schema}"."${view}"`);
logger.progress({ message: `Start getting data from view`, containerName: schema, entityName: view });

Expand Down
73 changes: 46 additions & 27 deletions reverse_engineering/helpers/redshiftHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,26 @@
};
};

const getRowsCount = async tableName => {
const getRowsCount = async ({ schemaName, tableName, logger }) => {
try {
const record = await execute(`SELECT count(*) AS COUNT FROM ${tableName};`);
const record = await execute(`SELECT count(*) AS COUNT FROM "${schemaName}"."${tableName}";`);
return _.get(record, '[0][0].longValue');
} catch {
return '';
} catch (err) {
logTableWarning({
logger,
schemaName,
tableName,
message: `Could not count the rows, so no sample data is read: ${err.message}`,
});
return 0;
}
};

const logTableWarning = ({ logger, schemaName, tableName, message }) => {
logger.error(`"${schemaName}"."${tableName}": ${message}`);
logger.progress({ message, containerName: schemaName, entityName: tableName });
};

const getSampleDocSize = (count, recordSamplingSettings) => {
if (recordSamplingSettings.active === 'absolute') {
return Number(recordSamplingSettings.absolute.value);
Expand Down Expand Up @@ -546,15 +557,28 @@
return type;
};

const describeTable = async (schemaName, tableName) => {
const describeTable = async ({ schemaName, tableName, logger }) => {
try {
const records = await redshift.redshiftDataInstance.describeTable({
...redshift.connectionParams,
Table: tableName,
Schema: schemaName,
});
return _.get(records, 'ColumnList').map(column => ({ name: column.name, type: column.typeName }));
let columns = [];
let NextToken;
do {
const result = await redshift.redshiftDataInstance.describeTable({
...redshift.connectionParams,
Table: tableName,
Schema: schemaName,
NextToken,
});

Check warning on line 570 in reverse_engineering/helpers/redshiftHelper.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected `await` inside a loop.

See more on https://sonarcloud.io/project/issues?id=hackolade_Redshift&issues=AaI3XgAcHzQSomk-HnbD&open=AaI3XgAcHzQSomk-HnbD&pullRequest=94
columns = columns.concat(result.ColumnList.map(column => ({ name: column.name, type: column.typeName })));
NextToken = result.NextToken;
} while (NextToken);
return columns;
} catch (err) {
logTableWarning({
logger,
schemaName,
tableName,
message: `Could not read the column types, so SUPER columns get no structure and no sample data: ${err.message}`,
});
return [];
}
};
Expand All @@ -571,12 +595,13 @@
}
};

const getDocuments = async (schemaName, tableName, quantity, recordSamplingSettings) => {
const limit = getSampleDocSize(quantity, recordSamplingSettings);
const columns = await describeTable(schemaName, tableName);
const getDocuments = async ({ schemaName, tableName, columns, recordSamplingSettings, logger }) => {
if (!tableHasColumnsOfSuperType(columns)) {
return [];
}
const quantity = await getRowsCount({ schemaName, tableName, logger });
logger.info(`Found ${quantity} documents: "${schemaName}"."${tableName}"`);
const limit = getSampleDocSize(quantity, recordSamplingSettings);
const records = await execute(`SELECT * FROM "${schemaName}"."${tableName}" LIMIT ${limit};`);
const documents = records
.map(document =>
Expand Down Expand Up @@ -612,18 +637,12 @@
}, {});
};

const getJsonSchema = async ({ documents, schemaName, tableName, logger }) => {
try {
let rows = await describeTable(schemaName, tableName);
rows = rows.filter(row => row.type === 'super');
return {
properties: getJsonSchemaFromRows(documents, rows),
};
} catch (err) {
logger.error(err);
return { properties: {} };
}
};
const getJsonSchema = ({ documents, columns }) => ({
properties: getJsonSchemaFromRows(
documents,
columns.filter(column => column.type === 'super'),
),
});

const getJsonSchemaFromRows = (documents, rows) => {
const properties = rows.reduce((properties, row) => {
Expand Down Expand Up @@ -682,7 +701,7 @@
getTableDDL,
getSchemaQuota,
getViewDDL,
getRowsCount,
describeTable,
getDocuments,
getJsonSchema,
getModelData,
Expand Down