Skip to content

Commit

Permalink
Add UUIDs to some of the nutrition models
Browse files Browse the repository at this point in the history
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
rolandgeider committed Oct 20, 2024
1 parent 22a72f9 commit f700637
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 71 deletions.
Empty file modified manage.py
100644 → 100755
Empty file.
122 changes: 122 additions & 0 deletions wger/nutrition/migrations/0025_add_uuids.py
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,
),
),
]
69 changes: 0 additions & 69 deletions wger/nutrition/migrations/0025_create_ivm_entries.py

This file was deleted.

117 changes: 117 additions & 0 deletions wger/nutrition/migrations/0026_create_ivm_entries.py
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),
]
8 changes: 8 additions & 0 deletions wger/nutrition/models/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Standard Library
import uuid
from decimal import Decimal

# Django
Expand Down Expand Up @@ -47,6 +48,13 @@ class Meta:
'-datetime',
]

uuid = models.UUIDField(
default=uuid.uuid4,
editable=False,
null=False,
unique=True,
)

plan = models.ForeignKey(
NutritionPlan,
verbose_name=_('Nutrition plan'),
Expand Down
8 changes: 8 additions & 0 deletions wger/nutrition/models/meal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Standard Library
import logging
import uuid

# Django
from django.db import models
Expand Down Expand Up @@ -43,6 +44,13 @@ class Meta:
'time',
]

uuid = models.UUIDField(
default=uuid.uuid4,
editable=False,
null=False,
unique=True,
)

plan = models.ForeignKey(
NutritionPlan,
verbose_name=_('Nutrition plan'),
Expand Down
8 changes: 8 additions & 0 deletions wger/nutrition/models/meal_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# Standard Library
import logging
import uuid
from decimal import Decimal

# Django
Expand Down Expand Up @@ -43,6 +44,13 @@ class MealItem(BaseMealItem, models.Model):
An item (component) of a meal
"""

uuid = models.UUIDField(
default=uuid.uuid4,
editable=False,
null=False,
unique=True,
)

meal = models.ForeignKey(
Meal,
verbose_name=_('Nutrition plan'),
Expand Down
Loading

0 comments on commit f700637

Please sign in to comment.