-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UUIDs to some of the nutrition models
This is needed in the flutter app to properly be able to set IDs on device, since the regular IDs can only be set by postgres.
- Loading branch information
1 parent
22a72f9
commit f700637
Showing
8 changed files
with
271 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Generated by Django 4.2.16 on 2024-10-19 21:04 | ||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
def gen_uuids(apps, schema_editor): | ||
NutritionPlan = apps.get_model('nutrition', 'NutritionPlan') | ||
Meal = apps.get_model('nutrition', 'Meal') | ||
MealItem = apps.get_model('nutrition', 'MealItem') | ||
LogItem = apps.get_model('nutrition', 'LogItem') | ||
|
||
for item in LogItem.objects.all(): | ||
item.uuid = uuid.uuid4() | ||
item.save(update_fields=['uuid']) | ||
|
||
for item in MealItem.objects.all(): | ||
item.uuid = uuid.uuid4() | ||
item.save(update_fields=['uuid']) | ||
|
||
for meal in Meal.objects.all(): | ||
meal.uuid = uuid.uuid4() | ||
meal.save(update_fields=['uuid']) | ||
|
||
for plan in NutritionPlan.objects.all(): | ||
plan.uuid = uuid.uuid4() | ||
plan.save(update_fields=['uuid']) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('nutrition', '0024_remove_ingredient_status'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='nutritionplan', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=True, | ||
unique=False, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='meal', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=True, | ||
unique=False, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='mealitem', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=True, | ||
unique=False, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='logitem', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=True, | ||
unique=False, | ||
), | ||
), | ||
# Generate UUIDs | ||
migrations.RunPython( | ||
gen_uuids, | ||
reverse_code=migrations.RunPython.noop, | ||
), | ||
# Set uuid fields to non-nullable | ||
migrations.AlterField( | ||
model_name='nutritionplan', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=False, | ||
unique=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='meal', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=False, | ||
unique=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='mealitem', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=False, | ||
unique=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='logitem', | ||
name='uuid', | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
null=False, | ||
unique=True, | ||
), | ||
), | ||
] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from django.db import migrations | ||
|
||
from wger.utils.db import postgres_only | ||
|
||
|
||
@postgres_only | ||
def add_ivm_views(apps, schema_editor): | ||
""" | ||
Note: the select statements are written a bit weirdly because of this issue | ||
https://github.com/sraoss/pg_ivm/issues/85 | ||
When this is resolved, we can remove the subqueries and write e.g. | ||
SELECT m.*, p.user_id | ||
FROM nutrition_meal AS m | ||
JOIN nutrition_nutritionplan AS p ON m.plan_id = p.id; | ||
""" | ||
|
||
schema_editor.execute( | ||
""" | ||
SELECT create_immv( | ||
'ivm_nutrition_nutritionplan', | ||
'SELECT | ||
p.uuid AS id, | ||
p.id AS remote_id, | ||
creation_date, | ||
description, | ||
has_goal_calories, | ||
user_id, | ||
only_logging, | ||
goal_carbohydrates, | ||
goal_energy, | ||
goal_fat, | ||
goal_protein, | ||
goal_fiber | ||
FROM nutrition_nutritionplan AS p;' | ||
); | ||
""" | ||
) | ||
|
||
schema_editor.execute( | ||
""" | ||
SELECT create_immv( | ||
'ivm_nutrition_meal', | ||
'SELECT | ||
m.uuid AS id, | ||
m.id AS remote_id, | ||
"order", | ||
time, | ||
plan_id, | ||
name, | ||
p.user_id | ||
FROM (SELECT * FROM nutrition_meal) AS m | ||
JOIN (SELECT id, user_id FROM nutrition_nutritionplan) AS p ON m.plan_id = p.id;' | ||
); | ||
""" | ||
) | ||
|
||
schema_editor.execute( | ||
""" | ||
SELECT create_immv( | ||
'ivm_nutrition_mealitem', | ||
'SELECT | ||
mi.uuid AS id, | ||
mi.id AS remote_id, | ||
"order", | ||
amount, | ||
ingredient_id, | ||
meal_id, | ||
weight_unit_id, | ||
p.user_id | ||
FROM (SELECT * FROM nutrition_mealitem) AS mi | ||
JOIN (SELECT id, plan_id FROM nutrition_meal) AS m ON mi.meal_id = m.id | ||
JOIN (SELECT id, user_id FROM nutrition_nutritionplan) AS p ON m.plan_id = p.id;' | ||
); | ||
""" | ||
) | ||
|
||
schema_editor.execute( | ||
""" | ||
SELECT create_immv( | ||
'ivm_nutrition_logitem', | ||
'SELECT | ||
li.uuid as id, | ||
li.id AS remote_id, | ||
datetime, | ||
comment, | ||
amount, | ||
ingredient_id, | ||
plan_id, | ||
weight_unit_id, | ||
meal_id, | ||
p.user_id | ||
FROM (SELECT * FROM nutrition_logitem) AS li | ||
JOIN (SELECT id, user_id FROM nutrition_nutritionplan) AS p ON li.plan_id = p.id;' | ||
); | ||
""" | ||
) | ||
|
||
|
||
@postgres_only | ||
def remove_ivm_views(apps, schema_editor): | ||
schema_editor.execute('DROP TABLE IF EXISTS ivm_nutrition_nutritionplan;') | ||
schema_editor.execute('DROP TABLE IF EXISTS ivm_nutrition_meal;') | ||
schema_editor.execute('DROP TABLE IF EXISTS ivm_nutrition_mealitem;') | ||
schema_editor.execute('DROP TABLE IF EXISTS ivm_nutrition_logitem;') | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('nutrition', '0025_add_uuids'), | ||
('core', '0018_create_publication_add_ivm_extension'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_ivm_views, reverse_code=remove_ivm_views), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.