diff --git a/reverse_engineering/reverseEngineeringService/helpers/getUniqueIndexesColumns.js b/reverse_engineering/reverseEngineeringService/helpers/getUniqueIndexesColumns.js new file mode 100644 index 0000000..7350a24 --- /dev/null +++ b/reverse_engineering/reverseEngineeringService/helpers/getUniqueIndexesColumns.js @@ -0,0 +1,23 @@ +const getColumnUniqueKey = ({ IndexName, TableName, schemaName, columnName }) => + `${schemaName}${IndexName}${TableName}${columnName}`; + +const getUniqueIndexesColumns = ({ indexesColumns }) => { + const uniqueKeysToColumns = {}; + + for (const indexesColumn of indexesColumns) { + const columnKey = getColumnUniqueKey(indexesColumn); + const isColumnUnique = !uniqueKeysToColumns[columnKey]; + + if (!isColumnUnique) { + continue; + } + + uniqueKeysToColumns[columnKey] = indexesColumn; + } + + return Object.values(uniqueKeysToColumns); +}; + +module.exports = { + getUniqueIndexesColumns, +}; diff --git a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js index e025d58..494880b 100644 --- a/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js +++ b/reverse_engineering/reverseEngineeringService/reverseEngineeringService.js @@ -42,6 +42,7 @@ const { getPeriodForSystemTime, } = require('./helpers'); const pipe = require('../helpers/pipe'); +const { getUniqueIndexesColumns } = require('./helpers/getUniqueIndexesColumns'); const mergeCollectionsWithViews = ({ jsonSchemas }) => { const [viewSchemas, collectionSchemas] = partition(jsonSchemas, jsonSchema => jsonSchema.relatedTables); @@ -309,7 +310,8 @@ const reverseCollectionsToJSON = logger => async (dbConnectionClient, tablesInfo rawDatabaseIndexes.map(i => i.index_id), logger, ); - const databaseIndexes = addTotalBucketCountToDatabaseIndexes(rawDatabaseIndexes, indexesBucketCount); + const uniqueDatabaseIndexesColumns = getUniqueIndexesColumns({ indexesColumns: rawDatabaseIndexes }); + const databaseIndexes = addTotalBucketCountToDatabaseIndexes(uniqueDatabaseIndexesColumns, indexesBucketCount); return await Object.entries(tablesInfo).reduce(async (jsonSchemas, [schemaName, tableNames]) => { logger.log('info', { message: `Fetching '${dbName}' database information` }, 'Reverse Engineering');