From cea313d813f3c1f5ebc3ead1affa298dee6dfd1a Mon Sep 17 00:00:00 2001 From: enxtur Date: Sun, 5 May 2024 18:38:23 +0800 Subject: [PATCH] HPC-9551: Chunk versionModel update by batch limit --- src/common-libs/plan/versioning.ts | 34 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/common-libs/plan/versioning.ts b/src/common-libs/plan/versioning.ts index 5cec9d1b..6b7572a3 100644 --- a/src/common-libs/plan/versioning.ts +++ b/src/common-libs/plan/versioning.ts @@ -40,6 +40,8 @@ import { createBrandedValue, type Brand, } from '@unocha/hpc-api-core/src/util/types'; +import { chunk } from 'lodash'; +import { CONFIG } from '../../../config'; // eslint-disable-next-line @typescript-eslint/no-explicit-any type AnyModelId = Brand; @@ -257,18 +259,28 @@ const updateBaseAndVersionModelTags = async ( }, skipValidation: skipAttachmentsAndMeasurementsValidation(tableName), }); - await versionModel.update({ - values: { - latestTaggedVersion: false, - ...(tag.public ? { currentVersion: false } : {}), - }, - where: { - id: { - [models.Op.IN]: oldVersions.map((v) => v.id as AnyModelId), + /** + * 2^16=65536 is the maximum number of parameters that can be passed to a query + * latestTaggedVersion, currentVersion, and updatedAt is also included in the parameters + * so we subtract 3 from the limit to get the maximum number of parameters that can be passed to a query + */ + for (const chunkedOldVersionModelIDs of chunk( + oldVersions.map((v) => v.id as AnyModelId), + CONFIG.db.batchLimit - 3 + )) { + await versionModel.update({ + values: { + latestTaggedVersion: false, + ...(tag.public ? { currentVersion: false } : {}), }, - }, - skipValidation: skipAttachmentsAndMeasurementsValidation(tableName), - }); + where: { + id: { + [models.Op.IN]: chunkedOldVersionModelIDs, + }, + }, + skipValidation: skipAttachmentsAndMeasurementsValidation(tableName), + }); + } }; const updateBaseModelTags = async (