Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPC-9551: Chunk model version's bulk update #250

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const CONFIG = {
name: process.env.NODE_ENV,
authBaseUrl: process.env.AUTHBASE_URL,
db: {
batchLimit: 32_768, // 2^15,
connection: process.env.POSTGRES_SERVER,
logging: !!parseInt(process.env.POSTGRES_LOGGING ?? ''),
poolMin: 2,
Expand Down
35 changes: 24 additions & 11 deletions src/common-libs/plan/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, any, any>;
Expand Down Expand Up @@ -257,18 +259,29 @@ 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),
/**
* We have to limit the number of parameters that can be passed to a query.
* Limit is 32768 (2^15) which is batchLimit in the config file.
* 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 (
Expand Down
Loading