From ed8b212a1159718d2fbebf126990580aa09ec535 Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Thu, 18 Jul 2024 12:47:54 +0200 Subject: [PATCH] Migration fixes --- ot-node.js | 13 ++++++------ package-lock.json | 4 ++-- package.json | 2 +- src/migration/migration-executor.js | 2 +- .../service-agreement-pruning-migration.js | 21 ++++++++++++++----- .../service-agreement-repository.js | 19 +++++++++++++---- .../repository/repository-module-manager.js | 8 +++++++ 7 files changed, 49 insertions(+), 20 deletions(-) diff --git a/ot-node.js b/ot-node.js index 4900e328d2..7544257526 100644 --- a/ot-node.js +++ b/ot-node.js @@ -78,6 +78,12 @@ class OTNode { this.config, ); + await MigrationExecutor.executeServiceAgreementPruningMigration( + this.container, + this.logger, + this.config, + ); + await this.initializeRouters(); await this.startNetworkModule(); this.startTelemetryModule(); @@ -90,13 +96,6 @@ class OTNode { this.config, ); - - MigrationExecutor.executeServiceAgreementPruningMigration( - this.container, - this.logger, - this.config, - ); - MigrationExecutor.executeRemoveDuplicateServiceAgreementMigration( this.container, this.logger, diff --git a/package-lock.json b/package-lock.json index bc8afa7698..4004d3ae08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "origintrail_node", - "version": "6.5.1", + "version": "6.5.1+hotfix.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "origintrail_node", - "version": "6.5.1", + "version": "6.5.1+hotfix.1", "license": "ISC", "dependencies": { "@comunica/query-sparql": "^2.4.3", diff --git a/package.json b/package.json index e7f54dafa9..e72debf9c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "origintrail_node", - "version": "6.5.1", + "version": "6.5.1+hotfix.1", "description": "OTNode V6", "main": "index.js", "type": "module", diff --git a/src/migration/migration-executor.js b/src/migration/migration-executor.js index a3e2635ecc..77469b986e 100644 --- a/src/migration/migration-executor.js +++ b/src/migration/migration-executor.js @@ -496,7 +496,7 @@ class MigrationExecutor { await migration.migrate(); } catch (error) { logger.error( - `Unable to execute service agreement pruning migration. Error: ${error.message}`, + `Unable to execute remove duplicate service agreement migration. Error: ${error.message}`, ); } } diff --git a/src/migration/service-agreement-pruning-migration.js b/src/migration/service-agreement-pruning-migration.js index 2e7853c751..c29ba4de17 100644 --- a/src/migration/service-agreement-pruning-migration.js +++ b/src/migration/service-agreement-pruning-migration.js @@ -24,11 +24,22 @@ class ServiceAgreementPruningMigration extends BaseMigration { // eslint-disable-next-line no-await-in-loop await this.blockchainModuleManager.getAssetStorageContractAddresses(blockchainId); - // eslint-disable-next-line no-await-in-loop - await this.repositoryModuleManager.removeServiceAgreementsByBlockchainAndContract( - blockchainId, - assetStorageContractAddresses[0], - ); + const countOfServiceAgreementsToBeRemoved = + // eslint-disable-next-line no-await-in-loop + await this.repositoryModuleManager.getCountOfServiceAgreementsByBlockchainAndContract( + blockchainId, + assetStorageContractAddresses[0], + ); + + // removeServiceAgreementsByBlockchainAndContract deletes in batches od 100_000 + const numberOfIteration = Math.ceil(countOfServiceAgreementsToBeRemoved / 100_000); + for (let i = 0; i < numberOfIteration; i += 1) { + // eslint-disable-next-line no-await-in-loop + await this.repositoryModuleManager.removeServiceAgreementsByBlockchainAndContract( + blockchainId, + assetStorageContractAddresses[0], + ); + } } } } diff --git a/src/modules/repository/implementation/sequelize/repositories/service-agreement-repository.js b/src/modules/repository/implementation/sequelize/repositories/service-agreement-repository.js index 6857a46a50..1c3a6725c7 100644 --- a/src/modules/repository/implementation/sequelize/repositories/service-agreement-repository.js +++ b/src/modules/repository/implementation/sequelize/repositories/service-agreement-repository.js @@ -287,9 +287,8 @@ class ServiceAgreementRepository { }); } - - async removeServiceAgreementsByBlockchainAndContract(blockchainId, contract) { - await this.model.destroy({ + async getCountOfServiceAgreementsByBlockchainAndContract(blockchainId, contract) { + await this.model.count({ where: { blockchainId, assetStorageContractAddress: { @@ -298,7 +297,19 @@ class ServiceAgreementRepository { }, }); } - + + async removeServiceAgreementsByBlockchainAndContract(blockchainId, contract) { + const query = ` + DELETE FROM service_agreement + WHERE blockchain_id = '${blockchainId}' + AND asset_storage_contract_address = '${contract}' + LIMIT 100000; + `; + await this.model.query(query, { + type: Sequelize.QueryTypes.DELETE, + }); + } + async findDuplicateServiceAgreements(blockchainId) { return this.model.findAll({ attributes: ['token_id', [Sequelize.fn('COUNT', Sequelize.col('*')), 'count']], diff --git a/src/modules/repository/repository-module-manager.js b/src/modules/repository/repository-module-manager.js index 33f6ffb442..eea9d49833 100644 --- a/src/modules/repository/repository-module-manager.js +++ b/src/modules/repository/repository-module-manager.js @@ -403,6 +403,14 @@ class RepositoryModuleManager extends BaseModuleManager { } } + async getCountOfServiceAgreementsByBlockchainAndContract(blockchainId, contract) { + if (this.initialized) { + return this.getRepository( + 'service_agreement', + ).getCountOfServiceAgreementsByBlockchainAndContract(blockchainId, contract); + } + } + async removeServiceAgreementsByBlockchainAndContract(blockchainId, contract) { if (this.initialized) { return this.getRepository(