Coverage for apps / recipes / migrations / 0007_recipe_profile.py: 90%

10 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 00:40 +0000

1# Generated migration for adding profile ownership to Recipe 

2 

3from django.db import migrations, models 

4import django.db.models.deletion 

5 

6 

7def delete_all_recipes(apps, schema_editor): 

8 """Delete all existing recipes since we're adding required profile field.""" 

9 Recipe = apps.get_model('recipes', 'Recipe') 

10 Recipe.objects.all().delete() 

11 

12 

13def noop(apps, schema_editor): 

14 """Reverse is a no-op.""" 

15 pass 

16 

17 

18class Migration(migrations.Migration): 

19 

20 dependencies = [ 

21 ('profiles', '0001_initial'), 

22 ('recipes', '0006_serving_adjustment_instructions_times'), 

23 ] 

24 

25 operations = [ 

26 # First add profile field as nullable 

27 migrations.AddField( 

28 model_name='recipe', 

29 name='profile', 

30 field=models.ForeignKey( 

31 null=True, 

32 blank=True, 

33 on_delete=django.db.models.deletion.CASCADE, 

34 related_name='recipes', 

35 to='profiles.profile', 

36 ), 

37 ), 

38 

39 # Delete all existing recipes (user said data can be wiped) 

40 migrations.RunPython(delete_all_recipes, noop), 

41 

42 # Now make profile field required (non-nullable) 

43 migrations.AlterField( 

44 model_name='recipe', 

45 name='profile', 

46 field=models.ForeignKey( 

47 on_delete=django.db.models.deletion.CASCADE, 

48 related_name='recipes', 

49 to='profiles.profile', 

50 ), 

51 ), 

52 

53 # Add index for profile field 

54 migrations.AddIndex( 

55 model_name='recipe', 

56 index=models.Index(fields=['profile'], name='recipes_rec_profile_idx'), 

57 ), 

58 ] 

← Back to Dashboard