From 2ae68a171b638499bc71734bc2ba44314d37778e Mon Sep 17 00:00:00 2001 From: Carlos Monastyrski Date: Thu, 2 Oct 2025 19:18:08 -0300 Subject: [PATCH 1/2] Fix PIT migration batch issue with folders overflowing the batch size --- ...452_pit-projects-commits-initialization.ts | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts b/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts index dd4034c57..592f93349 100644 --- a/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts +++ b/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts @@ -265,12 +265,14 @@ export async function up(knex: Knex): Promise { }); // Create folder commit changes + const currentBatchFolderIds = new Set(newCommitsInserted.map((commit) => commit.folderId)); // eslint-disable-next-line no-await-in-loop await knex.batchInsert( TableName.FolderCommitChanges, foldersCommitsList .map((folderCommit) => folderCommit.changes) .flat() + .filter((change) => currentBatchFolderIds.has(change.folderId)) .map((change) => ({ folderCommitId: newCommitsMap[change.folderId], changeType: change.changeType, @@ -289,6 +291,7 @@ export async function up(knex: Knex): Promise { foldersCommitsList .map((folderCommit) => folderCommit.changes) .flat() + .filter((change) => currentBatchFolderIds.has(change.folderId)) .map((change) => ({ folderCheckpointId: newCheckpointsMap[change.folderId], folderVersionId: change.folderVersionId, @@ -303,9 +306,11 @@ export async function up(knex: Knex): Promise { const newTreeCheckpoints = (await knex .batchInsert( TableName.FolderTreeCheckpoint, - Object.keys(rootFoldersMap).map((folderId) => ({ - folderCommitId: newCommitsMap[folderId] - })) + Object.keys(rootFoldersMap) + .filter((folderId) => currentBatchFolderIds.has(folderId)) + .map((folderId) => ({ + folderCommitId: newCommitsMap[folderId] + })) ) .returning("*")) as TFolderTreeCheckpoints[]; @@ -321,11 +326,13 @@ export async function up(knex: Knex): Promise { await knex .batchInsert( TableName.FolderTreeCheckpointResources, - newCommitsInserted.map((folderCommit) => ({ - folderTreeCheckpointId: newTreeCheckpointsMap[folderCommit.envId], - folderId: folderCommit.folderId, - folderCommitId: folderCommit.id - })) + newCommitsInserted + .filter((folderCommit) => newTreeCheckpointsMap[folderCommit.envId]) + .map((folderCommit) => ({ + folderTreeCheckpointId: newTreeCheckpointsMap[folderCommit.envId], + folderId: folderCommit.folderId, + folderCommitId: folderCommit.id + })) ) .returning("*"); From 15fe26c7e7fb8e680f7b39d7b0f2d42a922f2359 Mon Sep 17 00:00:00 2001 From: Carlos Monastyrski Date: Thu, 2 Oct 2025 19:53:09 -0300 Subject: [PATCH 2/2] Improve FolderTreeCheckpointResources migration handling --- ...452_pit-projects-commits-initialization.ts | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts b/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts index 592f93349..f382db0d6 100644 --- a/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts +++ b/backend/src/db/migrations/20250602155452_pit-projects-commits-initialization.ts @@ -226,7 +226,7 @@ export async function up(knex: Knex): Promise { // Insert New Commits in batches of 9000 const newCommits = foldersCommitsList.map((folderCommit) => folderCommit.commit); const commitBatches = chunkArray(newCommits, 9000); - + let pendingDeepTreeCommitResources: TFolderCommits[] = []; let j = 0; for (const commitBatch of commitBatches) { j += 1; @@ -322,20 +322,31 @@ export async function up(knex: Knex): Promise { }); // Create Folder Tree Checkpoint Resources - // eslint-disable-next-line no-await-in-loop - await knex - .batchInsert( - TableName.FolderTreeCheckpointResources, - newCommitsInserted - .filter((folderCommit) => newTreeCheckpointsMap[folderCommit.envId]) - .map((folderCommit) => ({ + const commitsToProcess = pendingDeepTreeCommitResources.concat(newCommitsInserted); + const unprocessableCommits: TFolderCommits[] = []; + const processableCommits = commitsToProcess.filter((folderCommit) => { + const isProcessable = newTreeCheckpointsMap[folderCommit.envId]; + if (!isProcessable) { + unprocessableCommits.push(folderCommit); + } + return isProcessable; + }); + + if (processableCommits.length > 0) { + // eslint-disable-next-line no-await-in-loop + await knex + .batchInsert( + TableName.FolderTreeCheckpointResources, + processableCommits.map((folderCommit) => ({ folderTreeCheckpointId: newTreeCheckpointsMap[folderCommit.envId], folderId: folderCommit.folderId, folderCommitId: folderCommit.id })) - ) - .returning("*"); + ) + .returning("*"); + } + pendingDeepTreeCommitResources = unprocessableCommits; logger.info(`Finished inserting folder tree checkpoint resources - batch ${j} of ${commitBatches.length}`); } }