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

@@ -9,13 +9,55 @@ from .customer import Customer, CustomerContact
from .order import CustomerOrder, OrderItem, OrderStatusHistory
from .procurement import ProcurementPlan, ProcurementRequirement
# Import enums
from .enums import (
CustomerType,
DeliveryMethod,
PaymentTerms,
PaymentMethod,
PaymentStatus,
CustomerSegment,
SalesChannel,
BusinessModel,
OrderType,
OrderSource,
OrderStatus,
DeliveryStatus,
ProcurementPlanType,
ProcurementStrategy,
PlanStatus,
PriorityLevel,
RequirementStatus,
RiskLevel,
)
# List all models for easier access
__all__ = [
# Models
"Customer",
"CustomerContact",
"CustomerContact",
"CustomerOrder",
"OrderItem",
"OrderStatusHistory",
"ProcurementPlan",
"ProcurementRequirement"
"ProcurementRequirement",
# Enums
"CustomerType",
"DeliveryMethod",
"PaymentTerms",
"PaymentMethod",
"PaymentStatus",
"CustomerSegment",
"SalesChannel",
"BusinessModel",
"OrderType",
"OrderSource",
"OrderStatus",
"DeliveryStatus",
"ProcurementPlanType",
"ProcurementStrategy",
"PlanStatus",
"PriorityLevel",
"RequirementStatus",
"RiskLevel",
]

View File

@@ -14,7 +14,7 @@ from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base
from shared.database.base import Base
class Customer(Base):

View File

@@ -14,7 +14,7 @@ from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base
from shared.database.base import Base
class CustomerOrder(Base):

View File

@@ -14,7 +14,7 @@ from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base
from shared.database.base import Base
class ProcurementPlan(Base):

View File

