We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
psycopg2.errors.NotNullViolation: column "slug" contains null values
### What I'm trying to achieve
Upgrade from version 2.9.0 to 2.10.0-rc.1. Running the migrate command successfully.

Result:
```
 Applying product.0111_auto_20191209_0437... OK
  Applying product.0112_auto_20200129_0050...Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: column "slug" contains null values


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
    post_migrate_state = executor.migrate(
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 564, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py", line 152, in _alter_field
    super()._alter_field(
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 710, in _alter_field
    self.execute(
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "slug" contains null values
``` 



### Steps to reproduce the problem
1. Running 2.9.0 version with some data included
2. Upgrade the docker container, try to run the migrate command

### What I expected to happen
Run the migrate command successfully. The migration still seems to be very buggy. We already had issue before as seen here: #5391 

### Screenshots
<!-- If applicable, add screenshots to help explain your problem. -->

**System information**
Operating system:


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `saleor/warehouse/migrations/0003_warehouse_slug.py`
Content:
```
1 # Generated by Django 2.2.9 on 2020-01-29 06:52
2 
3 from django.db import migrations, models
4 from django.db.models.functions import Lower
5 from django.utils.text import slugify
6 
7 
8 def create_unique_slug_for_warehouses(apps, schema_editor):
9     Warehouse = apps.get_model("warehouse", "Warehouse")
10 
11     warehouses = (
12         Warehouse.objects.filter(slug__isnull=True).order_by(Lower("name")).iterator()
13     )
14     previous_char = ""
15     slug_values = []
16     for warehouse in warehouses:
17         first_char = warehouse.name[0].lower()
18         if first_char != previous_char:
19             previous_char = first_char
20             slug_values = Warehouse.objects.filter(
21                 slug__istartswith=first_char
22             ).values_list("slug", flat=True)
23 
24         slug = generate_unique_slug(warehouse, slug_values)
25         warehouse.slug = slug
26         slug_values.append(slug)
27 
28 
29 def generate_unique_slug(instance, slug_values):
30     slug = slugify(instance.name)
31     unique_slug = slug
32     extension = 1
33 
34     while unique_slug in slug_values:
35         extension += 1
36         unique_slug = f"{slug}-{extension}"
37 
38     return unique_slug
39 
40 
41 class Migration(migrations.Migration):
42 
43     dependencies = [
44         ("warehouse", "0002_auto_20200123_0036"),
45     ]
46 
47     operations = [
48         migrations.AddField(
49             model_name="warehouse",
50             name="slug",
51             field=models.SlugField(null=True, max_length=255, unique=True),
52             preserve_default=False,
53         ),
54         migrations.RunPython(
55             create_unique_slug_for_warehouses, migrations.RunPython.noop
56         ),
57         migrations.AlterField(
58             model_name="warehouse",
59             name="slug",
60             field=models.SlugField(max_length=255, unique=True),
61         ),
62     ]
63
```

Path: `saleor/product/migrations/0114_auto_20200129_0815.py`
Content:
```
1 # Generated by Django 2.2.9 on 2020-01-29 14:15
2 
3 from django.db import migrations, models
4 from django.db.models.functions import Lower
5 from django.utils.text import slugify
6 
7 
8 def create_unique_slug_for_products(apps, schema_editor):
9     Product = apps.get_model("product", "Product")
10 
11     products = (
12         Product.objects.filter(slug__isnull=True).order_by(Lower("name")).iterator()
13     )
14     previous_char = ""
15     slug_values = []
16     for product in products:
17         first_char = product.name[0].lower()
18         if first_char != previous_char:
19             previous_char = first_char
20             slug_values = Product.objects.filter(
21                 slug__istartswith=first_char
22             ).values_list("slug", flat=True)
23 
24         slug = generate_unique_slug(product, slug_values)
25         product.slug = slug
26         slug_values.append(slug)
27 
28 
29 def generate_unique_slug(instance, slug_values):
30     slug = slugify(instance.name)
31     unique_slug = slug
32     extension = 1
33 
34     while unique_slug in slug_values:
35         extension += 1
36         unique_slug = f"{slug}-{extension}"
37 
38     return unique_slug
39 
40 
41 class Migration(migrations.Migration):
42 
43     dependencies = [
44         ("product", "0113_auto_20200129_0717"),
45     ]
46 
47     operations = [
48         migrations.AddField(
49             model_name="product",
50             name="slug",
51             field=models.SlugField(null=True, max_length=255, unique=True),
52             preserve_default=False,
53         ),
54         migrations.AlterField(
55             model_name="product", name="name", field=models.CharField(max_length=250),
56         ),
57         migrations.RunPython(
58             create_unique_slug_for_products, migrations.RunPython.noop
59         ),
60         migrations.AlterField(
61             model_name="product",
62             name="slug",
63             field=models.SlugField(max_length=255, unique=True),
64         ),
65     ]
66
```

Path: `saleor/product/migrations/0112_auto_20200129_0050.py`
Content:
```
1 # Generated by Django 2.2.9 on 2020-01-29 06:50
2 
3 from collections import defaultdict
4 
5 from django.db import migrations, models
6 from django.db.models.functions import Lower
7 from django.utils.text import slugify
8 
9 
10 def create_unique_slugs_for_producttypes(apps, schema_editor):
11     ProductType = apps.get_model("product", "ProductType")
12 
13     product_types = (
14         ProductType.objects.filter(slug__isnull=True).order_by(Lower("name")).iterator()
15     )
16     previous_char = ""
17     slug_values = []
18     for product_type in product_types:
19         first_char = product_type.name[0].lower()
20         if first_char != previous_char:
21             previous_char = first_char
22             slug_values = list(
23                 ProductType.objects.filter(slug__istartswith=first_char).values_list(
24                     "slug", flat=True
25                 )
26             )
27 
28         slug = generate_unique_slug(product_type, slug_values)
29         product_type.slug = slug
30         slug_values.append(slug)
31 
32 
33 def generate_unique_slug(instance, slug_values_list):
34     slug = slugify(instance.name)
35     unique_slug = slug
36 
37     extension = 1
38 
39     while unique_slug in slug_values_list:
40         extension += 1
41         unique_slug = f"{slug}-{extension}"
42 
43     return unique_slug
44 
45 
46 def update_non_unique_slugs_for_models(apps, schema_editor):
47     models_to_update = ["Category", "Collection"]
48 
49     for model in models_to_update:
50         Model = apps.get_model("product", model)
51 
52         duplicated_slugs = (
53             Model.objects.all()
54             .values("slug")
55             .annotate(duplicated_slug_num=models.Count("slug"))
56             .filter(duplicated_slug_num__gt=1)
57         )
58 
59         slugs_counter = defaultdict(int)
60         for data in duplicated_slugs:
61             slugs_counter[data["slug"]] = data["duplicated_slug_num"]
62 
63         queryset = Model.objects.filter(slug__in=slugs_counter.keys()).order_by("name")
64 
65         for instance in queryset:
66             slugs_counter[instance.slug] -= 1
67             slug = update_slug_to_unique_value(instance.slug, slugs_counter)
68             instance.slug = slug
69             slugs_counter[slug] += 1
70 
71 
72 def update_slug_to_unique_value(slug_value, slugs_counter):
73     unique_slug = slug_value
74     extension = 1
75 
76     while unique_slug in slugs_counter and slugs_counter[unique_slug] > 0:
77         extension += 1
78         unique_slug = f"{slug_value}-{extension}"
79 
80     return unique_slug
81 
82 
83 class Migration(migrations.Migration):
84 
85     dependencies = [
86         ("product", "0111_auto_20191209_0437"),
87     ]
88 
89     operations = [
90         migrations.RunPython(
91             update_non_unique_slugs_for_models, migrations.RunPython.noop
92         ),
93         migrations.AddField(
94             model_name="producttype",
95             name="slug",
96             field=models.SlugField(null=True, max_length=128, unique=True),
97             preserve_default=False,
98         ),
99         migrations.AlterField(
100             model_name="category",
101             name="slug",
102             field=models.SlugField(max_length=128, unique=True),
103         ),
104         migrations.AlterField(
105             model_name="collection",
106             name="slug",
107             field=models.SlugField(max_length=128, unique=True),
108         ),
109         migrations.RunPython(
110             create_unique_slugs_for_producttypes, migrations.RunPython.noop
111         ),
112         migrations.AlterField(
113             model_name="producttype",
114             name="slug",
115             field=models.SlugField(max_length=128, unique=True),
116         ),
117     ]
118
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/saleor/product/migrations/0112_auto_20200129_0050.py b/saleor/product/migrations/0112_auto_20200129_0050.py
--- a/saleor/product/migrations/0112_auto_20200129_0050.py
+++ b/saleor/product/migrations/0112_auto_20200129_0050.py
@@ -27,6 +27,7 @@
 
         slug = generate_unique_slug(product_type, slug_values)
         product_type.slug = slug
+        product_type.save(update_fields=["slug"])
         slug_values.append(slug)
 
 
@@ -66,6 +67,7 @@
             slugs_counter[instance.slug] -= 1
             slug = update_slug_to_unique_value(instance.slug, slugs_counter)
             instance.slug = slug
+            instance.save(update_fields=["slug"])
             slugs_counter[slug] += 1
 
 
diff --git a/saleor/product/migrations/0114_auto_20200129_0815.py b/saleor/product/migrations/0114_auto_20200129_0815.py
--- a/saleor/product/migrations/0114_auto_20200129_0815.py
+++ b/saleor/product/migrations/0114_auto_20200129_0815.py
@@ -17,12 +17,15 @@
         first_char = product.name[0].lower()
         if first_char != previous_char:
             previous_char = first_char
-            slug_values = Product.objects.filter(
-                slug__istartswith=first_char
-            ).values_list("slug", flat=True)
+            slug_values = list(
+                Product.objects.filter(slug__istartswith=first_char).values_list(
+                    "slug", flat=True
+                )
+            )
 
         slug = generate_unique_slug(product, slug_values)
         product.slug = slug
+        product.save(update_fields=["slug"])
         slug_values.append(slug)
 
 
diff --git a/saleor/warehouse/migrations/0003_warehouse_slug.py b/saleor/warehouse/migrations/0003_warehouse_slug.py
--- a/saleor/warehouse/migrations/0003_warehouse_slug.py
+++ b/saleor/warehouse/migrations/0003_warehouse_slug.py
@@ -17,12 +17,15 @@
         first_char = warehouse.name[0].lower()
         if first_char != previous_char:
             previous_char = first_char
-            slug_values = Warehouse.objects.filter(
-                slug__istartswith=first_char
-            ).values_list("slug", flat=True)
+            slug_values = list(
+                Warehouse.objects.filter(slug__istartswith=first_char).values_list(
+                    "slug", flat=True
+                )
+            )
 
         slug = generate_unique_slug(warehouse, slug_values)
         warehouse.slug = slug
+        warehouse.save(update_fields=["slug"])
         slug_values.append(slug)

{"golden_diff": "diff --git a/saleor/product/migrations/0112_auto_20200129_0050.py b/saleor/product/migrations/0112_auto_20200129_0050.py\n--- a/saleor/product/migrations/0112_auto_20200129_0050.py\n+++ b/saleor/product/migrations/0112_auto_20200129_0050.py\n@@ -27,6 +27,7 @@\n \n         slug = generate_unique_slug(product_type, slug_values)\n         product_type.slug = slug\n+        product_type.save(update_fields=[\"slug\"])\n         slug_values.append(slug)\n \n \n@@ -66,6 +67,7 @@\n             slugs_counter[instance.slug] -= 1\n             slug = update_slug_to_unique_value(instance.slug, slugs_counter)\n             instance.slug = slug\n+            instance.save(update_fields=[\"slug\"])\n             slugs_counter[slug] += 1\n \n \ndiff --git a/saleor/product/migrations/0114_auto_20200129_0815.py b/saleor/product/migrations/0114_auto_20200129_0815.py\n--- a/saleor/product/migrations/0114_auto_20200129_0815.py\n+++ b/saleor/product/migrations/0114_auto_20200129_0815.py\n@@ -17,12 +17,15 @@\n         first_char = product.name[0].lower()\n         if first_char != previous_char:\n             previous_char = first_char\n-            slug_values = Product.objects.filter(\n-                slug__istartswith=first_char\n-            ).values_list(\"slug\", flat=True)\n+            slug_values = list(\n+                Product.objects.filter(slug__istartswith=first_char).values_list(\n+                    \"slug\", flat=True\n+                )\n+            )\n \n         slug = generate_unique_slug(product, slug_values)\n         product.slug = slug\n+        product.save(update_fields=[\"slug\"])\n         slug_values.append(slug)\n \n \ndiff --git a/saleor/warehouse/migrations/0003_warehouse_slug.py b/saleor/warehouse/migrations/0003_warehouse_slug.py\n--- a/saleor/warehouse/migrations/0003_warehouse_slug.py\n+++ b/saleor/warehouse/migrations/0003_warehouse_slug.py\n@@ -17,12 +17,15 @@\n         first_char = warehouse.name[0].lower()\n         if first_char != previous_char:\n             previous_char = first_char\n-            slug_values = Warehouse.objects.filter(\n-                slug__istartswith=first_char\n-            ).values_list(\"slug\", flat=True)\n+            slug_values = list(\n+                Warehouse.objects.filter(slug__istartswith=first_char).values_list(\n+                    \"slug\", flat=True\n+                )\n+            )\n \n         slug = generate_unique_slug(warehouse, slug_values)\n         warehouse.slug = slug\n+        warehouse.save(update_fields=[\"slug\"])\n         slug_values.append(slug)\n", "issue": "psycopg2.errors.NotNullViolation: column \"slug\" contains null values\n### What I'm trying to achieve\r\nUpgrade from version 2.9.0 to 2.10.0-rc.1. Running the migrate command successfully.\r\n\r\nResult:\r\n```\r\n Applying product.0111_auto_20191209_0437... OK\r\n  Applying product.0112_auto_20200129_0050...Traceback (most recent call last):\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py\", line 86, in _execute\r\n    return self.cursor.execute(sql, params)\r\npsycopg2.errors.NotNullViolation: column \"slug\" contains null values\r\n\r\n\r\nThe above exception was the direct cause of the following exception:\r\n\r\nTraceback (most recent call last):\r\n  File \"manage.py\", line 10, in <module>\r\n    execute_from_command_line(sys.argv)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py\", line 401, in execute_from_command_line\r\n    utility.execute()\r\n  File \"/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py\", line 395, in execute\r\n    self.fetch_command(subcommand).run_from_argv(self.argv)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/core/management/base.py\", line 328, in run_from_argv\r\n    self.execute(*args, **cmd_options)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/core/management/base.py\", line 369, in execute\r\n    output = self.handle(*args, **options)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/core/management/base.py\", line 83, in wrapped\r\n    res = handle_func(*args, **kwargs)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py\", line 231, in handle\r\n    post_migrate_state = executor.migrate(\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py\", line 117, in migrate\r\n    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py\", line 147, in _migrate_all_forwards\r\n    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py\", line 245, in apply_migration\r\n    state = migration.apply(state, schema_editor)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/migrations/migration.py\", line 124, in apply\r\n    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/migrations/operations/fields.py\", line 249, in database_forwards\r\n    schema_editor.alter_field(from_model, from_field, to_field)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py\", line 564, in alter_field\r\n    self._alter_field(model, old_field, new_field, old_type, new_type,\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/schema.py\", line 152, in _alter_field\r\n    super()._alter_field(\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py\", line 710, in _alter_field\r\n    self.execute(\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/base/schema.py\", line 142, in execute\r\n    cursor.execute(sql, params)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py\", line 100, in execute\r\n    return super().execute(sql, params)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py\", line 68, in execute\r\n    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py\", line 77, in _execute_with_wrappers\r\n    return executor(sql, params, many, context)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py\", line 86, in _execute\r\n    return self.cursor.execute(sql, params)\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/utils.py\", line 90, in __exit__\r\n    raise dj_exc_value.with_traceback(traceback) from exc_value\r\n  File \"/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py\", line 86, in _execute\r\n    return self.cursor.execute(sql, params)\r\ndjango.db.utils.IntegrityError: column \"slug\" contains null values\r\n``` \r\n\r\n\r\n\r\n### Steps to reproduce the problem\r\n1. Running 2.9.0 version with some data included\r\n2. Upgrade the docker container, try to run the migrate command\r\n\r\n### What I expected to happen\r\nRun the migrate command successfully. The migration still seems to be very buggy. We already had issue before as seen here: #5391 \r\n\r\n### Screenshots\r\n<!-- If applicable, add screenshots to help explain your problem. -->\r\n\r\n**System information**\r\nOperating system:\r\n\n", "before_files": [{"content": "# Generated by Django 2.2.9 on 2020-01-29 06:52\n\nfrom django.db import migrations, models\nfrom django.db.models.functions import Lower\nfrom django.utils.text import slugify\n\n\ndef create_unique_slug_for_warehouses(apps, schema_editor):\n    Warehouse = apps.get_model(\"warehouse\", \"Warehouse\")\n\n    warehouses = (\n        Warehouse.objects.filter(slug__isnull=True).order_by(Lower(\"name\")).iterator()\n    )\n    previous_char = \"\"\n    slug_values = []\n    for warehouse in warehouses:\n        first_char = warehouse.name[0].lower()\n        if first_char != previous_char:\n            previous_char = first_char\n            slug_values = Warehouse.objects.filter(\n                slug__istartswith=first_char\n            ).values_list(\"slug\", flat=True)\n\n        slug = generate_unique_slug(warehouse, slug_values)\n        warehouse.slug = slug\n        slug_values.append(slug)\n\n\ndef generate_unique_slug(instance, slug_values):\n    slug = slugify(instance.name)\n    unique_slug = slug\n    extension = 1\n\n    while unique_slug in slug_values:\n        extension += 1\n        unique_slug = f\"{slug}-{extension}\"\n\n    return unique_slug\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        (\"warehouse\", \"0002_auto_20200123_0036\"),\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name=\"warehouse\",\n            name=\"slug\",\n            field=models.SlugField(null=True, max_length=255, unique=True),\n            preserve_default=False,\n        ),\n        migrations.RunPython(\n            create_unique_slug_for_warehouses, migrations.RunPython.noop\n        ),\n        migrations.AlterField(\n            model_name=\"warehouse\",\n            name=\"slug\",\n            field=models.SlugField(max_length=255, unique=True),\n        ),\n    ]\n", "path": "saleor/warehouse/migrations/0003_warehouse_slug.py"}, {"content": "# Generated by Django 2.2.9 on 2020-01-29 14:15\n\nfrom django.db import migrations, models\nfrom django.db.models.functions import Lower\nfrom django.utils.text import slugify\n\n\ndef create_unique_slug_for_products(apps, schema_editor):\n    Product = apps.get_model(\"product\", \"Product\")\n\n    products = (\n        Product.objects.filter(slug__isnull=True).order_by(Lower(\"name\")).iterator()\n    )\n    previous_char = \"\"\n    slug_values = []\n    for product in products:\n        first_char = product.name[0].lower()\n        if first_char != previous_char:\n            previous_char = first_char\n            slug_values = Product.objects.filter(\n                slug__istartswith=first_char\n            ).values_list(\"slug\", flat=True)\n\n        slug = generate_unique_slug(product, slug_values)\n        product.slug = slug\n        slug_values.append(slug)\n\n\ndef generate_unique_slug(instance, slug_values):\n    slug = slugify(instance.name)\n    unique_slug = slug\n    extension = 1\n\n    while unique_slug in slug_values:\n        extension += 1\n        unique_slug = f\"{slug}-{extension}\"\n\n    return unique_slug\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        (\"product\", \"0113_auto_20200129_0717\"),\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name=\"product\",\n            name=\"slug\",\n            field=models.SlugField(null=True, max_length=255, unique=True),\n            preserve_default=False,\n        ),\n        migrations.AlterField(\n            model_name=\"product\", name=\"name\", field=models.CharField(max_length=250),\n        ),\n        migrations.RunPython(\n            create_unique_slug_for_products, migrations.RunPython.noop\n        ),\n        migrations.AlterField(\n            model_name=\"product\",\n            name=\"slug\",\n            field=models.SlugField(max_length=255, unique=True),\n        ),\n    ]\n", "path": "saleor/product/migrations/0114_auto_20200129_0815.py"}, {"content": "# Generated by Django 2.2.9 on 2020-01-29 06:50\n\nfrom collections import defaultdict\n\nfrom django.db import migrations, models\nfrom django.db.models.functions import Lower\nfrom django.utils.text import slugify\n\n\ndef create_unique_slugs_for_producttypes(apps, schema_editor):\n    ProductType = apps.get_model(\"product\", \"ProductType\")\n\n    product_types = (\n        ProductType.objects.filter(slug__isnull=True).order_by(Lower(\"name\")).iterator()\n    )\n    previous_char = \"\"\n    slug_values = []\n    for product_type in product_types:\n        first_char = product_type.name[0].lower()\n        if first_char != previous_char:\n            previous_char = first_char\n            slug_values = list(\n                ProductType.objects.filter(slug__istartswith=first_char).values_list(\n                    \"slug\", flat=True\n                )\n            )\n\n        slug = generate_unique_slug(product_type, slug_values)\n        product_type.slug = slug\n        slug_values.append(slug)\n\n\ndef generate_unique_slug(instance, slug_values_list):\n    slug = slugify(instance.name)\n    unique_slug = slug\n\n    extension = 1\n\n    while unique_slug in slug_values_list:\n        extension += 1\n        unique_slug = f\"{slug}-{extension}\"\n\n    return unique_slug\n\n\ndef update_non_unique_slugs_for_models(apps, schema_editor):\n    models_to_update = [\"Category\", \"Collection\"]\n\n    for model in models_to_update:\n        Model = apps.get_model(\"product\", model)\n\n        duplicated_slugs = (\n            Model.objects.all()\n            .values(\"slug\")\n            .annotate(duplicated_slug_num=models.Count(\"slug\"))\n            .filter(duplicated_slug_num__gt=1)\n        )\n\n        slugs_counter = defaultdict(int)\n        for data in duplicated_slugs:\n            slugs_counter[data[\"slug\"]] = data[\"duplicated_slug_num\"]\n\n        queryset = Model.objects.filter(slug__in=slugs_counter.keys()).order_by(\"name\")\n\n        for instance in queryset:\n            slugs_counter[instance.slug] -= 1\n            slug = update_slug_to_unique_value(instance.slug, slugs_counter)\n            instance.slug = slug\n            slugs_counter[slug] += 1\n\n\ndef update_slug_to_unique_value(slug_value, slugs_counter):\n    unique_slug = slug_value\n    extension = 1\n\n    while unique_slug in slugs_counter and slugs_counter[unique_slug] > 0:\n        extension += 1\n        unique_slug = f\"{slug_value}-{extension}\"\n\n    return unique_slug\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        (\"product\", \"0111_auto_20191209_0437\"),\n    ]\n\n    operations = [\n        migrations.RunPython(\n            update_non_unique_slugs_for_models, migrations.RunPython.noop\n        ),\n        migrations.AddField(\n            model_name=\"producttype\",\n            name=\"slug\",\n            field=models.SlugField(null=True, max_length=128, unique=True),\n            preserve_default=False,\n        ),\n        migrations.AlterField(\n            model_name=\"category\",\n            name=\"slug\",\n            field=models.SlugField(max_length=128, unique=True),\n        ),\n        migrations.AlterField(\n            model_name=\"collection\",\n            name=\"slug\",\n            field=models.SlugField(max_length=128, unique=True),\n        ),\n        migrations.RunPython(\n            create_unique_slugs_for_producttypes, migrations.RunPython.noop\n        ),\n        migrations.AlterField(\n            model_name=\"producttype\",\n            name=\"slug\",\n            field=models.SlugField(max_length=128, unique=True),\n        ),\n    ]\n", "path": "saleor/product/migrations/0112_auto_20200129_0050.py"}], "after_files": [{"content": "# Generated by Django 2.2.9 on 2020-01-29 06:52\n\nfrom django.db import migrations, models\nfrom django.db.models.functions import Lower\nfrom django.utils.text import slugify\n\n\ndef create_unique_slug_for_warehouses(apps, schema_editor):\n    Warehouse = apps.get_model(\"warehouse\", \"Warehouse\")\n\n    warehouses = (\n        Warehouse.objects.filter(slug__isnull=True).order_by(Lower(\"name\")).iterator()\n    )\n    previous_char = \"\"\n    slug_values = []\n    for warehouse in warehouses:\n        first_char = warehouse.name[0].lower()\n        if first_char != previous_char:\n            previous_char = first_char\n            slug_values = list(\n                Warehouse.objects.filter(slug__istartswith=first_char).values_list(\n                    \"slug\", flat=True\n                )\n            )\n\n        slug = generate_unique_slug(warehouse, slug_values)\n        warehouse.slug = slug\n        warehouse.save(update_fields=[\"slug\"])\n        slug_values.append(slug)\n\n\ndef generate_unique_slug(instance, slug_values):\n    slug = slugify(instance.name)\n    unique_slug = slug\n    extension = 1\n\n    while unique_slug in slug_values:\n        extension += 1\n        unique_slug = f\"{slug}-{extension}\"\n\n    return unique_slug\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        (\"warehouse\", \"0002_auto_20200123_0036\"),\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name=\"warehouse\",\n            name=\"slug\",\n            field=models.SlugField(null=True, max_length=255, unique=True),\n            preserve_default=False,\n        ),\n        migrations.RunPython(\n            create_unique_slug_for_warehouses, migrations.RunPython.noop\n        ),\n        migrations.AlterField(\n            model_name=\"warehouse\",\n            name=\"slug\",\n            field=models.SlugField(max_length=255, unique=True),\n        ),\n    ]\n", "path": "saleor/warehouse/migrations/0003_warehouse_slug.py"}, {"content": "# Generated by Django 2.2.9 on 2020-01-29 14:15\n\nfrom django.db import migrations, models\nfrom django.db.models.functions import Lower\nfrom django.utils.text import slugify\n\n\ndef create_unique_slug_for_products(apps, schema_editor):\n    Product = apps.get_model(\"product\", \"Product\")\n\n    products = (\n        Product.objects.filter(slug__isnull=True).order_by(Lower(\"name\")).iterator()\n    )\n    previous_char = \"\"\n    slug_values = []\n    for product in products:\n        first_char = product.name[0].lower()\n        if first_char != previous_char:\n            previous_char = first_char\n            slug_values = list(\n                Product.objects.filter(slug__istartswith=first_char).values_list(\n                    \"slug\", flat=True\n                )\n            )\n\n        slug = generate_unique_slug(product, slug_values)\n        product.slug = slug\n        product.save(update_fields=[\"slug\"])\n        slug_values.append(slug)\n\n\ndef generate_unique_slug(instance, slug_values):\n    slug = slugify(instance.name)\n    unique_slug = slug\n    extension = 1\n\n    while unique_slug in slug_values:\n        extension += 1\n        unique_slug = f\"{slug}-{extension}\"\n\n    return unique_slug\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        (\"product\", \"0113_auto_20200129_0717\"),\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name=\"product\",\n            name=\"slug\",\n            field=models.SlugField(null=True, max_length=255, unique=True),\n            preserve_default=False,\n        ),\n        migrations.AlterField(\n            model_name=\"product\", name=\"name\", field=models.CharField(max_length=250),\n        ),\n        migrations.RunPython(\n            create_unique_slug_for_products, migrations.RunPython.noop\n        ),\n        migrations.AlterField(\n            model_name=\"product\",\n            name=\"slug\",\n            field=models.SlugField(max_length=255, unique=True),\n        ),\n    ]\n", "path": "saleor/product/migrations/0114_auto_20200129_0815.py"}, {"content": "# Generated by Django 2.2.9 on 2020-01-29 06:50\n\nfrom collections import defaultdict\n\nfrom django.db import migrations, models\nfrom django.db.models.functions import Lower\nfrom django.utils.text import slugify\n\n\ndef create_unique_slugs_for_producttypes(apps, schema_editor):\n    ProductType = apps.get_model(\"product\", \"ProductType\")\n\n    product_types = (\n        ProductType.objects.filter(slug__isnull=True).order_by(Lower(\"name\")).iterator()\n    )\n    previous_char = \"\"\n    slug_values = []\n    for product_type in product_types:\n        first_char = product_type.name[0].lower()\n        if first_char != previous_char:\n            previous_char = first_char\n            slug_values = list(\n                ProductType.objects.filter(slug__istartswith=first_char).values_list(\n                    \"slug\", flat=True\n                )\n            )\n\n        slug = generate_unique_slug(product_type, slug_values)\n        product_type.slug = slug\n        product_type.save(update_fields=[\"slug\"])\n        slug_values.append(slug)\n\n\ndef generate_unique_slug(instance, slug_values_list):\n    slug = slugify(instance.name)\n    unique_slug = slug\n\n    extension = 1\n\n    while unique_slug in slug_values_list:\n        extension += 1\n        unique_slug = f\"{slug}-{extension}\"\n\n    return unique_slug\n\n\ndef update_non_unique_slugs_for_models(apps, schema_editor):\n    models_to_update = [\"Category\", \"Collection\"]\n\n    for model in models_to_update:\n        Model = apps.get_model(\"product\", model)\n\n        duplicated_slugs = (\n            Model.objects.all()\n            .values(\"slug\")\n            .annotate(duplicated_slug_num=models.Count(\"slug\"))\n            .filter(duplicated_slug_num__gt=1)\n        )\n\n        slugs_counter = defaultdict(int)\n        for data in duplicated_slugs:\n            slugs_counter[data[\"slug\"]] = data[\"duplicated_slug_num\"]\n\n        queryset = Model.objects.filter(slug__in=slugs_counter.keys()).order_by(\"name\")\n\n        for instance in queryset:\n            slugs_counter[instance.slug] -= 1\n            slug = update_slug_to_unique_value(instance.slug, slugs_counter)\n            instance.slug = slug\n            instance.save(update_fields=[\"slug\"])\n            slugs_counter[slug] += 1\n\n\ndef update_slug_to_unique_value(slug_value, slugs_counter):\n    unique_slug = slug_value\n    extension = 1\n\n    while unique_slug in slugs_counter and slugs_counter[unique_slug] > 0:\n        extension += 1\n        unique_slug = f\"{slug_value}-{extension}\"\n\n    return unique_slug\n\n\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        (\"product\", \"0111_auto_20191209_0437\"),\n    ]\n\n    operations = [\n        migrations.RunPython(\n            update_non_unique_slugs_for_models, migrations.RunPython.noop\n        ),\n        migrations.AddField(\n            model_name=\"producttype\",\n            name=\"slug\",\n            field=models.SlugField(null=True, max_length=128, unique=True),\n            preserve_default=False,\n        ),\n        migrations.AlterField(\n            model_name=\"category\",\n            name=\"slug\",\n            field=models.SlugField(max_length=128, unique=True),\n        ),\n        migrations.AlterField(\n            model_name=\"collection\",\n            name=\"slug\",\n            field=models.SlugField(max_length=128, unique=True),\n        ),\n        migrations.RunPython(\n            create_unique_slugs_for_producttypes, migrations.RunPython.noop\n        ),\n        migrations.AlterField(\n            model_name=\"producttype\",\n            name=\"slug\",\n            field=models.SlugField(max_length=128, unique=True),\n        ),\n    ]\n", "path": "saleor/product/migrations/0112_auto_20200129_0050.py"}]}