Skip to content

Commit

Permalink
fix(Django): fix daily import script (avoid ON CONFLICT DO UPDATE err…
Browse files Browse the repository at this point in the history
…or. duplicate). ref #376
  • Loading branch information
raphodn committed Sep 15, 2024
1 parent 0656431 commit 99112e5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion open_prices/common/openfoodfacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def import_product_db(flavor: Flavor = Flavor.off, batch_size: int = 1000) -> No
from open_prices.products.models import Product

print((f"Launching import_product_db ({flavor})"))
existing_product_codes = set(Product.objects.values_list("code", flat=True))
existing_product_flavor_codes = set(
Product.objects.filter(source=flavor).values_list("code", flat=True)
)
Expand Down Expand Up @@ -200,9 +201,10 @@ def import_product_db(flavor: Flavor = Flavor.off, batch_size: int = 1000) -> No
product_dict = normalize_product_fields(product_dict)

# Case 1: new OFF product (not in OP database)
if product_code not in existing_product_flavor_codes:
if product_code not in existing_product_codes:
product_dict["code"] = product_code
products_to_create_or_update.append(Product(**product_dict))
existing_product_codes.add(product_code)
added_count += 1

# Case 2: existing product (already in OP database)
Expand All @@ -229,6 +231,7 @@ def import_product_db(flavor: Flavor = Flavor.off, batch_size: int = 1000) -> No
):
Product.objects.bulk_create(
products_to_create_or_update,
ignore_conflicts=True,
update_conflicts=True,
update_fields=OFF_FIELDS,
unique_fields=["code"],
Expand All @@ -239,6 +242,7 @@ def import_product_db(flavor: Flavor = Flavor.off, batch_size: int = 1000) -> No
# final database update
Product.objects.bulk_create(
products_to_create_or_update,
ignore_conflicts=True,
update_conflicts=True,
update_fields=OFF_FIELDS,
unique_fields=["code"],
Expand Down

0 comments on commit 99112e5

Please sign in to comment.