Fix Alembic issue

This commit is contained in:
Urtzi Alfaro
2025-10-01 11:24:06 +02:00
parent 7cc4b957a5
commit 2eeebfc1e0
62 changed files with 6114 additions and 3676 deletions

View File

@@ -1,7 +1,6 @@
"""Alembic environment configuration for recipes service"""
import asyncio
import logging
import os
import sys
from logging.config import fileConfig
@@ -25,7 +24,7 @@ try:
from shared.database.base import Base
# Import all models to ensure they are registered with Base.metadata
from app.models import * # Import all models
from app.models import * # noqa: F401, F403
except ImportError as e:
print(f"Import error in migrations env.py: {e}")
@@ -35,12 +34,19 @@ except ImportError as e:
# this is the Alembic Config object
config = context.config
# Set database URL from environment variables or settings
# Try service-specific DATABASE_URL first, then fall back to generic
database_url = os.getenv('RECIPES_DATABASE_URL') or os.getenv('DATABASE_URL')
# Determine service name from file path
service_name = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
service_name_upper = service_name.upper().replace('-', '_')
# Set database URL from environment variables with multiple fallback strategies
database_url = (
os.getenv(f'{service_name_upper}_DATABASE_URL') or # Service-specific
os.getenv('DATABASE_URL') # Generic fallback
)
# If DATABASE_URL is not set, construct from individual components
if not database_url:
# Try generic PostgreSQL environment variables first
postgres_host = os.getenv('POSTGRES_HOST')
postgres_port = os.getenv('POSTGRES_PORT', '5432')
postgres_db = os.getenv('POSTGRES_DB')
@@ -50,11 +56,28 @@ if not database_url:
if all([postgres_host, postgres_db, postgres_user, postgres_password]):
database_url = f"postgresql+asyncpg://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
else:
# Fallback to settings
database_url = getattr(settings, 'DATABASE_URL', None)
# Try service-specific environment variables
db_host = os.getenv(f'{service_name_upper}_DB_HOST', f'{service_name}-db-service')
db_port = os.getenv(f'{service_name_upper}_DB_PORT', '5432')
db_name = os.getenv(f'{service_name_upper}_DB_NAME', f'{service_name.replace("-", "_")}_db')
db_user = os.getenv(f'{service_name_upper}_DB_USER', f'{service_name.replace("-", "_")}_user')
db_password = os.getenv(f'{service_name_upper}_DB_PASSWORD')
if database_url:
config.set_main_option("sqlalchemy.url", database_url)
if db_password:
database_url = f"postgresql+asyncpg://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
else:
# Final fallback: try to get from settings object
try:
database_url = getattr(settings, 'DATABASE_URL', None)
except Exception:
pass
if not database_url:
error_msg = f"ERROR: No database URL configured for {service_name} service"
print(error_msg)
raise Exception(error_msg)
config.set_main_option("sqlalchemy.url", database_url)
# Interpret the config file for Python logging
if config.config_file_name is not None:
@@ -63,6 +86,7 @@ if config.config_file_name is not None:
# Set target metadata
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = config.get_main_option("sqlalchemy.url")
@@ -78,7 +102,9 @@ def run_migrations_offline() -> None:
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
"""Execute migrations with the given connection."""
context.configure(
connection=connection,
target_metadata=target_metadata,
@@ -89,8 +115,9 @@ def do_run_migrations(connection: Connection) -> None:
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
"""Run migrations in 'online' mode."""
"""Run migrations in 'online' mode with async support."""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
@@ -102,10 +129,12 @@ async def run_async_migrations() -> None:
await connectable.dispose()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:

View File

@@ -1,83 +0,0 @@
"""Initial schema for recipes service
Revision ID: 0001
Revises:
Create Date: 2025-09-30 18:00:00.0000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '00001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('recipes',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('category', sa.String(100), nullable=True),
sa.Column('cuisine', sa.String(100), nullable=True),
sa.Column('difficulty_level', sa.String(50), nullable=True),
sa.Column('preparation_time', sa.Integer(), nullable=True),
sa.Column('cooking_time', sa.Integer(), nullable=True),
sa.Column('total_time', sa.Integer(), nullable=True),
sa.Column('servings', sa.Integer(), nullable=True),
sa.Column('calories_per_serving', sa.Integer(), nullable=True),
sa.Column('status', sa.String(50), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_recipes_tenant_id'), 'recipes', ['tenant_id'], unique=False)
op.create_index(op.f('ix_recipes_name'), 'recipes', ['name'], unique=False)
op.create_index(op.f('ix_recipes_category'), 'recipes', ['category'], unique=False)
op.create_index(op.f('ix_recipes_status'), 'recipes', ['status'], unique=False)
op.create_table('recipe_ingredients',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('recipe_id', sa.UUID(), nullable=False),
sa.Column('ingredient_name', sa.String(255), nullable=False),
sa.Column('quantity', sa.Float(), nullable=False),
sa.Column('unit', sa.String(50), nullable=False),
sa.Column('optional', sa.Boolean(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['recipe_id'], ['recipes.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_recipe_ingredients_recipe_id'), 'recipe_ingredients', ['recipe_id'], unique=False)
op.create_index(op.f('ix_recipe_ingredients_ingredient_name'), 'recipe_ingredients', ['ingredient_name'], unique=False)
op.create_table('recipe_steps',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('recipe_id', sa.UUID(), nullable=False),
sa.Column('step_number', sa.Integer(), nullable=False),
sa.Column('description', sa.Text(), nullable=False),
sa.Column('duration', sa.Integer(), nullable=True),
sa.Column('temperature', sa.Float(), nullable=True),
sa.ForeignKeyConstraint(['recipe_id'], ['recipes.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_recipe_steps_recipe_id'), 'recipe_steps', ['recipe_id'], unique=False)
op.create_index(op.f('ix_recipe_steps_step_number'), 'recipe_steps', ['step_number'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_recipe_steps_step_number'), table_name='recipe_steps')
op.drop_index(op.f('ix_recipe_steps_recipe_id'), table_name='recipe_steps')
op.drop_table('recipe_steps')
op.drop_index(op.f('ix_recipe_ingredients_ingredient_name'), table_name='recipe_ingredients')
op.drop_index(op.f('ix_recipe_ingredients_recipe_id'), table_name='recipe_ingredients')
op.drop_table('recipe_ingredients')
op.drop_index(op.f('ix_recipes_status'), table_name='recipes')
op.drop_index(op.f('ix_recipes_category'), table_name='recipes')
op.drop_index(op.f('ix_recipes_name'), table_name='recipes')
op.drop_index(op.f('ix_recipes_tenant_id'), table_name='recipes')
op.drop_table('recipes')

View File

@@ -0,0 +1,288 @@
"""initial_schema_20251001_1118
Revision ID: 3957346a472c
Revises:
Create Date: 2025-10-01 11:18:33.794800+02:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '3957346a472c'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('production_schedules',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('schedule_date', sa.DateTime(timezone=True), nullable=False),
sa.Column('schedule_name', sa.String(length=255), nullable=True),
sa.Column('total_planned_batches', sa.Integer(), nullable=False),
sa.Column('total_planned_items', sa.Float(), nullable=False),
sa.Column('estimated_production_hours', sa.Float(), nullable=True),
sa.Column('estimated_material_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('is_published', sa.Boolean(), nullable=True),
sa.Column('is_completed', sa.Boolean(), nullable=True),
sa.Column('completion_percentage', sa.Float(), nullable=True),
sa.Column('available_staff_hours', sa.Float(), nullable=True),
sa.Column('oven_capacity_hours', sa.Float(), nullable=True),
sa.Column('production_capacity_limit', sa.Float(), nullable=True),
sa.Column('schedule_notes', sa.Text(), nullable=True),
sa.Column('preparation_instructions', sa.Text(), nullable=True),
sa.Column('special_requirements', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('published_by', sa.UUID(), nullable=True),
sa.Column('published_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_production_schedules_completed', 'production_schedules', ['tenant_id', 'is_completed', 'schedule_date'], unique=False)
op.create_index('idx_production_schedules_published', 'production_schedules', ['tenant_id', 'is_published', 'schedule_date'], unique=False)
op.create_index('idx_production_schedules_tenant_date', 'production_schedules', ['tenant_id', 'schedule_date'], unique=False)
op.create_index(op.f('ix_production_schedules_schedule_date'), 'production_schedules', ['schedule_date'], unique=False)
op.create_index(op.f('ix_production_schedules_tenant_id'), 'production_schedules', ['tenant_id'], unique=False)
op.create_table('recipes',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('recipe_code', sa.String(length=100), nullable=True),
sa.Column('version', sa.String(length=20), nullable=False),
sa.Column('finished_product_id', sa.UUID(), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('category', sa.String(length=100), nullable=True),
sa.Column('cuisine_type', sa.String(length=100), nullable=True),
sa.Column('difficulty_level', sa.Integer(), nullable=False),
sa.Column('yield_quantity', sa.Float(), nullable=False),
sa.Column('yield_unit', sa.Enum('GRAMS', 'KILOGRAMS', 'MILLILITERS', 'LITERS', 'CUPS', 'TABLESPOONS', 'TEASPOONS', 'UNITS', 'PIECES', 'PERCENTAGE', name='measurementunit'), nullable=False),
sa.Column('prep_time_minutes', sa.Integer(), nullable=True),
sa.Column('cook_time_minutes', sa.Integer(), nullable=True),
sa.Column('total_time_minutes', sa.Integer(), nullable=True),
sa.Column('rest_time_minutes', sa.Integer(), nullable=True),
sa.Column('estimated_cost_per_unit', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('last_calculated_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('cost_calculation_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('target_margin_percentage', sa.Float(), nullable=True),
sa.Column('suggested_selling_price', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('instructions', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('preparation_notes', sa.Text(), nullable=True),
sa.Column('storage_instructions', sa.Text(), nullable=True),
sa.Column('quality_standards', sa.Text(), nullable=True),
sa.Column('serves_count', sa.Integer(), nullable=True),
sa.Column('nutritional_info', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('allergen_info', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('dietary_tags', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('batch_size_multiplier', sa.Float(), nullable=False),
sa.Column('minimum_batch_size', sa.Float(), nullable=True),
sa.Column('maximum_batch_size', sa.Float(), nullable=True),
sa.Column('optimal_production_temperature', sa.Float(), nullable=True),
sa.Column('optimal_humidity', sa.Float(), nullable=True),
sa.Column('quality_check_points', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('quality_check_configuration', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('common_issues', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('status', sa.Enum('DRAFT', 'ACTIVE', 'TESTING', 'ARCHIVED', 'DISCONTINUED', name='recipestatus'), nullable=False),
sa.Column('is_seasonal', sa.Boolean(), nullable=True),
sa.Column('season_start_month', sa.Integer(), nullable=True),
sa.Column('season_end_month', sa.Integer(), nullable=True),
sa.Column('is_signature_item', sa.Boolean(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_recipes_category', 'recipes', ['tenant_id', 'category', 'status'], unique=False)
op.create_index('idx_recipes_seasonal', 'recipes', ['tenant_id', 'is_seasonal', 'season_start_month', 'season_end_month'], unique=False)
op.create_index('idx_recipes_signature', 'recipes', ['tenant_id', 'is_signature_item', 'status'], unique=False)
op.create_index('idx_recipes_status', 'recipes', ['tenant_id', 'status'], unique=False)
op.create_index('idx_recipes_tenant_name', 'recipes', ['tenant_id', 'name'], unique=False)
op.create_index('idx_recipes_tenant_product', 'recipes', ['tenant_id', 'finished_product_id'], unique=False)
op.create_index(op.f('ix_recipes_category'), 'recipes', ['category'], unique=False)
op.create_index(op.f('ix_recipes_finished_product_id'), 'recipes', ['finished_product_id'], unique=False)
op.create_index(op.f('ix_recipes_name'), 'recipes', ['name'], unique=False)
op.create_index(op.f('ix_recipes_recipe_code'), 'recipes', ['recipe_code'], unique=False)
op.create_index(op.f('ix_recipes_status'), 'recipes', ['status'], unique=False)
op.create_index(op.f('ix_recipes_tenant_id'), 'recipes', ['tenant_id'], unique=False)
op.create_table('production_batches',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('recipe_id', sa.UUID(), nullable=False),
sa.Column('batch_number', sa.String(length=100), nullable=False),
sa.Column('production_date', sa.DateTime(timezone=True), nullable=False),
sa.Column('planned_start_time', sa.DateTime(timezone=True), nullable=True),
sa.Column('actual_start_time', sa.DateTime(timezone=True), nullable=True),
sa.Column('planned_end_time', sa.DateTime(timezone=True), nullable=True),
sa.Column('actual_end_time', sa.DateTime(timezone=True), nullable=True),
sa.Column('planned_quantity', sa.Float(), nullable=False),
sa.Column('actual_quantity', sa.Float(), nullable=True),
sa.Column('yield_percentage', sa.Float(), nullable=True),
sa.Column('batch_size_multiplier', sa.Float(), nullable=False),
sa.Column('status', sa.Enum('PLANNED', 'IN_PROGRESS', 'COMPLETED', 'FAILED', 'CANCELLED', name='productionstatus'), nullable=False),
sa.Column('priority', sa.Enum('LOW', 'NORMAL', 'HIGH', 'URGENT', name='productionpriority'), nullable=False),
sa.Column('assigned_staff', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('production_notes', sa.Text(), nullable=True),
sa.Column('quality_score', sa.Float(), nullable=True),
sa.Column('quality_notes', sa.Text(), nullable=True),
sa.Column('defect_rate', sa.Float(), nullable=True),
sa.Column('rework_required', sa.Boolean(), nullable=True),
sa.Column('planned_material_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('actual_material_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('labor_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('overhead_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('total_production_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('cost_per_unit', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('production_temperature', sa.Float(), nullable=True),
sa.Column('production_humidity', sa.Float(), nullable=True),
sa.Column('oven_temperature', sa.Float(), nullable=True),
sa.Column('baking_time_minutes', sa.Integer(), nullable=True),
sa.Column('waste_quantity', sa.Float(), nullable=False),
sa.Column('waste_reason', sa.String(length=255), nullable=True),
sa.Column('efficiency_percentage', sa.Float(), nullable=True),
sa.Column('customer_order_reference', sa.String(length=100), nullable=True),
sa.Column('pre_order_quantity', sa.Float(), nullable=True),
sa.Column('shelf_quantity', sa.Float(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('completed_by', sa.UUID(), nullable=True),
sa.ForeignKeyConstraint(['recipe_id'], ['recipes.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_production_batches_batch_number', 'production_batches', ['tenant_id', 'batch_number'], unique=False)
op.create_index('idx_production_batches_priority', 'production_batches', ['tenant_id', 'priority', 'planned_start_time'], unique=False)
op.create_index('idx_production_batches_recipe', 'production_batches', ['recipe_id', 'production_date'], unique=False)
op.create_index('idx_production_batches_status', 'production_batches', ['tenant_id', 'status', 'production_date'], unique=False)
op.create_index('idx_production_batches_tenant_date', 'production_batches', ['tenant_id', 'production_date'], unique=False)
op.create_index(op.f('ix_production_batches_batch_number'), 'production_batches', ['batch_number'], unique=False)
op.create_index(op.f('ix_production_batches_production_date'), 'production_batches', ['production_date'], unique=False)
op.create_index(op.f('ix_production_batches_recipe_id'), 'production_batches', ['recipe_id'], unique=False)
op.create_index(op.f('ix_production_batches_status'), 'production_batches', ['status'], unique=False)
op.create_index(op.f('ix_production_batches_tenant_id'), 'production_batches', ['tenant_id'], unique=False)
op.create_table('recipe_ingredients',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('recipe_id', sa.UUID(), nullable=False),
sa.Column('ingredient_id', sa.UUID(), nullable=False),
sa.Column('quantity', sa.Float(), nullable=False),
sa.Column('unit', sa.Enum('GRAMS', 'KILOGRAMS', 'MILLILITERS', 'LITERS', 'CUPS', 'TABLESPOONS', 'TEASPOONS', 'UNITS', 'PIECES', 'PERCENTAGE', name='measurementunit'), nullable=False),
sa.Column('quantity_in_base_unit', sa.Float(), nullable=True),
sa.Column('alternative_quantity', sa.Float(), nullable=True),
sa.Column('alternative_unit', sa.Enum('GRAMS', 'KILOGRAMS', 'MILLILITERS', 'LITERS', 'CUPS', 'TABLESPOONS', 'TEASPOONS', 'UNITS', 'PIECES', 'PERCENTAGE', name='measurementunit'), nullable=True),
sa.Column('preparation_method', sa.String(length=255), nullable=True),
sa.Column('ingredient_notes', sa.Text(), nullable=True),
sa.Column('is_optional', sa.Boolean(), nullable=True),
sa.Column('ingredient_order', sa.Integer(), nullable=False),
sa.Column('ingredient_group', sa.String(length=100), nullable=True),
sa.Column('substitution_options', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('substitution_ratio', sa.Float(), nullable=True),
sa.Column('unit_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('total_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('cost_updated_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['recipe_id'], ['recipes.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_recipe_ingredients_group', 'recipe_ingredients', ['recipe_id', 'ingredient_group', 'ingredient_order'], unique=False)
op.create_index('idx_recipe_ingredients_ingredient', 'recipe_ingredients', ['ingredient_id'], unique=False)
op.create_index('idx_recipe_ingredients_recipe', 'recipe_ingredients', ['recipe_id', 'ingredient_order'], unique=False)
op.create_index('idx_recipe_ingredients_tenant', 'recipe_ingredients', ['tenant_id', 'recipe_id'], unique=False)
op.create_index(op.f('ix_recipe_ingredients_ingredient_id'), 'recipe_ingredients', ['ingredient_id'], unique=False)
op.create_index(op.f('ix_recipe_ingredients_recipe_id'), 'recipe_ingredients', ['recipe_id'], unique=False)
op.create_index(op.f('ix_recipe_ingredients_tenant_id'), 'recipe_ingredients', ['tenant_id'], unique=False)
op.create_table('production_ingredient_consumption',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('production_batch_id', sa.UUID(), nullable=False),
sa.Column('recipe_ingredient_id', sa.UUID(), nullable=False),
sa.Column('ingredient_id', sa.UUID(), nullable=False),
sa.Column('stock_id', sa.UUID(), nullable=True),
sa.Column('planned_quantity', sa.Float(), nullable=False),
sa.Column('actual_quantity', sa.Float(), nullable=False),
sa.Column('unit', sa.Enum('GRAMS', 'KILOGRAMS', 'MILLILITERS', 'LITERS', 'CUPS', 'TABLESPOONS', 'TEASPOONS', 'UNITS', 'PIECES', 'PERCENTAGE', name='measurementunit'), nullable=False),
sa.Column('variance_quantity', sa.Float(), nullable=True),
sa.Column('variance_percentage', sa.Float(), nullable=True),
sa.Column('unit_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('total_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('consumption_time', sa.DateTime(timezone=True), nullable=False),
sa.Column('consumption_notes', sa.Text(), nullable=True),
sa.Column('staff_member', sa.UUID(), nullable=True),
sa.Column('ingredient_condition', sa.String(length=50), nullable=True),
sa.Column('quality_impact', sa.String(length=255), nullable=True),
sa.Column('substitution_used', sa.Boolean(), nullable=True),
sa.Column('substitution_details', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['production_batch_id'], ['production_batches.id'], ),
sa.ForeignKeyConstraint(['recipe_ingredient_id'], ['recipe_ingredients.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index('idx_consumption_batch', 'production_ingredient_consumption', ['production_batch_id'], unique=False)
op.create_index('idx_consumption_ingredient', 'production_ingredient_consumption', ['ingredient_id', 'consumption_time'], unique=False)
op.create_index('idx_consumption_recipe_ingredient', 'production_ingredient_consumption', ['recipe_ingredient_id'], unique=False)
op.create_index('idx_consumption_stock', 'production_ingredient_consumption', ['stock_id'], unique=False)
op.create_index('idx_consumption_tenant', 'production_ingredient_consumption', ['tenant_id', 'consumption_time'], unique=False)
op.create_index(op.f('ix_production_ingredient_consumption_ingredient_id'), 'production_ingredient_consumption', ['ingredient_id'], unique=False)
op.create_index(op.f('ix_production_ingredient_consumption_production_batch_id'), 'production_ingredient_consumption', ['production_batch_id'], unique=False)
op.create_index(op.f('ix_production_ingredient_consumption_recipe_ingredient_id'), 'production_ingredient_consumption', ['recipe_ingredient_id'], unique=False)
op.create_index(op.f('ix_production_ingredient_consumption_stock_id'), 'production_ingredient_consumption', ['stock_id'], unique=False)
op.create_index(op.f('ix_production_ingredient_consumption_tenant_id'), 'production_ingredient_consumption', ['tenant_id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_production_ingredient_consumption_tenant_id'), table_name='production_ingredient_consumption')
op.drop_index(op.f('ix_production_ingredient_consumption_stock_id'), table_name='production_ingredient_consumption')
op.drop_index(op.f('ix_production_ingredient_consumption_recipe_ingredient_id'), table_name='production_ingredient_consumption')
op.drop_index(op.f('ix_production_ingredient_consumption_production_batch_id'), table_name='production_ingredient_consumption')
op.drop_index(op.f('ix_production_ingredient_consumption_ingredient_id'), table_name='production_ingredient_consumption')
op.drop_index('idx_consumption_tenant', table_name='production_ingredient_consumption')
op.drop_index('idx_consumption_stock', table_name='production_ingredient_consumption')
op.drop_index('idx_consumption_recipe_ingredient', table_name='production_ingredient_consumption')
op.drop_index('idx_consumption_ingredient', table_name='production_ingredient_consumption')
op.drop_index('idx_consumption_batch', table_name='production_ingredient_consumption')
op.drop_table('production_ingredient_consumption')
op.drop_index(op.f('ix_recipe_ingredients_tenant_id'), table_name='recipe_ingredients')
op.drop_index(op.f('ix_recipe_ingredients_recipe_id'), table_name='recipe_ingredients')
op.drop_index(op.f('ix_recipe_ingredients_ingredient_id'), table_name='recipe_ingredients')
op.drop_index('idx_recipe_ingredients_tenant', table_name='recipe_ingredients')
op.drop_index('idx_recipe_ingredients_recipe', table_name='recipe_ingredients')
op.drop_index('idx_recipe_ingredients_ingredient', table_name='recipe_ingredients')
op.drop_index('idx_recipe_ingredients_group', table_name='recipe_ingredients')
op.drop_table('recipe_ingredients')
op.drop_index(op.f('ix_production_batches_tenant_id'), table_name='production_batches')
op.drop_index(op.f('ix_production_batches_status'), table_name='production_batches')
op.drop_index(op.f('ix_production_batches_recipe_id'), table_name='production_batches')
op.drop_index(op.f('ix_production_batches_production_date'), table_name='production_batches')
op.drop_index(op.f('ix_production_batches_batch_number'), table_name='production_batches')
op.drop_index('idx_production_batches_tenant_date', table_name='production_batches')
op.drop_index('idx_production_batches_status', table_name='production_batches')
op.drop_index('idx_production_batches_recipe', table_name='production_batches')
op.drop_index('idx_production_batches_priority', table_name='production_batches')
op.drop_index('idx_production_batches_batch_number', table_name='production_batches')
op.drop_table('production_batches')
op.drop_index(op.f('ix_recipes_tenant_id'), table_name='recipes')
op.drop_index(op.f('ix_recipes_status'), table_name='recipes')
op.drop_index(op.f('ix_recipes_recipe_code'), table_name='recipes')
op.drop_index(op.f('ix_recipes_name'), table_name='recipes')
op.drop_index(op.f('ix_recipes_finished_product_id'), table_name='recipes')
op.drop_index(op.f('ix_recipes_category'), table_name='recipes')
op.drop_index('idx_recipes_tenant_product', table_name='recipes')
op.drop_index('idx_recipes_tenant_name', table_name='recipes')
op.drop_index('idx_recipes_status', table_name='recipes')
op.drop_index('idx_recipes_signature', table_name='recipes')
op.drop_index('idx_recipes_seasonal', table_name='recipes')
op.drop_index('idx_recipes_category', table_name='recipes')
op.drop_table('recipes')
op.drop_index(op.f('ix_production_schedules_tenant_id'), table_name='production_schedules')
op.drop_index(op.f('ix_production_schedules_schedule_date'), table_name='production_schedules')
op.drop_index('idx_production_schedules_tenant_date', table_name='production_schedules')
op.drop_index('idx_production_schedules_published', table_name='production_schedules')
op.drop_index('idx_production_schedules_completed', table_name='production_schedules')
op.drop_table('production_schedules')
# ### end Alembic commands ###