Browse Source

fix(core): Fix collection delete for big collection (#1966)

Fixes #1961
Thierry 3 years ago
parent
commit
6541975b84
1 changed files with 9 additions and 0 deletions
  1. 9 0
      packages/core/src/service/services/collection.service.ts

+ 9 - 0
packages/core/src/service/services/collection.service.ts

@@ -499,6 +499,15 @@ export class CollectionService implements OnModuleInit {
         for (const coll of [...descendants.reverse(), collection]) {
             const affectedVariantIds = await this.getCollectionProductVariantIds(coll);
             const deletedColl = new Collection(coll);
+            // To avoid perfomance issues on huge collection, we first delete the links between the product variants and the collection by chunks
+            const chunkedDeleteIds = this.chunkArray(affectedVariantIds, 500);
+            for (const chunkedDeleteId of chunkedDeleteIds) {
+                await this.connection.rawConnection
+                    .createQueryBuilder()
+                    .relation(Collection, 'productVariants')
+                    .of(collection)
+                    .remove(chunkedDeleteId);
+            }
             await this.connection.getRepository(ctx, Collection).remove(coll);
             this.eventBus.publish(new CollectionModificationEvent(ctx, deletedColl, affectedVariantIds));
         }