From c6d41e881032a471a8ba9bd1439e24f441905708 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 1 Jan 2024 19:11:37 +0100 Subject: [PATCH] fixed migrations 200 and 204 --- ...pe_options_remove_keyword_icon_and_more.py | 22 ++++++++++++++----- .../migrations/0204_propertytype_fdc_id.py | 10 ++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) 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 104d6a721..a57ad9773 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 @@ -1,7 +1,7 @@ # Generated by Django 4.1.10 on 2023-08-29 11:59 from django.db import migrations -from django.db.models import F, Value +from django.db.models import F, Value, Count from django.db.models.functions import Concat from django_scopes import scopes_disabled @@ -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('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('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('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('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,10 +40,7 @@ class Migration(migrations.Migration): ] operations = [ - - migrations.RunPython( - migrate_icons - ), + migrations.RunPython( migrate_icons), migrations.AlterModelOptions( name='propertytype', options={'ordering': ('order',)}, diff --git a/cookbook/migrations/0204_propertytype_fdc_id.py b/cookbook/migrations/0204_propertytype_fdc_id.py index 1744cc9bc..705984632 100644 --- a/cookbook/migrations/0204_propertytype_fdc_id.py +++ b/cookbook/migrations/0204_propertytype_fdc_id.py @@ -1,15 +1,23 @@ # Generated by Django 4.2.7 on 2023-11-27 21:09 from django.db import migrations, models +from django_scopes import scopes_disabled + + +def fix_fdc_ids(apps, schema_editor): + with scopes_disabled(): + # in case any food had a non digit fdc ID before this migration, remove it + Food = apps.get_model('cookbook', 'Food') + Food.objects.exclude(fdc_id__regex=r'^\d+$').exclude(fdc_id=None).update(fdc_id=None) class Migration(migrations.Migration): - dependencies = [ ('cookbook', '0203_alter_unique_contstraints'), ] operations = [ + migrations.RunPython(fix_fdc_ids), migrations.AddField( model_name='propertytype', name='fdc_id',