Repository: saleor/saleor

Issue:
Cart migration fails: cannot drop/alter table `cart_cart`
It looks like merging cart with checkout generated a migration that causes problems:

```console
$ python manage.py migrate --no-input
...
Running migrations:
  Applying checkout.0007_merge_cart_with_checkout...Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.OperationalError: cannot ALTER TABLE "cart_cart" because it has pending trigger events
```


Discussion:
If it can help:

```
ERROR: cannot drop table cart_cart because other objects depend on it DETAIL: constraint cart_cartline_cart_id_c7b9981e_fk_cart_cart_token on table cart_cartline depends on table cart_cart HINT: Use DROP ... CASCADE to drop the dependent objects too.
```
@patrys could you rename the issue title so people can find this issue easier if they run into it (very likely)?

If people don't care about losing carts, they can run this on their database as temporary solution:
```sql
TRUNCATE TABLE cart_cart CASCADE;
```
Just successfully deployed to Heroku, not sure if the issue still persists? 
It only breaks if you have existing data from before that migration and try to upgrade.
@Pacu2 as this migration was introduced by #2206 (v2018.05), it's only concerning people that were using Saleor before the 2018.05 release. 

Here are the steps to reproduce the issue:

1. Checkout and migrate ([v2018.04](https://github.com/mirumee/saleor/tree/v2018.04)),
2. Login into an account (can still be done after adding the item to the cart),
3. Add an item to the cart,
4. Checkout to master (or v2018.05),
5. Run the migrations.

The migrations should fail.
This seems to fix the issue:

```patch
Index: saleor/checkout/migrations/0007_merge_cart_with_checkout.py
<+>UTF-8
===================================================================
--- saleor/checkout/migrations/0007_merge_cart_with_checkout.py (revision 0d508362b76b7e3693b4e9dd5c0ce4b79f48c16f)
+++ saleor/checkout/migrations/0007_merge_cart_with_checkout.py (date 1531873695000)
@@ -5,6 +5,14 @@
 import django_prices.models
 
 
+def fix_carts(apps, schema_editor):
+    Cart = apps.get_model('checkout', 'Cart')
+
+    for cart in Cart.objects.filter(email__isnull=True):
+        cart.email = ''
+        cart.save()
+
+
 class Migration(migrations.Migration):
 
     dependencies = [
@@ -14,6 +22,7 @@
     ]
 
     operations = [
+        migrations.RunPython(fix_carts),
         migrations.AlterModelOptions(
             name='cart',
             options={'ordering': ('-last_change',)},

```

Resolving patch:
diff --git a/saleor/checkout/migrations/0007_merge_cart_with_checkout.py b/saleor/checkout/migrations/0007_merge_cart_with_checkout.py
--- a/saleor/checkout/migrations/0007_merge_cart_with_checkout.py
+++ b/saleor/checkout/migrations/0007_merge_cart_with_checkout.py
@@ -5,6 +5,14 @@
 import django_prices.models
 
 
+def fix_blank_emails_in_carts(apps, schema_editor):
+    Cart = apps.get_model('checkout', 'Cart')
+
+    for cart in Cart.objects.filter(email__isnull=True):
+        cart.email = ''
+        cart.save()
+
+
 class Migration(migrations.Migration):
 
     dependencies = [
@@ -14,6 +22,7 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
+        migrations.RunPython(fix_blank_emails_in_carts),
         migrations.AlterModelOptions(
             name='cart',
             options={'ordering': ('-last_change',)},


Test patch: