mirror of
https://github.com/jazzband/django-categories.git
synced 2026-03-16 22:30:24 +00:00
make datamigration for non-unique slugs, test the migration
This commit is contained in:
parent
841e14a17c
commit
5f3eba8f1c
3 changed files with 66 additions and 1 deletions
|
|
@ -2,14 +2,32 @@
|
|||
|
||||
from django.db import migrations, models
|
||||
|
||||
from categories.models import Category
|
||||
|
||||
|
||||
def make_slugs_unique(apps, schema_editor):
|
||||
duplicates = Category.tree.values("slug").annotate(slug_count=models.Count("slug")).filter(slug_count__gt=1)
|
||||
category_objs = []
|
||||
for duplicate in duplicates:
|
||||
slug = duplicate["slug"]
|
||||
categories = Category.tree.filter(slug=slug)
|
||||
count = categories.count()
|
||||
i = 0
|
||||
for category in categories.all():
|
||||
if i != 0:
|
||||
category.slug = "{}-{}".format(slug, str(i).zfill(len(str(count))))
|
||||
category_objs.append(category)
|
||||
i += 1
|
||||
Category.objects.bulk_update(category_objs, ["slug"])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("categories", "0004_auto_20200517_1832"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(make_slugs_unique, reverse_code=migrations.RunPython.noop),
|
||||
migrations.AlterField(
|
||||
model_name="category",
|
||||
name="slug",
|
||||
|
|
|
|||
42
categories/tests/test_migrations.py
Normal file
42
categories/tests/test_migrations.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import sys
|
||||
|
||||
if sys.version_info >= (3, 0):
|
||||
from django_test_migrations.contrib.unittest_case import MigratorTestCase
|
||||
|
||||
class TestMigrations(MigratorTestCase):
|
||||
migrate_from = ("categories", "0004_auto_20200517_1832")
|
||||
migrate_to = ("categories", "0005_unique_category_slug")
|
||||
|
||||
def prepare(self):
|
||||
Category = self.old_state.apps.get_model("categories", "Category")
|
||||
Category.tree.create(slug="foo", lft=0, rght=0, tree_id=0, level=0)
|
||||
Category.tree.create(slug="foo", lft=0, rght=0, tree_id=0, level=0)
|
||||
Category.tree.create(slug="foo", lft=0, rght=0, tree_id=0, level=0)
|
||||
for i in range(1, 12):
|
||||
Category.tree.create(slug="bar", lft=0, rght=0, tree_id=0, level=0)
|
||||
Category.tree.create(slug="baz", lft=0, rght=0, tree_id=0, level=0)
|
||||
assert Category.tree.count() == 15
|
||||
|
||||
def test_unique_slug_migration(self):
|
||||
Category = self.new_state.apps.get_model("categories", "Category")
|
||||
|
||||
self.assertListEqual(
|
||||
list(Category.tree.values_list("slug", flat=True)),
|
||||
[
|
||||
"foo",
|
||||
"foo-1",
|
||||
"foo-2",
|
||||
"bar",
|
||||
"bar-01",
|
||||
"bar-02",
|
||||
"bar-03",
|
||||
"bar-04",
|
||||
"bar-05",
|
||||
"bar-06",
|
||||
"bar-07",
|
||||
"bar-08",
|
||||
"bar-09",
|
||||
"bar-10",
|
||||
"baz",
|
||||
],
|
||||
)
|
||||
5
tox.ini
5
tox.ini
|
|
@ -37,6 +37,11 @@ deps=
|
|||
pillow
|
||||
ipdb
|
||||
codecov
|
||||
django-test-migrations
|
||||
django21: django-test-migrations<=1.2.0
|
||||
django22: django-test-migrations<=1.2.0
|
||||
django3: django-test-migrations<=1.2.0
|
||||
django31: django-test-migrations<=1.2.0
|
||||
-r{toxinidir}/requirements.txt
|
||||
|
||||
commands=
|
||||
|
|
|
|||
Loading…
Reference in a new issue