Skip to content

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

Fix migration to a new field type

akjanike49ba32cab39
diff --git a/saleor/userprofile/migrations/0013_auto_20171120_0521.py b/saleor/userprofile/migrations/0013_auto_20171120_0521.py
index 63e7a6fc09..2db4886660 100644
--- a/saleor/userprofile/migrations/0013_auto_20171120_0521.py
+++ b/saleor/userprofile/migrations/0013_auto_20171120_0521.py
@@ -4,6 +4,26 @@ from __future__ import unicode_literals
from django.db import migrations
import phonenumber_field.modelfields
+import phonenumbers
+
+
+def convert_phone_number_to_phonenumberfield(apps, schema_editor):
+ Address = apps.get_model("userprofile", "Address")
+ for address in Address.objects.all():
+ if address.phone:
+ phone_number = phonenumbers.parse(address.phone.raw_input,
+ address.country.code)
+ address.phone = phone_number
+ address.save()
+
+
+def convert_phone_number_to_str(apps, schema_editor):
+ Address = apps.get_model("userprofile", "Address")
+ for address in Address.objects.all():
+ if address.phone:
+ phone_number = str(address.phone)
+ address.phone = phone_number
+ address.save()
class Migration(migrations.Migration):
@@ -18,4 +38,6 @@ class Migration(migrations.Migration):
name='phone',
field=phonenumber_field.modelfields.PhoneNumberField(blank=True, default='', max_length=128, verbose_name='phone number'),
),
+ migrations.RunPython(convert_phone_number_to_phonenumberfield,
+ convert_phone_number_to_str),
]
diff --git a/saleor/userprofile/migrations/0014_auto_20171121_0430.py b/saleor/userprofile/migrations/0014_auto_20171121_0430.py
deleted file mode 100644
index d31780c873..0000000000
--- a/saleor/userprofile/migrations/0014_auto_20171121_0430.py
+++ /dev/null
@@ -1,37 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.11.5 on 2017-11-21 10:30
-from __future__ import unicode_literals
-import random
-
-from django.db import migrations
-from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
-
-
-REVERSED_COUNTRY_CODE = dict(
- zip(COUNTRY_CODE_TO_REGION_CODE.values(),
- COUNTRY_CODE_TO_REGION_CODE.keys()))
-
-
-def add_phone_number(apps, schema_editor):
- # We get the model from the versioned app registry;
- # if we directly import it, it'll be the wrong version
- Address = apps.get_model("userprofile", "Address")
- db_alias = schema_editor.connection.alias
- for address in Address.objects.all():
- if (address.country,) in REVERSED_COUNTRY_CODE:
- phone_number = '+{}{}'.format(
- REVERSED_COUNTRY_CODE[(address.country,)],
- str(random.randint(10**9, 10**10 - 1)))
- address.phone = phone_number
- address.save()
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('userprofile', '0013_auto_20171120_0521'),
- ]
-
- operations = [
- migrations.RunPython(add_phone_number),
- ]