mirror of
https://github.com/inventree/InvenTree.git
synced 2026-02-19 13:18:03 +00:00
Batch update of records during migration (#11371)
* Batch update of records during migration Reduce issues with very large datasets * Add short-circuit logic * Filter and prefetch
This commit is contained in:
@@ -10,24 +10,40 @@ def add_part_links(apps, schema_editor):
|
|||||||
|
|
||||||
history_entries = []
|
history_entries = []
|
||||||
|
|
||||||
for tracking in StockItemTracking.objects.all():
|
N = StockItemTracking.objects.count()
|
||||||
|
|
||||||
|
if N > 0:
|
||||||
|
print(f"\nUpdating {N} StockItemTracking entries with part links...")
|
||||||
|
|
||||||
|
for tracking in StockItemTracking.objects.filter(part__isnull=True).select_related('item__part'):
|
||||||
|
|
||||||
item = tracking.item
|
item = tracking.item
|
||||||
|
|
||||||
|
# No item link - skip
|
||||||
if item is None:
|
if item is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
part = item.part
|
part = item.part
|
||||||
|
|
||||||
|
# No part link - skip
|
||||||
if part is None:
|
if part is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Already linked to the correct part - skip
|
||||||
|
if tracking.part == part:
|
||||||
|
continue
|
||||||
|
|
||||||
tracking.part = part
|
tracking.part = part
|
||||||
history_entries.append(tracking)
|
history_entries.append(tracking)
|
||||||
|
|
||||||
|
# Process in batches to avoid issues with very large datasets
|
||||||
|
if len(history_entries) >= 100:
|
||||||
|
StockItemTracking.objects.bulk_update(history_entries, ['part'])
|
||||||
|
history_entries = []
|
||||||
|
print(".", end='', flush=True)
|
||||||
|
|
||||||
if len(history_entries) > 0:
|
if len(history_entries) > 0:
|
||||||
StockItemTracking.objects.bulk_update(history_entries, ['part'])
|
StockItemTracking.objects.bulk_update(history_entries, ['part'])
|
||||||
print(f"\nUpdated {len(history_entries)} StockItemTracking entries with part links")
|
|
||||||
|
|
||||||
|
|
||||||
def remove_null_items(apps, schema_editor):
|
def remove_null_items(apps, schema_editor):
|
||||||
|
|||||||
Reference in New Issue
Block a user