Features: 1) Add chunk size argument for deleting never ordered products;
Fixes: 1) Improve error handling with detailed exception logging during deletion; Extra: 1) Replace hardcoded chunk size with a dynamic option; 2) Enhance log output for better debugging.
This commit is contained in:
parent
db0d64bfd2
commit
d3dd23d99f
1 changed files with 13 additions and 4 deletions
|
|
@ -2,16 +2,24 @@ from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from core.models import AttributeValue, Product, ProductImage
|
from core.models import AttributeValue, Product, ProductImage
|
||||||
|
|
||||||
CHUNK_SIZE = 5000
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = "Delete Product rows with no OrderProduct, in batches"
|
help = "Delete Product rows with no OrderProduct, in batches"
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--size",
|
||||||
|
required=False,
|
||||||
|
default=5000,
|
||||||
|
help="Chunk size to delete",
|
||||||
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
|
size = options["size"]
|
||||||
while True:
|
while True:
|
||||||
batch_ids = list(
|
batch_ids = list(
|
||||||
Product.objects.filter(orderproduct__isnull=True).values_list("pk", flat=True)[:CHUNK_SIZE]
|
Product.objects.filter(orderproduct__isnull=True).values_list("pk", flat=True)[:size]
|
||||||
)
|
)
|
||||||
if not batch_ids:
|
if not batch_ids:
|
||||||
break
|
break
|
||||||
|
|
@ -19,7 +27,8 @@ class Command(BaseCommand):
|
||||||
AttributeValue.objects.filter(product_id__in=batch_ids).delete()
|
AttributeValue.objects.filter(product_id__in=batch_ids).delete()
|
||||||
ProductImage.objects.filter(product_id__in=batch_ids).delete()
|
ProductImage.objects.filter(product_id__in=batch_ids).delete()
|
||||||
Product.objects.filter(pk__in=batch_ids).delete()
|
Product.objects.filter(pk__in=batch_ids).delete()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
|
self.stdout.write("Couldn't delete some of the products(will retry later): %s" % str(e))
|
||||||
continue
|
continue
|
||||||
self.stdout.write(f"Deleted {len(batch_ids)} products…")
|
self.stdout.write(f"Deleted {len(batch_ids)} products…")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue