From 3fa4a6bdaf05e0c1c2fd1fdf38514cc46601bca4 Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Thu, 19 Dec 2024 14:09:08 +0100 Subject: [PATCH] fixes --- ot-node.js | 5 +- .../20241105160000-add-indexes-to-tables.js | 264 ++++++++---------- 2 files changed, 125 insertions(+), 144 deletions(-) diff --git a/ot-node.js b/ot-node.js index ecc313641..7b516ae7a 100644 --- a/ot-node.js +++ b/ot-node.js @@ -331,7 +331,8 @@ class OTNode { continue; } - const { blockchain, contract, tokenId } = ualService.resolveUAL(paranetUAL); + const { blockchain, contract, knowledgeCollectionId } = + ualService.resolveUAL(paranetUAL); if (!blockchainModuleManager.getImplementationNames().includes(blockchain)) { this.logger.warn( `Unable to initialize Paranet with id ${paranetUAL} because of unsupported blockchain implementation`, @@ -339,7 +340,7 @@ class OTNode { continue; } - const paranetId = paranetService.constructParanetId(contract, tokenId); + const paranetId = paranetService.constructParanetId(contract, knowledgeCollectionId); // eslint-disable-next-line no-await-in-loop const paranetExists = await blockchainModuleManager.paranetExists( blockchain, diff --git a/src/modules/repository/implementation/sequelize/migrations/20241105160000-add-indexes-to-tables.js b/src/modules/repository/implementation/sequelize/migrations/20241105160000-add-indexes-to-tables.js index 6a9bd3cc9..e78f89736 100644 --- a/src/modules/repository/implementation/sequelize/migrations/20241105160000-add-indexes-to-tables.js +++ b/src/modules/repository/implementation/sequelize/migrations/20241105160000-add-indexes-to-tables.js @@ -1,127 +1,101 @@ export async function up({ context: { queryInterface } }) { - await queryInterface.addIndex('shard', ['blockchain_id'], { - name: 'shard_blockchain_id_index', - }); - - await queryInterface.addIndex('shard', ['last_dialed'], { - name: 'last_dialed_index', - }); - - await queryInterface.addIndex('service_agreement', ['blockchain_id'], { - name: 'service_agreement_blockchain_id_index', - }); - - await queryInterface.addIndex('service_agreement', ['blockchain_id', 'token_id'], { - name: 'blockchain_id_token_id_index', - }); - - await queryInterface.addIndex('service_agreement', ['token_id'], { - name: 'token_id_index', - }); - - await queryInterface.addIndex('paranet_synced_asset', ['ual'], { - name: 'ual_index', - }); - - await queryInterface.addIndex('paranet_synced_asset', ['paranet_ual', 'data_source'], { - name: 'paranet_ual_data_source_index', - }); - - await queryInterface.addIndex('paranet', ['blockchain_id', 'paranet_id'], { - name: 'blockchain_id_paranet_id_index', - }); - - await queryInterface.addIndex('missed_paranet_asset', ['paranet_ual'], { - name: 'paranet_ual_index', - }); - - await queryInterface.addIndex('missed_paranet_asset', ['ual'], { - name: 'ual_index', - }); - - await queryInterface.addIndex('event', ['name', 'timestamp'], { - name: 'name_timestamp_index', - }); - - await queryInterface.addIndex('event', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('commands', ['name', 'status'], { - name: 'name_status_index', - }); - - await queryInterface.addIndex('commands', ['status', 'started_at'], { - name: 'status_started_at_index', - }); - - await queryInterface.addIndex('get', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('publish', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('update', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('publish_paranet', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('get', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('publish', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('update', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('publish_paranet', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('get_response', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('publish_response', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('update_response', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('publish_paranet_response', ['operation_id'], { - name: 'operation_id_index', - }); - - await queryInterface.addIndex('get_response', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('publish_response', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('update_response', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('publish_paranet_response', ['created_at'], { - name: 'created_at_index', - }); - - await queryInterface.addIndex('blockchain', ['contract'], { - name: 'contract_index', - }); + const indexes = [ + { table: 'shard', column: ['blockchain_id'], name: 'shard_blockchain_id_index' }, + { table: 'shard', column: ['last_dialed'], name: 'last_dialed_index' }, + { + table: 'service_agreement', + column: ['blockchain_id'], + name: 'service_agreement_blockchain_id_index', + }, + { + table: 'service_agreement', + column: ['blockchain_id', 'token_id'], + name: 'blockchain_id_token_id_index', + }, + { table: 'service_agreement', column: ['token_id'], name: 'token_id_index' }, + { table: 'paranet_synced_asset', column: ['ual'], name: 'paranet_synced_asset_ual_index' }, + { + table: 'paranet_synced_asset', + column: ['paranet_ual', 'data_source'], + name: 'paranet_ual_data_source_index', + }, + { + table: 'paranet', + column: ['blockchain_id', 'paranet_id'], + name: 'blockchain_id_paranet_id_index', + }, + { table: 'missed_paranet_asset', column: ['paranet_ual'], name: 'paranet_ual_index' }, + { table: 'missed_paranet_asset', column: ['ual'], name: 'missed_paranet_asset_ual_index' }, + { table: 'event', column: ['name', 'timestamp'], name: 'name_timestamp_index' }, + { table: 'event', column: ['operation_id'], name: 'event_operation_id_index' }, + { table: 'commands', column: ['name', 'status'], name: 'name_status_index' }, + { table: 'commands', column: ['status', 'started_at'], name: 'status_started_at_index' }, + { table: 'get', column: ['operation_id'], name: 'get_operation_id_index' }, + { table: 'publish', column: ['operation_id'], name: 'publish_operation_id_index' }, + { table: 'update', column: ['operation_id'], name: 'update_operation_id_index' }, + { + table: 'publish_paranet', + column: ['operation_id'], + name: 'publish_paranet_operation_id_index', + }, + { table: 'get', column: ['created_at'], name: 'get_created_at_index' }, + { table: 'publish', column: ['created_at'], name: 'publish_created_at_index' }, + { table: 'update', column: ['created_at'], name: 'update_created_at_index' }, + { + table: 'publish_paranet', + column: ['created_at'], + name: 'publish_paranet_created_at_index', + }, + { + table: 'get_response', + column: ['operation_id'], + name: 'get_response_operation_id_index', + }, + { table: 'publish_response', column: ['operation_id'], name: 'operation_id_index' }, + { + table: 'update_response', + column: ['operation_id'], + name: 'update_response_operation_id_index', + }, + { + table: 'publish_paranet_response', + column: ['operation_id'], + name: 'publish_paranet_response_operation_id_index', + }, + { table: 'get_response', column: ['created_at'], name: 'get_response_created_at_index' }, + { + table: 'publish_response', + column: ['created_at'], + name: 'publish_response_created_at_index', + }, + { + table: 'update_response', + column: ['created_at'], + name: 'update_response_created_at_index', + }, + { + table: 'publish_paranet_response', + column: ['created_at'], + name: 'publish_paranet_response_created_at_index', + }, + { table: 'blockchain', column: ['contract'], name: 'contract_index' }, + ]; + + for (const index of indexes) { + const { table, column, name } = index; + + // eslint-disable-next-line no-await-in-loop + const [results] = await queryInterface.sequelize.query(` + SELECT COUNT(1) AS count + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '${table}' AND INDEX_NAME = '${name}'; + `); + + if (results[0].count === 0) { + // eslint-disable-next-line no-await-in-loop + await queryInterface.addIndex(table, column, { name }); + } + } } export async function down({ context: { queryInterface } }) { @@ -135,7 +109,7 @@ export async function down({ context: { queryInterface } }) { await queryInterface.removeIndex('service_agreement', 'token_id_index'); - await queryInterface.removeIndex('paranet_synced_asset', 'ual_index'); + await queryInterface.removeIndex('paranet_synced_asset', 'paranet_synced_asset_ual_index'); await queryInterface.removeIndex('paranet_synced_asset', 'paranet_ual_data_source_index'); @@ -143,47 +117,53 @@ export async function down({ context: { queryInterface } }) { await queryInterface.removeIndex('missed_paranet_asset', 'paranet_ual_index'); - await queryInterface.removeIndex('missed_paranet_asset', 'ual_index'); + await queryInterface.removeIndex('missed_paranet_asset', 'missed_paranet_asset_ual_index'); await queryInterface.removeIndex('event', 'name_timestamp_index'); - await queryInterface.removeIndex('event', 'operation_id_index'); + await queryInterface.removeIndex('event', 'event_operation_id_index'); await queryInterface.removeIndex('commands', 'name_status_index'); await queryInterface.removeIndex('commands', 'status_started_at_index'); - await queryInterface.removeIndex('get', 'operation_id_index'); + await queryInterface.removeIndex('get', 'get_operation_id_index'); - await queryInterface.removeIndex('publish', 'operation_id_index'); + await queryInterface.removeIndex('publish', 'publish_operation_id_index'); - await queryInterface.removeIndex('update', 'operation_id_index'); + await queryInterface.removeIndex('update', 'update_operation_id_index'); - await queryInterface.removeIndex('publish_paranet', 'operation_id_index'); + await queryInterface.removeIndex('publish_paranet', 'publish_paranet_operation_id_index'); - await queryInterface.removeIndex('get', 'created_at_index'); + await queryInterface.removeIndex('get', 'get_created_at_index'); - await queryInterface.removeIndex('publish', 'created_at_index'); + await queryInterface.removeIndex('publish', 'publish_created_at_index'); - await queryInterface.removeIndex('update', 'created_at_index'); + await queryInterface.removeIndex('update', 'update_created_at_index'); - await queryInterface.removeIndex('publish_paranet', 'created_at_index'); + await queryInterface.removeIndex('publish_paranet', 'publish_paranet_created_at_index'); - await queryInterface.removeIndex('get_response', 'operation_id_index'); + await queryInterface.removeIndex('get_response', 'get_response_operation_id_index'); - await queryInterface.removeIndex('publish_response', 'operation_id_index'); + await queryInterface.removeIndex('publish_response', 'publish_response_operation_id_index'); - await queryInterface.removeIndex('update_response', 'operation_id_index'); + await queryInterface.removeIndex('update_response', 'update_response_operation_id_index'); - await queryInterface.removeIndex('publish_paranet_response', 'operation_id_index'); + await queryInterface.removeIndex( + 'publish_paranet_response', + 'publish_paranet_response_operation_id_index', + ); - await queryInterface.removeIndex('get_response', 'created_at_index'); + await queryInterface.removeIndex('get_response', 'get_response_created_at_index'); - await queryInterface.removeIndex('publish_response', 'created_at_index'); + await queryInterface.removeIndex('publish_response', 'publish_response_created_at_index'); - await queryInterface.removeIndex('update_response', 'created_at_index'); + await queryInterface.removeIndex('update_response', 'update_response_created_at_index'); - await queryInterface.removeIndex('publish_paranet_response', 'created_at_index'); + await queryInterface.removeIndex( + 'publish_paranet_response', + 'publish_paranet_response_created_at_index', + ); await queryInterface.removeIndex('blockchain', 'contract_index'); }