Skip to content

Commit

Permalink
Migration fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihajlo-Pavlovic committed Jul 18, 2024
1 parent 0ccdd3d commit ed8b212
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
13 changes: 6 additions & 7 deletions ot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -90,13 +96,6 @@ class OTNode {
this.config,
);


MigrationExecutor.executeServiceAgreementPruningMigration(
this.container,
this.logger,
this.config,
);

MigrationExecutor.executeRemoveDuplicateServiceAgreementMigration(
this.container,
this.logger,
Expand Down
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/migration/migration-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/migration/service-agreement-pruning-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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']],
Expand Down
8 changes: 8 additions & 0 deletions src/modules/repository/repository-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ed8b212

Please sign in to comment.