Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fixed the invalid migration of attributes from HStore to JSONB

NyanKiyoshib687fbfa4a2b
diff --git a/saleor/product/migrations/0102_migrate_data_enterprise_grade_attributes.py b/saleor/product/migrations/0102_migrate_data_enterprise_grade_attributes.py
index dae3fb1c4a..7d9feccff3 100644
--- a/saleor/product/migrations/0102_migrate_data_enterprise_grade_attributes.py
+++ b/saleor/product/migrations/0102_migrate_data_enterprise_grade_attributes.py
@@ -23,27 +23,6 @@ def ensure_attribute_slugs_are_unique_or_fix(apps, schema_editor):
attr.save(update_fields=["slug"])
-def migrate_attributes_to_list(model_name):
- """Migrate HStore attributes configuration to JSONB with a list of values."""
-
- def make_migration(apps, schema):
- Model = apps.get_model("product", model_name)
-
- for instance in Model.objects.all():
- new_attributes = {}
-
- for k, v in instance.attributes.items():
- if not isinstance(v, list):
- new_attributes[k] = [v]
- else:
- new_attributes[k] = v
-
- instance.attributes = new_attributes
- instance.save(update_fields=["attributes"])
-
- return make_migration
-
-
def remove_duplicates_products_in_collections(apps, schema_editor):
"""Remove any duplicated M2M, and keep only one of them.
@@ -106,11 +85,6 @@ PRODUCT_TYPE_UNIQUE_SLUGS = [
migrations.RunPython(ensure_attribute_slugs_are_unique_or_fix)
]
-ATTRIBUTE_MULTIPLE_VALUES = [
- migrations.RunPython(migrate_attributes_to_list("Product")),
- migrations.RunPython(migrate_attributes_to_list("ProductVariant")),
-]
-
M2M_UNIQUE_TOGETHER = [migrations.RunPython(remove_duplicates_products_in_collections)]
SORTING_NULLABLE_LOGIC = [
@@ -125,8 +99,5 @@ class Migration(migrations.Migration):
dependencies = [("product", "0101_auto_20190719_0839")]
operations = (
- PRODUCT_TYPE_UNIQUE_SLUGS
- + ATTRIBUTE_MULTIPLE_VALUES
- + M2M_UNIQUE_TOGETHER
- + SORTING_NULLABLE_LOGIC
+ PRODUCT_TYPE_UNIQUE_SLUGS + M2M_UNIQUE_TOGETHER + SORTING_NULLABLE_LOGIC
)
diff --git a/saleor/product/migrations/0104_fix_invalid_attributes_map.py b/saleor/product/migrations/0104_fix_invalid_attributes_map.py
new file mode 100644
index 0000000000..8e7ec46e4c
--- /dev/null
+++ b/saleor/product/migrations/0104_fix_invalid_attributes_map.py
@@ -0,0 +1,41 @@
+# Generated by Django 2.2.4 on 2019-08-12 15:08
+import json
+
+from django.db import migrations
+
+
+def migrate_attributes_to_list(model_name):
+ """Migrate HStore attributes configuration to JSONB with a list of values."""
+
+ def make_migration(apps, schema):
+ Model = apps.get_model("product", model_name)
+
+ for instance in Model.objects.all():
+ new_attributes = {}
+
+ for k, v in instance.attributes.items():
+ if isinstance(v, str) and not v.isnumeric():
+ # Fix a migration error where attributes were stored as repr(list)
+ loaded = json.loads(v.replace("'", '"'))
+ assert isinstance(loaded, list)
+ assert all([isinstance(v_pk, str) for v_pk in loaded])
+ new_attributes[k] = loaded
+ elif not isinstance(v, list):
+ new_attributes[k] = [v]
+ else:
+ new_attributes[k] = v
+
+ instance.attributes = new_attributes
+ instance.save(update_fields=["attributes"])
+
+ return make_migration
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("product", "0103_schema_data_enterprise_grade_attributes")]
+
+ operations = [
+ migrations.RunPython(migrate_attributes_to_list("Product")),
+ migrations.RunPython(migrate_attributes_to_list("ProductVariant")),
+ ]