Skip to content

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

Prepare cart migration to new JSONField

Krzysztof Wolskie2959ddb964e
diff --git a/saleor/cart/migrations/0004_auto_20161209_0652.py b/saleor/cart/migrations/0004_auto_20161209_0652.py
new file mode 100644
index 0000000000..2493a81b14
--- /dev/null
+++ b/saleor/cart/migrations/0004_auto_20161209_0652.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-09 12:52
+from __future__ import unicode_literals
+
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('cart', 'fix_empty_data_in_lines'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='cart',
+ name='checkout_data_postgres',
+ field=django.contrib.postgres.fields.jsonb.JSONField(editable=False, null=True),
+ ),
+ migrations.AddField(
+ model_name='cartline',
+ name='data_postgres',
+ field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default={}),
+ ),
+ ]
diff --git a/saleor/cart/migrations/0005_migrate_json_data.py b/saleor/cart/migrations/0005_migrate_json_data.py
new file mode 100644
index 0000000000..8d91ba125e
--- /dev/null
+++ b/saleor/cart/migrations/0005_migrate_json_data.py
@@ -0,0 +1,33 @@
+from __future__ import unicode_literals
+
+import json
+from django.db import migrations
+
+
+def move_data(apps, schema_editor):
+ CartLine = apps.get_model('cart', 'CartLine')
+ for line in CartLine.objects.all():
+ if isinstance(line.data, dict):
+ line.data_postgres = line.data
+ else:
+ line.data_postgres = json.loads(line.data)
+ line.save()
+
+ Cart = apps.get_model('cart', 'Cart')
+ for cart in Cart.objects.all():
+ if isinstance(cart.checkout_data, dict):
+ cart.checkout_data_postgres = cart.checkout_data
+ else:
+ cart.checkout_data_postgres = json.loads(cart.checkout_data)
+ cart.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('cart', '0004_auto_20161209_0652'),
+ ]
+
+ operations = [
+ migrations.RunPython(move_data)
+ ]
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index d350979538..9922cb5de3 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -12,6 +12,7 @@ from django.utils.encoding import python_2_unicode_compatible, smart_str
from django.utils.timezone import now
from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
+from django.contrib.postgres.fields import JSONField as postgres_JSONField
from jsonfield import JSONField
from satchless.item import ItemLine, ItemSet, ItemList, partition
@@ -101,6 +102,7 @@ class Cart(models.Model):
voucher = models.ForeignKey(
Voucher, null=True, related_name='+', on_delete=models.SET_NULL)
checkout_data = JSONField(null=True, editable=False)
+ checkout_data_postgres = postgres_JSONField(null=True, editable=False)
total = PriceField(
currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
@@ -229,6 +231,7 @@ class CartLine(models.Model, ItemLine):
pgettext_lazy('Cart line', 'quantity'),
validators=[MinValueValidator(0), MaxValueValidator(999)])
data = JSONField(blank=True, default={})
+ data_postgres = postgres_JSONField(blank=True, default={})
class Meta:
unique_together = ('cart', 'variant', 'data')