refactor: Unify database migrations into single initial schemas
Consolidated incremental migrations into single unified initial schema files for both procurement and production services. This simplifies database setup and eliminates migration chain complexity. Changes: - Procurement: Merged 3 migrations into 001_unified_initial_schema.py - Initial schema (20251015_1229) - Add supplier_price_list_id (20251030_0737) - Add JTBD reasoning fields (20251107) - Production: Merged 3 migrations into 001_unified_initial_schema.py - Initial schema (20251015_1231) - Add waste tracking fields (20251023_0900) - Add JTBD reasoning fields (20251107) All new fields (reasoning, consequence, reasoning_data, waste_defect_type, is_ai_assisted, supplier_price_list_id) are now included in the initial schemas from the start. Updated model files to use deferred() for reasoning fields to prevent breaking queries when running against existing databases.
This commit is contained in:
@@ -114,6 +114,35 @@ async def create_purchase_order(
|
||||
shipping_cost = Decimal(str(random.uniform(0, 20)))
|
||||
total = subtotal + tax_amount + shipping_cost
|
||||
|
||||
# Generate reasoning for JTBD dashboard (if columns exist after migration)
|
||||
days_until_delivery = (required_delivery - created_at).days
|
||||
reasoning_text = None
|
||||
reasoning_json = None
|
||||
consequence_text = None
|
||||
|
||||
try:
|
||||
# Try to set reasoning fields (will work after migration)
|
||||
if status == PurchaseOrderStatus.pending_approval:
|
||||
reasoning_text = f"Low stock detected for {supplier.name} items. Current inventory projected to run out in {days_until_delivery + 2} days."
|
||||
consequence_text = f"Stock-out risk in {days_until_delivery + 2} days if not approved. Production may be impacted."
|
||||
reasoning_json = {
|
||||
"trigger": "low_stock",
|
||||
"urgency_score": 75 if days_until_delivery < 5 else 50,
|
||||
"days_remaining": days_until_delivery + 2,
|
||||
"supplier_trust_score": supplier.trust_score
|
||||
}
|
||||
elif auto_approved:
|
||||
reasoning_text = f"Auto-approved based on supplier trust score ({supplier.trust_score:.0%}) and amount within threshold (€{subtotal:.2f})."
|
||||
reasoning_json = {
|
||||
"trigger": "auto_approval",
|
||||
"trust_score": supplier.trust_score,
|
||||
"amount": float(subtotal),
|
||||
"threshold": 500.0
|
||||
}
|
||||
except Exception:
|
||||
# Columns don't exist yet, that's ok
|
||||
pass
|
||||
|
||||
# Create PO
|
||||
po = PurchaseOrder(
|
||||
id=uuid.uuid4(),
|
||||
@@ -136,6 +165,15 @@ async def create_purchase_order(
|
||||
updated_by=SYSTEM_USER_ID
|
||||
)
|
||||
|
||||
# Set reasoning fields if they exist (after migration)
|
||||
if reasoning_text:
|
||||
try:
|
||||
po.reasoning = reasoning_text
|
||||
po.consequence = consequence_text
|
||||
po.reasoning_data = reasoning_json
|
||||
except Exception:
|
||||
pass # Columns don't exist yet
|
||||
|
||||
# Set approval data if approved
|
||||
if status in [PurchaseOrderStatus.approved, PurchaseOrderStatus.sent_to_supplier,
|
||||
PurchaseOrderStatus.confirmed, PurchaseOrderStatus.completed]:
|
||||
|
||||
Reference in New Issue
Block a user