-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4638 from NyanKiyoshi/attributes/fix/migration
Fixed the invalid migration of attributes from HStore to JSONB
- Loading branch information
Showing
2 changed files
with
42 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
saleor/product/migrations/0104_fix_invalid_attributes_map.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")), | ||
] |