@@ -1,7 +1,6 @@
"""Alembic environment configuration for orders 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('ORDERS_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,396 +0,0 @@
"""Initial schema for orders service
Revision ID: 000001
Revises:
Create Date: 2025-09-30 18:00:00.000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create customers table
op.create_table('customers',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('customer_code', sa.String(50), nullable=False),
sa.Column('name', sa.String(200), nullable=False),
sa.Column('business_name', sa.String(200), nullable=True),
sa.Column('customer_type', sa.String(50), nullable=True),
sa.Column('email', sa.String(255), nullable=True),
sa.Column('phone', sa.String(50), nullable=True),
sa.Column('address_line1', sa.String(255), nullable=True),
sa.Column('address_line2', sa.String(255), nullable=True),
sa.Column('city', sa.String(100), nullable=True),
sa.Column('state', sa.String(100), nullable=True),
sa.Column('postal_code', sa.String(20), nullable=True),
sa.Column('country', sa.String(100), nullable=True),
sa.Column('tax_id', sa.String(50), nullable=True),
sa.Column('business_license', sa.String(100), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('preferred_delivery_method', sa.String(50), nullable=True),
sa.Column('payment_terms', sa.String(50), nullable=True),
sa.Column('credit_limit', sa.Numeric(10, 2), nullable=True),
sa.Column('discount_percentage', sa.Numeric(5, 2), nullable=True),
sa.Column('customer_segment', sa.String(50), nullable=True),
sa.Column('priority_level', sa.String(20), nullable=True),
sa.Column('special_instructions', sa.Text(), nullable=True),
sa.Column('delivery_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('product_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('total_orders', sa.Integer(), nullable=True),
sa.Column('total_spent', sa.Numeric(12, 2), nullable=True),
sa.Column('average_order_value', sa.Numeric(10, 2), nullable=True),
sa.Column('last_order_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_customers_tenant_id'), 'customers', ['tenant_id'], unique=False)
op.create_index(op.f('ix_customers_customer_code'), 'customers', ['customer_code'], unique=False)
op.create_index(op.f('ix_customers_name'), 'customers', ['name'], unique=False)
op.create_index(op.f('ix_customers_email'), 'customers', ['email'], unique=False)
op.create_index(op.f('ix_customers_phone'), 'customers', ['phone'], unique=False)
# Create customer_contacts table
op.create_table('customer_contacts',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('customer_id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(200), nullable=False),
sa.Column('title', sa.String(100), nullable=True),
sa.Column('department', sa.String(10), nullable=True),
sa.Column('email', sa.String(255), nullable=True),
sa.Column('phone', sa.String(50), nullable=True),
sa.Column('mobile', sa.String(50), nullable=True),
sa.Column('is_primary', sa.Boolean(), nullable=True),
sa.Column('contact_for_orders', sa.Boolean(), nullable=True),
sa.Column('contact_for_delivery', sa.Boolean(), nullable=True),
sa.Column('contact_for_billing', sa.Boolean(), nullable=True),
sa.Column('contact_for_support', sa.Boolean(), nullable=True),
sa.Column('preferred_contact_method', sa.String(50), nullable=True),
sa.Column('contact_time_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_customer_contacts_customer_id'), 'customer_contacts', ['customer_id'], unique=False)
op.create_index(op.f('ix_customer_contacts_name'), 'customer_contacts', ['name'], unique=False)
op.create_index(op.f('ix_customer_contacts_email'), 'customer_contacts', ['email'], unique=False)
# Create customer_orders table
op.create_table('customer_orders',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('order_number', sa.String(50), nullable=False),
sa.Column('customer_id', sa.UUID(), nullable=False),
sa.Column('status', sa.String(50), nullable=True),
sa.Column('order_type', sa.String(50), nullable=True),
sa.Column('priority', sa.String(20), nullable=True),
sa.Column('order_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('requested_delivery_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('confirmed_delivery_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('actual_delivery_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('delivery_method', sa.String(50), nullable=True),
sa.Column('delivery_address', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('delivery_instructions', sa.Text(), nullable=True),
sa.Column('delivery_window_start', sa.DateTime(timezone=True), nullable=True),
sa.Column('delivery_window_end', sa.DateTime(timezone=True), nullable=True),
sa.Column('subtotal', sa.Numeric(10, 2), nullable=True),
sa.Column('discount_amount', sa.Numeric(10, 2), nullable=True),
sa.Column('discount_percentage', sa.Numeric(5, 2), nullable=True),
sa.Column('tax_amount', sa.Numeric(10, 2), nullable=True),
sa.Column('delivery_fee', sa.Numeric(10, 2), nullable=True),
sa.Column('total_amount', sa.Numeric(10, 2), nullable=True),
sa.Column('payment_status', sa.String(50), nullable=True),
sa.Column('payment_method', sa.String(50), nullable=True),
sa.Column('payment_terms', sa.String(50), nullable=True),
sa.Column('payment_due_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('special_instructions', sa.Text(), nullable=True),
sa.Column('custom_requirements', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('allergen_warnings', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('business_model', sa.String(50), nullable=True),
sa.Column('estimated_business_model', sa.String(50), nullable=True),
sa.Column('order_source', sa.String(50), nullable=True),
sa.Column('sales_channel', sa.String(50), nullable=True),
sa.Column('order_origin', sa.String(100), nullable=True),
sa.Column('production_batch_id', sa.UUID(), nullable=True),
sa.Column('fulfillment_location', sa.String(100), nullable=True),
sa.Column('estimated_preparation_time', sa.Integer(), nullable=True),
sa.Column('actual_preparation_time', sa.Integer(), nullable=True),
sa.Column('customer_notified_confirmed', sa.Boolean(), nullable=True),
sa.Column('customer_notified_ready', sa.Boolean(), nullable=True),
sa.Column('customer_notified_delivered', sa.Boolean(), nullable=True),
sa.Column('communication_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('quality_score', sa.Numeric(3, 1), nullable=True),
sa.Column('customer_rating', sa.Integer(), nullable=True),
sa.Column('customer_feedback', sa.Text(), nullable=True),
sa.Column('cancellation_reason', sa.String(200), nullable=True),
sa.Column('cancelled_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('cancelled_by', sa.UUID(), nullable=True),
sa.Column('refund_amount', sa.Numeric(10, 2), nullable=True),
sa.Column('refund_processed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.Column('order_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('order_number')
)
op.create_index(op.f('ix_customer_orders_tenant_id'), 'customer_orders', ['tenant_id'], unique=False)
op.create_index(op.f('ix_customer_orders_customer_id'), 'customer_orders', ['customer_id'], unique=False)
op.create_index(op.f('ix_customer_orders_order_number'), 'customer_orders', ['order_number'], unique=False)
op.create_index(op.f('ix_customer_orders_status'), 'customer_orders', ['status'], unique=False)
op.create_index(op.f('ix_customer_orders_order_date'), 'customer_orders', ['order_date'], unique=False)
# Create order_items table
op.create_table('order_items',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('order_id', sa.UUID(), nullable=False),
sa.Column('product_id', sa.UUID(), nullable=False),
sa.Column('product_name', sa.String(200), nullable=False),
sa.Column('product_sku', sa.String(100), nullable=True),
sa.Column('product_category', sa.String(100), nullable=True),
sa.Column('quantity', sa.Numeric(10, 3), nullable=True),
sa.Column('unit_of_measure', sa.String(50), nullable=True),
sa.Column('weight', sa.Numeric(10, 3), nullable=True),
sa.Column('unit_price', sa.Numeric(10, 2), nullable=True),
sa.Column('line_discount', sa.Numeric(10, 2), nullable=True),
sa.Column('line_total', sa.Numeric(10, 2), nullable=True),
sa.Column('product_specifications', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('customization_details', sa.Text(), nullable=True),
sa.Column('special_instructions', sa.Text(), nullable=True),
sa.Column('recipe_id', sa.UUID(), nullable=True),
sa.Column('production_requirements', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('estimated_production_time', sa.Integer(), nullable=True),
sa.Column('status', sa.String(50), nullable=True),
sa.Column('production_started_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('production_completed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('quality_checked', sa.Boolean(), nullable=True),
sa.Column('quality_score', sa.Numeric(3, 1), nullable=True),
sa.Column('ingredient_cost', sa.Numeric(10, 2), nullable=True),
sa.Column('labor_cost', sa.Numeric(10, 2), nullable=True),
sa.Column('overhead_cost', sa.Numeric(10, 2), nullable=True),
sa.Column('total_cost', sa.Numeric(10, 2), nullable=True),
sa.Column('margin', sa.Numeric(10, 2), nullable=True),
sa.Column('reserved_inventory', sa.Boolean(), nullable=True),
sa.Column('inventory_allocated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('customer_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['order_id'], ['customer_orders.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_order_items_order_id'), 'order_items', ['order_id'], unique=False)
op.create_index(op.f('ix_order_items_product_id'), 'order_items', ['product_id'], unique=False)
op.create_index(op.f('ix_order_items_product_name'), 'order_items', ['product_name'], unique=False)
# Create order_status_history table
op.create_table('order_status_history',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('order_id', sa.UUID(), nullable=False),
sa.Column('from_status', sa.String(50), nullable=True),
sa.Column('to_status', sa.String(50), nullable=False),
sa.Column('change_reason', sa.String(200), nullable=True),
sa.Column('event_type', sa.String(50), nullable=True),
sa.Column('event_description', sa.Text(), nullable=True),
sa.Column('event_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('changed_by', sa.UUID(), nullable=True),
sa.Column('change_source', sa.String(50), nullable=True),
sa.Column('changed_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('customer_notified', sa.Boolean(), nullable=True),
sa.Column('notification_method', sa.String(50), nullable=True),
sa.Column('notification_sent_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['order_id'], ['customer_orders.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_order_status_history_order_id'), 'order_status_history', ['order_id'], unique=False)
op.create_index(op.f('ix_order_status_history_to_status'), 'order_status_history', ['to_status'], unique=False)
op.create_index(op.f('ix_order_status_history_event_type'), 'order_status_history', ['event_type'], unique=False)
op.create_index(op.f('ix_order_status_history_changed_at'), 'order_status_history', ['changed_at'], unique=False)
# Create procurement_plans table
op.create_table('procurement_plans',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('plan_number', sa.String(50), nullable=False),
sa.Column('plan_date', sa.Date(), nullable=True),
sa.Column('plan_period_start', sa.Date(), nullable=True),
sa.Column('plan_period_end', sa.Date(), nullable=True),
sa.Column('planning_horizon_days', sa.Integer(), nullable=True),
sa.Column('status', sa.String(50), nullable=True),
sa.Column('plan_type', sa.String(50), nullable=True),
sa.Column('priority', sa.String(20), nullable=True),
sa.Column('business_model', sa.String(50), nullable=True),
sa.Column('procurement_strategy', sa.String(50), nullable=True),
sa.Column('total_requirements', sa.Integer(), nullable=True),
sa.Column('total_estimated_cost', sa.Numeric(12, 2), nullable=True),
sa.Column('total_approved_cost', sa.Numeric(12, 2), nullable=True),
sa.Column('cost_variance', sa.Numeric(12, 2), nullable=True),
sa.Column('total_demand_orders', sa.Integer(), nullable=True),
sa.Column('total_demand_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('total_production_requirements', sa.Numeric(12, 3), nullable=True),
sa.Column('safety_stock_buffer', sa.Numeric(5, 2), nullable=True),
sa.Column('primary_suppliers_count', sa.Integer(), nullable=True),
sa.Column('backup_suppliers_count', sa.Integer(), nullable=True),
sa.Column('supplier_diversification_score', sa.Numeric(3, 1), nullable=True),
sa.Column('supply_risk_level', sa.String(20), nullable=True),
sa.Column('demand_forecast_confidence', sa.Numeric(3, 1), nullable=True),
sa.Column('seasonality_adjustment', sa.Numeric(5, 2), nullable=True),
sa.Column('fulfillment_rate', sa.Numeric(5, 2), nullable=True),
sa.Column('on_time_delivery_rate', sa.Numeric(5, 2), nullable=True),
sa.Column('cost_accuracy', sa.Numeric(5, 2), nullable=True),
sa.Column('quality_score', sa.Numeric(3, 1), nullable=True),
sa.Column('source_orders', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('production_schedules', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('inventory_snapshots', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('stakeholder_notifications', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('approval_workflow', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('special_requirements', sa.Text(), nullable=True),
sa.Column('seasonal_adjustments', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('emergency_provisions', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('erp_reference', sa.String(100), nullable=True),
sa.Column('supplier_portal_reference', sa.String(100), nullable=True),
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('approved_by', sa.UUID(), nullable=True),
sa.Column('execution_started_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('execution_completed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.Column('plan_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('plan_number')
)
op.create_index(op.f('ix_procurement_plans_tenant_id'), 'procurement_plans', ['tenant_id'], unique=False)
op.create_index(op.f('ix_procurement_plans_plan_number'), 'procurement_plans', ['plan_number'], unique=False)
op.create_index(op.f('ix_procurement_plans_status'), 'procurement_plans', ['status'], unique=False)
op.create_index(op.f('ix_procurement_plans_plan_date'), 'procurement_plans', ['plan_date'], unique=False)
# Create procurement_requirements table
op.create_table('procurement_requirements',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('plan_id', sa.UUID(), nullable=False),
sa.Column('requirement_number', sa.String(50), nullable=False),
sa.Column('product_id', sa.UUID(), nullable=False),
sa.Column('product_name', sa.String(200), nullable=False),
sa.Column('product_sku', sa.String(100), nullable=True),
sa.Column('product_category', sa.String(100), nullable=True),
sa.Column('product_type', sa.String(50), nullable=True),
sa.Column('required_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('unit_of_measure', sa.String(50), nullable=True),
sa.Column('safety_stock_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('total_quantity_needed', sa.Numeric(12, 3), nullable=True),
sa.Column('current_stock_level', sa.Numeric(12, 3), nullable=True),
sa.Column('reserved_stock', sa.Numeric(12, 3), nullable=True),
sa.Column('available_stock', sa.Numeric(12, 3), nullable=True),
sa.Column('net_requirement', sa.Numeric(12, 3), nullable=True),
sa.Column('order_demand', sa.Numeric(12, 3), nullable=True),
sa.Column('production_demand', sa.Numeric(12, 3), nullable=True),
sa.Column('forecast_demand', sa.Numeric(12, 3), nullable=True),
sa.Column('buffer_demand', sa.Numeric(12, 3), nullable=True),
sa.Column('preferred_supplier_id', sa.UUID(), nullable=True),
sa.Column('backup_supplier_id', sa.UUID(), nullable=True),
sa.Column('supplier_name', sa.String(200), nullable=True),
sa.Column('supplier_lead_time_days', sa.Integer(), nullable=True),
sa.Column('minimum_order_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('estimated_unit_cost', sa.Numeric(10, 4), nullable=True),
sa.Column('estimated_total_cost', sa.Numeric(12, 2), nullable=True),
sa.Column('last_purchase_cost', sa.Numeric(10, 4), nullable=True),
sa.Column('cost_variance', sa.Numeric(10, 2), nullable=True),
sa.Column('required_by_date', sa.Date(), nullable=True),
sa.Column('lead_time_buffer_days', sa.Integer(), nullable=True),
sa.Column('suggested_order_date', sa.Date(), nullable=True),
sa.Column('latest_order_date', sa.Date(), nullable=True),
sa.Column('quality_specifications', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('special_requirements', sa.Text(), nullable=True),
sa.Column('storage_requirements', sa.String(200), nullable=True),
sa.Column('shelf_life_days', sa.Integer(), nullable=True),
sa.Column('status', sa.String(50), nullable=True),
sa.Column('priority', sa.String(20), nullable=True),
sa.Column('risk_level', sa.String(20), nullable=True),
sa.Column('purchase_order_id', sa.UUID(), nullable=True),
sa.Column('purchase_order_number', sa.String(50), nullable=True),
sa.Column('ordered_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('ordered_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('expected_delivery_date', sa.Date(), nullable=True),
sa.Column('actual_delivery_date', sa.Date(), nullable=True),
sa.Column('received_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('delivery_status', sa.String(50), nullable=True),
sa.Column('fulfillment_rate', sa.Numeric(5, 2), nullable=True),
sa.Column('on_time_delivery', sa.Boolean(), nullable=True),
sa.Column('quality_rating', sa.Numeric(3, 1), nullable=True),
sa.Column('source_orders', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('source_production_batches', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('demand_analysis', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('approved_quantity', sa.Numeric(12, 3), nullable=True),
sa.Column('approved_cost', sa.Numeric(12, 2), nullable=True),
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('approved_by', sa.UUID(), nullable=True),
sa.Column('procurement_notes', sa.Text(), nullable=True),
sa.Column('supplier_communication', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('timezone(\'utc\', now())'), nullable=True),
sa.Column('requirement_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['plan_id'], ['procurement_plans.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_procurement_requirements_plan_id'), 'procurement_requirements', ['plan_id'], unique=False)
op.create_index(op.f('ix_procurement_requirements_requirement_number'), 'procurement_requirements', ['requirement_number'], unique=False)
op.create_index(op.f('ix_procurement_requirements_product_id'), 'procurement_requirements', ['product_id'], unique=False)
op.create_index(op.f('ix_procurement_requirements_status'), 'procurement_requirements', ['status'], unique=False)
op.create_index(op.f('ix_procurement_requirements_required_by_date'), 'procurement_requirements', ['required_by_date'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_procurement_requirements_required_by_date'), table_name='procurement_requirements')
op.drop_index(op.f('ix_procurement_requirements_status'), table_name='procurement_requirements')
op.drop_index(op.f('ix_procurement_requirements_product_id'), table_name='procurement_requirements')
op.drop_index(op.f('ix_procurement_requirements_requirement_number'), table_name='procurement_requirements')
op.drop_index(op.f('ix_procurement_requirements_plan_id'), table_name='procurement_requirements')
op.drop_table('procurement_requirements')
op.drop_index(op.f('ix_procurement_plans_plan_date'), table_name='procurement_plans')
op.drop_index(op.f('ix_procurement_plans_status'), table_name='procurement_plans')
op.drop_index(op.f('ix_procurement_plans_plan_number'), table_name='procurement_plans')
op.drop_index(op.f('ix_procurement_plans_tenant_id'), table_name='procurement_plans')
op.drop_table('procurement_plans')
op.drop_index(op.f('ix_order_status_history_changed_at'), table_name='order_status_history')
op.drop_index(op.f('ix_order_status_history_event_type'), table_name='order_status_history')
op.drop_index(op.f('ix_order_status_history_to_status'), table_name='order_status_history')
op.drop_index(op.f('ix_order_status_history_order_id'), table_name='order_status_history')
op.drop_table('order_status_history')
op.drop_index(op.f('ix_order_items_product_name'), table_name='order_items')
op.drop_index(op.f('ix_order_items_product_id'), table_name='order_items')
op.drop_index(op.f('ix_order_items_order_id'), table_name='order_items')
op.drop_table('order_items')
op.drop_index(op.f('ix_customer_orders_order_date'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_status'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_order_number'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_customer_id'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_tenant_id'), table_name='customer_orders')
op.drop_table('customer_orders')
op.drop_index(op.f('ix_customer_contacts_email'), table_name='customer_contacts')
op.drop_index(op.f('ix_customer_contacts_name'), table_name='customer_contacts')
op.drop_index(op.f('ix_customer_contacts_customer_id'), table_name='customer_contacts')
op.drop_table('customer_contacts')
op.drop_index(op.f('ix_customers_phone'), table_name='customers')
op.drop_index(op.f('ix_customers_email'), table_name='customers')
op.drop_index(op.f('ix_customers_name'), table_name='customers')
op.drop_index(op.f('ix_customers_customer_code'), table_name='customers')
op.drop_index(op.f('ix_customers_tenant_id'), table_name='customers')
op.drop_table('customers')

View File

@@ -0,0 +1,355 @@
"""initial_schema_20251001_1118
Revision ID: 07e130577d3f
Revises:
Create Date: 2025-10-01 11:18:52.812809+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 = '07e130577d3f'
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('customers',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('customer_code', sa.String(length=50), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('business_name', sa.String(length=200), nullable=True),
sa.Column('customer_type', sa.String(length=50), nullable=False),
sa.Column('email', sa.String(length=255), nullable=True),
sa.Column('phone', sa.String(length=50), nullable=True),
sa.Column('address_line1', sa.String(length=255), nullable=True),
sa.Column('address_line2', sa.String(length=255), nullable=True),
sa.Column('city', sa.String(length=100), nullable=True),
sa.Column('state', sa.String(length=100), nullable=True),
sa.Column('postal_code', sa.String(length=20), nullable=True),
sa.Column('country', sa.String(length=100), nullable=False),
sa.Column('tax_id', sa.String(length=50), nullable=True),
sa.Column('business_license', sa.String(length=100), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.Column('preferred_delivery_method', sa.String(length=50), nullable=False),
sa.Column('payment_terms', sa.String(length=50), nullable=False),
sa.Column('credit_limit', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('discount_percentage', sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column('customer_segment', sa.String(length=50), nullable=False),
sa.Column('priority_level', sa.String(length=20), nullable=False),
sa.Column('special_instructions', sa.Text(), nullable=True),
sa.Column('delivery_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('product_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('total_orders', sa.Integer(), nullable=False),
sa.Column('total_spent', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('average_order_value', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('last_order_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_customers_customer_code'), 'customers', ['customer_code'], unique=False)
op.create_index(op.f('ix_customers_tenant_id'), 'customers', ['tenant_id'], unique=False)
op.create_table('procurement_plans',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('plan_number', sa.String(length=50), nullable=False),
sa.Column('plan_date', sa.Date(), nullable=False),
sa.Column('plan_period_start', sa.Date(), nullable=False),
sa.Column('plan_period_end', sa.Date(), nullable=False),
sa.Column('planning_horizon_days', sa.Integer(), nullable=False),
sa.Column('status', sa.String(length=50), nullable=False),
sa.Column('plan_type', sa.String(length=50), nullable=False),
sa.Column('priority', sa.String(length=20), nullable=False),
sa.Column('business_model', sa.String(length=50), nullable=True),
sa.Column('procurement_strategy', sa.String(length=50), nullable=False),
sa.Column('total_requirements', sa.Integer(), nullable=False),
sa.Column('total_estimated_cost', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('total_approved_cost', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('cost_variance', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('total_demand_orders', sa.Integer(), nullable=False),
sa.Column('total_demand_quantity', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('total_production_requirements', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('safety_stock_buffer', sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column('primary_suppliers_count', sa.Integer(), nullable=False),
sa.Column('backup_suppliers_count', sa.Integer(), nullable=False),
sa.Column('supplier_diversification_score', sa.Numeric(precision=3, scale=1), nullable=True),
sa.Column('supply_risk_level', sa.String(length=20), nullable=False),
sa.Column('demand_forecast_confidence', sa.Numeric(precision=3, scale=1), nullable=True),
sa.Column('seasonality_adjustment', sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('approved_by', sa.UUID(), nullable=True),
sa.Column('execution_started_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('execution_completed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('fulfillment_rate', sa.Numeric(precision=5, scale=2), nullable=True),
sa.Column('on_time_delivery_rate', sa.Numeric(precision=5, scale=2), nullable=True),
sa.Column('cost_accuracy', sa.Numeric(precision=5, scale=2), nullable=True),
sa.Column('quality_score', sa.Numeric(precision=3, scale=1), nullable=True),
sa.Column('source_orders', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('production_schedules', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('inventory_snapshots', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('stakeholder_notifications', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('approval_workflow', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('special_requirements', sa.Text(), nullable=True),
sa.Column('seasonal_adjustments', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('emergency_provisions', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('erp_reference', sa.String(length=100), nullable=True),
sa.Column('supplier_portal_reference', sa.String(length=100), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.Column('plan_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_procurement_plans_plan_date'), 'procurement_plans', ['plan_date'], unique=False)
op.create_index(op.f('ix_procurement_plans_plan_number'), 'procurement_plans', ['plan_number'], unique=True)
op.create_index(op.f('ix_procurement_plans_status'), 'procurement_plans', ['status'], unique=False)
op.create_index(op.f('ix_procurement_plans_tenant_id'), 'procurement_plans', ['tenant_id'], unique=False)
op.create_table('customer_contacts',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('customer_id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('title', sa.String(length=100), nullable=True),
sa.Column('department', sa.String(length=100), nullable=True),
sa.Column('email', sa.String(length=255), nullable=True),
sa.Column('phone', sa.String(length=50), nullable=True),
sa.Column('mobile', sa.String(length=50), nullable=True),
sa.Column('is_primary', sa.Boolean(), nullable=False),
sa.Column('contact_for_orders', sa.Boolean(), nullable=False),
sa.Column('contact_for_delivery', sa.Boolean(), nullable=False),
sa.Column('contact_for_billing', sa.Boolean(), nullable=False),
sa.Column('contact_for_support', sa.Boolean(), nullable=False),
sa.Column('preferred_contact_method', sa.String(length=50), nullable=False),
sa.Column('contact_time_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_table('customer_orders',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('order_number', sa.String(length=50), nullable=False),
sa.Column('customer_id', sa.UUID(), nullable=False),
sa.Column('status', sa.String(length=50), nullable=False),
sa.Column('order_type', sa.String(length=50), nullable=False),
sa.Column('priority', sa.String(length=20), nullable=False),
sa.Column('order_date', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('requested_delivery_date', sa.DateTime(timezone=True), nullable=False),
sa.Column('confirmed_delivery_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('actual_delivery_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('delivery_method', sa.String(length=50), nullable=False),
sa.Column('delivery_address', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('delivery_instructions', sa.Text(), nullable=True),
sa.Column('delivery_window_start', sa.DateTime(timezone=True), nullable=True),
sa.Column('delivery_window_end', sa.DateTime(timezone=True), nullable=True),
sa.Column('subtotal', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('discount_amount', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('discount_percentage', sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column('tax_amount', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('delivery_fee', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('total_amount', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('payment_status', sa.String(length=50), nullable=False),
sa.Column('payment_method', sa.String(length=50), nullable=True),
sa.Column('payment_terms', sa.String(length=50), nullable=False),
sa.Column('payment_due_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('special_instructions', sa.Text(), nullable=True),
sa.Column('custom_requirements', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('allergen_warnings', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('business_model', sa.String(length=50), nullable=True),
sa.Column('estimated_business_model', sa.String(length=50), nullable=True),
sa.Column('order_source', sa.String(length=50), nullable=False),
sa.Column('sales_channel', sa.String(length=50), nullable=False),
sa.Column('order_origin', sa.String(length=100), nullable=True),
sa.Column('production_batch_id', sa.UUID(), nullable=True),
sa.Column('fulfillment_location', sa.String(length=100), nullable=True),
sa.Column('estimated_preparation_time', sa.Integer(), nullable=True),
sa.Column('actual_preparation_time', sa.Integer(), nullable=True),
sa.Column('customer_notified_confirmed', sa.Boolean(), nullable=False),
sa.Column('customer_notified_ready', sa.Boolean(), nullable=False),
sa.Column('customer_notified_delivered', sa.Boolean(), nullable=False),
sa.Column('communication_preferences', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('quality_score', sa.Numeric(precision=3, scale=1), nullable=True),
sa.Column('customer_rating', sa.Integer(), nullable=True),
sa.Column('customer_feedback', sa.Text(), nullable=True),
sa.Column('cancellation_reason', sa.String(length=200), nullable=True),
sa.Column('cancelled_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('cancelled_by', sa.UUID(), nullable=True),
sa.Column('refund_amount', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('refund_processed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('created_by', sa.UUID(), nullable=True),
sa.Column('updated_by', sa.UUID(), nullable=True),
sa.Column('order_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_customer_orders_customer_id'), 'customer_orders', ['customer_id'], unique=False)
op.create_index(op.f('ix_customer_orders_order_number'), 'customer_orders', ['order_number'], unique=True)
op.create_index(op.f('ix_customer_orders_status'), 'customer_orders', ['status'], unique=False)
op.create_index(op.f('ix_customer_orders_tenant_id'), 'customer_orders', ['tenant_id'], unique=False)
op.create_table('procurement_requirements',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('plan_id', sa.UUID(), nullable=False),
sa.Column('requirement_number', sa.String(length=50), nullable=False),
sa.Column('product_id', sa.UUID(), nullable=False),
sa.Column('product_name', sa.String(length=200), nullable=False),
sa.Column('product_sku', sa.String(length=100), nullable=True),
sa.Column('product_category', sa.String(length=100), nullable=True),
sa.Column('product_type', sa.String(length=50), nullable=False),
sa.Column('required_quantity', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('unit_of_measure', sa.String(length=50), nullable=False),
sa.Column('safety_stock_quantity', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('total_quantity_needed', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('current_stock_level', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('reserved_stock', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('available_stock', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('net_requirement', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('order_demand', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('production_demand', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('forecast_demand', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('buffer_demand', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('preferred_supplier_id', sa.UUID(), nullable=True),
sa.Column('backup_supplier_id', sa.UUID(), nullable=True),
sa.Column('supplier_name', sa.String(length=200), nullable=True),
sa.Column('supplier_lead_time_days', sa.Integer(), nullable=True),
sa.Column('minimum_order_quantity', sa.Numeric(precision=12, scale=3), nullable=True),
sa.Column('estimated_unit_cost', sa.Numeric(precision=10, scale=4), nullable=True),
sa.Column('estimated_total_cost', sa.Numeric(precision=12, scale=2), nullable=True),
sa.Column('last_purchase_cost', sa.Numeric(precision=10, scale=4), nullable=True),
sa.Column('cost_variance', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('required_by_date', sa.Date(), nullable=False),
sa.Column('lead_time_buffer_days', sa.Integer(), nullable=False),
sa.Column('suggested_order_date', sa.Date(), nullable=False),
sa.Column('latest_order_date', sa.Date(), nullable=False),
sa.Column('quality_specifications', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('special_requirements', sa.Text(), nullable=True),
sa.Column('storage_requirements', sa.String(length=200), nullable=True),
sa.Column('shelf_life_days', sa.Integer(), nullable=True),
sa.Column('status', sa.String(length=50), nullable=False),
sa.Column('priority', sa.String(length=20), nullable=False),
sa.Column('risk_level', sa.String(length=20), nullable=False),
sa.Column('purchase_order_id', sa.UUID(), nullable=True),
sa.Column('purchase_order_number', sa.String(length=50), nullable=True),
sa.Column('ordered_quantity', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('ordered_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('expected_delivery_date', sa.Date(), nullable=True),
sa.Column('actual_delivery_date', sa.Date(), nullable=True),
sa.Column('received_quantity', sa.Numeric(precision=12, scale=3), nullable=False),
sa.Column('delivery_status', sa.String(length=50), nullable=False),
sa.Column('fulfillment_rate', sa.Numeric(precision=5, scale=2), nullable=True),
sa.Column('on_time_delivery', sa.Boolean(), nullable=True),
sa.Column('quality_rating', sa.Numeric(precision=3, scale=1), nullable=True),
sa.Column('source_orders', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('source_production_batches', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('demand_analysis', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('approved_quantity', sa.Numeric(precision=12, scale=3), nullable=True),
sa.Column('approved_cost', sa.Numeric(precision=12, scale=2), nullable=True),
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('approved_by', sa.UUID(), nullable=True),
sa.Column('procurement_notes', sa.Text(), nullable=True),
sa.Column('supplier_communication', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('requirement_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['plan_id'], ['procurement_plans.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_procurement_requirements_product_id'), 'procurement_requirements', ['product_id'], unique=False)
op.create_index(op.f('ix_procurement_requirements_requirement_number'), 'procurement_requirements', ['requirement_number'], unique=False)
op.create_table('order_items',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('order_id', sa.UUID(), nullable=False),
sa.Column('product_id', sa.UUID(), nullable=False),
sa.Column('product_name', sa.String(length=200), nullable=False),
sa.Column('product_sku', sa.String(length=100), nullable=True),
sa.Column('product_category', sa.String(length=100), nullable=True),
sa.Column('quantity', sa.Numeric(precision=10, scale=3), nullable=False),
sa.Column('unit_of_measure', sa.String(length=50), nullable=False),
sa.Column('weight', sa.Numeric(precision=10, scale=3), nullable=True),
sa.Column('unit_price', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('line_discount', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('line_total', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('product_specifications', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('customization_details', sa.Text(), nullable=True),
sa.Column('special_instructions', sa.Text(), nullable=True),
sa.Column('recipe_id', sa.UUID(), nullable=True),
sa.Column('production_requirements', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('estimated_production_time', sa.Integer(), nullable=True),
sa.Column('status', sa.String(length=50), nullable=False),
sa.Column('production_started_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('production_completed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('quality_checked', sa.Boolean(), nullable=False),
sa.Column('quality_score', sa.Numeric(precision=3, scale=1), nullable=True),
sa.Column('ingredient_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_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('margin', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('reserved_inventory', sa.Boolean(), nullable=False),
sa.Column('inventory_allocated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('customer_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['order_id'], ['customer_orders.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_order_items_product_id'), 'order_items', ['product_id'], unique=False)
op.create_table('order_status_history',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('order_id', sa.UUID(), nullable=False),
sa.Column('from_status', sa.String(length=50), nullable=True),
sa.Column('to_status', sa.String(length=50), nullable=False),
sa.Column('change_reason', sa.String(length=200), nullable=True),
sa.Column('event_type', sa.String(length=50), nullable=False),
sa.Column('event_description', sa.Text(), nullable=True),
sa.Column('event_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('changed_by', sa.UUID(), nullable=True),
sa.Column('change_source', sa.String(length=50), nullable=False),
sa.Column('changed_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('customer_notified', sa.Boolean(), nullable=False),
sa.Column('notification_method', sa.String(length=50), nullable=True),
sa.Column('notification_sent_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['order_id'], ['customer_orders.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('order_status_history')
op.drop_index(op.f('ix_order_items_product_id'), table_name='order_items')
op.drop_table('order_items')
op.drop_index(op.f('ix_procurement_requirements_requirement_number'), table_name='procurement_requirements')
op.drop_index(op.f('ix_procurement_requirements_product_id'), table_name='procurement_requirements')
op.drop_table('procurement_requirements')
op.drop_index(op.f('ix_customer_orders_tenant_id'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_status'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_order_number'), table_name='customer_orders')
op.drop_index(op.f('ix_customer_orders_customer_id'), table_name='customer_orders')
op.drop_table('customer_orders')
op.drop_table('customer_contacts')
op.drop_index(op.f('ix_procurement_plans_tenant_id'), table_name='procurement_plans')
op.drop_index(op.f('ix_procurement_plans_status'), table_name='procurement_plans')
op.drop_index(op.f('ix_procurement_plans_plan_number'), table_name='procurement_plans')
op.drop_index(op.f('ix_procurement_plans_plan_date'), table_name='procurement_plans')
op.drop_table('procurement_plans')
op.drop_index(op.f('ix_customers_tenant_id'), table_name='customers')
op.drop_index(op.f('ix_customers_customer_code'), table_name='customers')
op.drop_table('customers')
# ### end Alembic commands ###