Skip to content

Commit

Permalink
Merge pull request #4638 from NyanKiyoshi/attributes/fix/migration
Browse files Browse the repository at this point in the history
Fixed the invalid migration of attributes from HStore to JSONB
  • Loading branch information
maarcingebala committed Aug 13, 2019
2 parents 38990a1 + b687fbf commit 1943069
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -106,11 +85,6 @@ def reorder_model(apps, schema_editor):
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 = [
Expand All @@ -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
)
41 changes: 41 additions & 0 deletions saleor/product/migrations/0104_fix_invalid_attributes_map.py
Original file line number Diff line number Diff line change
@@ -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")),
]

0 comments on commit 1943069

Please sign in to comment.