Skip to content

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

Migrate data about stock location to new table

Mira Szewczyka192c111f215
diff --git a/saleor/product/migrations/0013_auto_20161205_0920.py b/saleor/product/migrations/0013_auto_20161207_0555.py
similarity index 73%
rename from saleor/product/migrations/0013_auto_20161205_0920.py
rename to saleor/product/migrations/0013_auto_20161207_0555.py
index 5c8f82a7a6..fb979003e8 100644
--- a/saleor/product/migrations/0013_auto_20161205_0920.py
+++ b/saleor/product/migrations/0013_auto_20161207_0555.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-# Generated by Django 1.10.3 on 2016-12-05 15:20
+# Generated by Django 1.10.3 on 2016-12-07 11:55
from __future__ import unicode_literals
from django.db import migrations, models
@@ -20,9 +20,9 @@ class Migration(migrations.Migration):
('location', models.CharField(max_length=100, verbose_name='location')),
],
),
- migrations.AlterField(
+ migrations.AddField(
model_name='stock',
- name='location',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='product.StockLocation'),
+ name='location_link',
+ field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='product.StockLocation'),
),
]
diff --git a/saleor/product/migrations/0014_auto_20161207_0840.py b/saleor/product/migrations/0014_auto_20161207_0840.py
new file mode 100644
index 0000000000..971b9a6af4
--- /dev/null
+++ b/saleor/product/migrations/0014_auto_20161207_0840.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-07 14:40
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0013_auto_20161207_0555'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='stock',
+ name='location',
+ field=models.CharField(max_length=100, null=True, verbose_name='location'),
+ ),
+ ]
diff --git a/saleor/product/migrations/0015_transfer_locations.py b/saleor/product/migrations/0015_transfer_locations.py
new file mode 100644
index 0000000000..875c1c87ac
--- /dev/null
+++ b/saleor/product/migrations/0015_transfer_locations.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-07 14:41
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+def forward_transfer_locations(apps, schema_editor):
+ Stock = apps.get_model('product', 'Stock')
+ StockLocation = apps.get_model('product', 'StockLocation')
+ for stock in Stock.objects.all():
+ location = StockLocation.objects.get_or_create(
+ location=stock.location)[0]
+ stock.location_link = location
+ stock.save()
+
+
+def reverse_transfer_locations(apps, schema_editor):
+ Stock = apps.get_model('product', 'Stock')
+ for stock in Stock.objects.all():
+ if stock.location_link:
+ location = stock.location_link.location
+ stock.location = location
+ stock.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0014_auto_20161207_0840'),
+ ]
+
+ operations = [
+ migrations.RunPython(forward_transfer_locations,
+ reverse_transfer_locations)
+ ]
diff --git a/saleor/product/migrations/0016_auto_20161207_0843.py b/saleor/product/migrations/0016_auto_20161207_0843.py
new file mode 100644
index 0000000000..c5d89050a4
--- /dev/null
+++ b/saleor/product/migrations/0016_auto_20161207_0843.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-07 14:43
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0015_transfer_locations'),
+ ]
+
+ operations = [
+ migrations.AlterUniqueTogether(
+ name='stock',
+ unique_together=set([('variant', 'location_link')]),
+ ),
+ ]
diff --git a/saleor/product/migrations/0017_remove_stock_location.py b/saleor/product/migrations/0017_remove_stock_location.py
new file mode 100644
index 0000000000..9af6abbb41
--- /dev/null
+++ b/saleor/product/migrations/0017_remove_stock_location.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-07 14:43
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0016_auto_20161207_0843'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='stock',
+ name='location',
+ ),
+ ]
diff --git a/saleor/product/migrations/0018_auto_20161207_0844.py b/saleor/product/migrations/0018_auto_20161207_0844.py
new file mode 100644
index 0000000000..f559480cb5
--- /dev/null
+++ b/saleor/product/migrations/0018_auto_20161207_0844.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.10.3 on 2016-12-07 14:44
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0017_remove_stock_location'),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name='stock',
+ old_name='location_link',
+ new_name='location',
+ ),
+ migrations.AlterUniqueTogether(
+ name='stock',
+ unique_together=set([('variant', 'location')]),
+ ),
+ ]
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index d7c02dc531..03e5ee9d98 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -270,7 +270,7 @@ class Stock(models.Model):
variant = models.ForeignKey(
ProductVariant, related_name='stock',
verbose_name=pgettext_lazy('Stock item field', 'variant'))
- location = models.ForeignKey(StockLocation)
+ location = models.ForeignKey(StockLocation, null=True)
quantity = models.IntegerField(
pgettext_lazy('Stock item field', 'quantity'),
validators=[MinValueValidator(0)], default=Decimal(1))