From 36c0fbffbeebb940c8bcabcb1bf7f663e07e57bb Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 5 Feb 2024 14:52:40 +0100 Subject: [PATCH] fixed duplicate detection in migration 0200 --- .../management/commands/seed_basic_data.py | 25 +++++++++++++++++++ ...pe_options_remove_keyword_icon_and_more.py | 17 ++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 cookbook/management/commands/seed_basic_data.py diff --git a/cookbook/management/commands/seed_basic_data.py b/cookbook/management/commands/seed_basic_data.py new file mode 100644 index 00000000..25e5ef98 --- /dev/null +++ b/cookbook/management/commands/seed_basic_data.py @@ -0,0 +1,25 @@ +from django.conf import settings +from django.contrib.auth.models import User +from django.contrib.postgres.search import SearchVector +from django.core.management.base import BaseCommand +from django.utils import translation +from django.utils.translation import gettext_lazy as _ +from django_scopes import scopes_disabled + +from cookbook.managers import DICTIONARY +from cookbook.models import Recipe, Step, Space + + +class Command(BaseCommand): + help = 'Seeds some basic data (space, account, food)' + + def handle(self, *args, **options): + with scopes_disabled(): + user = User.objects.get_or_create(username='test')[0] + user.set_password('test') + user.save() + + space = Space.objects.get_or_create( + name='Test Space', + created_by=user + )[0] diff --git a/cookbook/migrations/0200_alter_propertytype_options_remove_keyword_icon_and_more.py b/cookbook/migrations/0200_alter_propertytype_options_remove_keyword_icon_and_more.py index f67a4188..03f7aa6d 100644 --- a/cookbook/migrations/0200_alter_propertytype_options_remove_keyword_icon_and_more.py +++ b/cookbook/migrations/0200_alter_propertytype_options_remove_keyword_icon_and_more.py @@ -13,9 +13,24 @@ def migrate_icons(apps, schema_editor): PropertyType = apps.get_model('cookbook', 'PropertyType') RecipeBook = apps.get_model('cookbook', 'RecipeBook') + duplicate_meal_types = MealType.objects.values('space_id', 'name').annotate(name_count=Count('name')).exclude(name_count=1).all() + if len(duplicate_meal_types) > 0: + raise RuntimeError(f'Duplicate MealTypes found, please remove/rename them and run migrations again/restart the container. {duplicate_meal_types}') MealType.objects.update(name=Concat(F('icon'), Value(' '), F('name'))) + + duplicate_meal_types = Keyword.objects.values('space_id', 'name').annotate(name_count=Count('name')).exclude(name_count=1).all() + if len(duplicate_meal_types) > 0: + raise RuntimeError(f'Duplicate Keyword found, please remove/rename them and run migrations again/restart the container. {duplicate_meal_types}') Keyword.objects.update(name=Concat(F('icon'), Value(' '), F('name'))) + + duplicate_meal_types = PropertyType.objects.values('space_id', 'name').annotate(name_count=Count('name')).exclude(name_count=1).all() + if len(duplicate_meal_types) > 0: + raise RuntimeError(f'Duplicate PropertyType found, please remove/rename them and run migrations again/restart the container. {duplicate_meal_types}') PropertyType.objects.update(name=Concat(F('icon'), Value(' '), F('name'))) + + duplicate_meal_types = RecipeBook.objects.values('space_id', 'name').annotate(name_count=Count('name')).exclude(name_count=1).all() + if len(duplicate_meal_types) > 0: + raise RuntimeError(f'Duplicate RecipeBook found, please remove/rename them and run migrations again/restart the container. {duplicate_meal_types}') RecipeBook.objects.update(name=Concat(F('icon'), Value(' '), F('name'))) @@ -25,7 +40,7 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython( migrate_icons), + migrations.RunPython(migrate_icons), migrations.AlterModelOptions( name='propertytype', options={'ordering': ('order',)},