Files
chat/alembic/versions/3f4e5a1b8c9d_create_nutrition_service_tables.py
Andrew K. Choi 537e7b363f
All checks were successful
continuous-integration/drone/push Build is passing
main commit
2025-10-16 16:30:25 +09:00

151 lines
8.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Create nutrition service tables
Revision ID: 3f4e5a1b8c9d
Revises: 2ede6d343f7c
Create Date: 2025-10-16 23:01:02.123456
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '3f4e5a1b8c9d'
down_revision = '49846a45b6b0'
branch_labels = None
depends_on = None
def upgrade():
# Таблица продуктов питания
op.create_table(
'food_items',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('fatsecret_id', sa.String(length=50), nullable=True),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('brand', sa.String(length=255), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('food_type', sa.String(length=50), nullable=True),
sa.Column('serving_size', sa.String(length=100), nullable=True),
sa.Column('serving_weight_grams', sa.Float(), nullable=True),
sa.Column('calories', sa.Float(), nullable=True),
sa.Column('protein_grams', sa.Float(), nullable=True),
sa.Column('fat_grams', sa.Float(), nullable=True),
sa.Column('carbs_grams', sa.Float(), nullable=True),
sa.Column('fiber_grams', sa.Float(), nullable=True),
sa.Column('sugar_grams', sa.Float(), nullable=True),
sa.Column('sodium_mg', sa.Float(), nullable=True),
sa.Column('cholesterol_mg', sa.Float(), nullable=True),
sa.Column('ingredients', sa.Text(), nullable=True),
sa.Column('is_verified', sa.Boolean(), nullable=True),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_food_items_fatsecret_id'), 'food_items', ['fatsecret_id'], unique=True)
op.create_index(op.f('ix_food_items_name'), 'food_items', ['name'], unique=False)
op.create_index(op.f('ix_food_items_uuid'), 'food_items', ['uuid'], unique=True)
# Таблица записей пользователя о потреблении пищи
op.create_table(
'user_nutrition_entries',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('entry_date', sa.Date(), nullable=False),
sa.Column('meal_type', sa.String(length=50), nullable=False),
sa.Column('food_item_id', sa.Integer(), nullable=True),
sa.Column('custom_food_name', sa.String(length=255), nullable=True),
sa.Column('quantity', sa.Float(), nullable=False),
sa.Column('unit', sa.String(length=50), nullable=True),
sa.Column('calories', sa.Float(), nullable=True),
sa.Column('protein_grams', sa.Float(), nullable=True),
sa.Column('fat_grams', sa.Float(), nullable=True),
sa.Column('carbs_grams', sa.Float(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['food_item_id'], ['food_items.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_user_nutrition_entries_entry_date'), 'user_nutrition_entries', ['entry_date'], unique=False)
op.create_index(op.f('ix_user_nutrition_entries_user_id'), 'user_nutrition_entries', ['user_id'], unique=False)
op.create_index(op.f('ix_user_nutrition_entries_uuid'), 'user_nutrition_entries', ['uuid'], unique=True)
# Таблица для отслеживания потребления воды
op.create_table(
'water_intake',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('entry_date', sa.Date(), nullable=False),
sa.Column('amount_ml', sa.Integer(), nullable=False),
sa.Column('entry_time', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('notes', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_water_intake_entry_date'), 'water_intake', ['entry_date'], unique=False)
op.create_index(op.f('ix_water_intake_user_id'), 'water_intake', ['user_id'], unique=False)
op.create_index(op.f('ix_water_intake_uuid'), 'water_intake', ['uuid'], unique=True)
# Таблица для отслеживания физической активности
op.create_table(
'user_activity_entries',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('entry_date', sa.Date(), nullable=False),
sa.Column('activity_type', sa.String(length=100), nullable=False),
sa.Column('duration_minutes', sa.Integer(), nullable=False),
sa.Column('calories_burned', sa.Float(), nullable=True),
sa.Column('distance_km', sa.Float(), nullable=True),
sa.Column('steps', sa.Integer(), nullable=True),
sa.Column('intensity', sa.String(length=20), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_user_activity_entries_entry_date'), 'user_activity_entries', ['entry_date'], unique=False)
op.create_index(op.f('ix_user_activity_entries_user_id'), 'user_activity_entries', ['user_id'], unique=False)
op.create_index(op.f('ix_user_activity_entries_uuid'), 'user_activity_entries', ['uuid'], unique=True)
# Таблица для хранения целей пользователя по питанию и активности
op.create_table(
'nutrition_goals',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('daily_calorie_goal', sa.Integer(), nullable=True),
sa.Column('protein_goal_grams', sa.Integer(), nullable=True),
sa.Column('fat_goal_grams', sa.Integer(), nullable=True),
sa.Column('carbs_goal_grams', sa.Integer(), nullable=True),
sa.Column('water_goal_ml', sa.Integer(), nullable=True),
sa.Column('activity_goal_minutes', sa.Integer(), nullable=True),
sa.Column('weight_goal_kg', sa.Float(), nullable=True),
sa.Column('goal_type', sa.String(length=50), nullable=True),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_nutrition_goals_user_id'), 'nutrition_goals', ['user_id'], unique=True)
def downgrade():
op.drop_index(op.f('ix_nutrition_goals_user_id'), table_name='nutrition_goals')
op.drop_table('nutrition_goals')
op.drop_index(op.f('ix_user_activity_entries_uuid'), table_name='user_activity_entries')
op.drop_index(op.f('ix_user_activity_entries_user_id'), table_name='user_activity_entries')
op.drop_index(op.f('ix_user_activity_entries_entry_date'), table_name='user_activity_entries')
op.drop_table('user_activity_entries')
op.drop_index(op.f('ix_water_intake_uuid'), table_name='water_intake')
op.drop_index(op.f('ix_water_intake_user_id'), table_name='water_intake')
op.drop_index(op.f('ix_water_intake_entry_date'), table_name='water_intake')
op.drop_table('water_intake')
op.drop_index(op.f('ix_user_nutrition_entries_uuid'), table_name='user_nutrition_entries')
op.drop_index(op.f('ix_user_nutrition_entries_user_id'), table_name='user_nutrition_entries')
op.drop_index(op.f('ix_user_nutrition_entries_entry_date'), table_name='user_nutrition_entries')
op.drop_table('user_nutrition_entries')
op.drop_index(op.f('ix_food_items_uuid'), table_name='food_items')
op.drop_index(op.f('ix_food_items_name'), table_name='food_items')
op.drop_index(op.f('ix_food_items_fatsecret_id'), table_name='food_items')
op.drop_table('food_items')