Skip to content

The open alternative to Salesforce, designed for AI.

fix(server): batch upgrade migration inserts to stay under PG param limit (#20588)

Charles Bochet78b30928869d
diff --git a/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-migration.service.ts b/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-migration.service.ts
index 2fd618d2b94..2077516ad9b 100644
--- a/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-migration.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/upgrade/services/upgrade-migration.service.ts
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
+import chunk from 'lodash.chunk';
import { isDefined } from 'twenty-shared/utils';
import { In, IsNull, type QueryRunner, Repository } from 'typeorm';
@@ -21,6 +22,8 @@ export type WorkspaceLastAttemptedCommand = {
isInitial: boolean;
};
+const UPGRADE_MIGRATION_SAVE_BATCH_SIZE = 1000;
+
@Injectable()
export class UpgradeMigrationService {
constructor(
@@ -95,7 +98,7 @@ export class UpgradeMigrationService {
where: { name, workspaceId: IsNull() },
});
- await repository.save([
+ const instanceRows = [
{
name,
status,
@@ -112,7 +115,14 @@ export class UpgradeMigrationService {
workspaceId,
errorMessage,
})),
- ]);
+ ];
+
+ for (const batch of chunk(
+ instanceRows,
+ UPGRADE_MIGRATION_SAVE_BATCH_SIZE,
+ )) {
+ await repository.save(batch);
+ }
return;
}
@@ -134,7 +144,9 @@ export class UpgradeMigrationService {
});
}
- await repository.save(rows);
+ for (const batch of chunk(rows, UPGRADE_MIGRATION_SAVE_BATCH_SIZE)) {
+ await repository.save(batch);
+ }
}
async markAsWorkspaceInitial({