#!/usr/bin/env python3 """ Script to create workflow tables in the database This adds the document workflow system tables to an existing database """ import sys import os # Add the project root to Python path sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from sqlalchemy import text from app.database.base import engine from app.models.document_workflows import ( DocumentWorkflow, WorkflowAction, WorkflowExecution, EventLog, WorkflowTemplate, WorkflowSchedule ) from app.models.deadlines import ( Deadline, DeadlineReminder, DeadlineTemplate, DeadlineHistory, CourtCalendar ) from app.models.base import BaseModel def table_exists(engine, table_name: str) -> bool: """Check if a table exists in the database""" with engine.begin() as conn: try: result = conn.execute(text(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}'")) return result.fetchone() is not None except Exception: return False def create_workflow_tables(): """Create workflow and deadline tables if they don't exist""" print("šŸš€ Creating Workflow and Deadline Tables for Delphi Database") print("=" * 60) # List of workflow and deadline table models all_tables = [ # Workflow tables ("document_workflows", DocumentWorkflow), ("workflow_actions", WorkflowAction), ("workflow_executions", WorkflowExecution), ("event_log", EventLog), ("workflow_templates", WorkflowTemplate), ("workflow_schedules", WorkflowSchedule), # Deadline tables ("deadlines", Deadline), ("deadline_reminders", DeadlineReminder), ("deadline_templates", DeadlineTemplate), ("deadline_history", DeadlineHistory), ("court_calendar", CourtCalendar), ] existing_tables = [] new_tables = [] # Check which tables already exist for table_name, table_model in all_tables: if table_exists(engine, table_name): existing_tables.append(table_name) print(f"āœ… Table '{table_name}' already exists") else: new_tables.append((table_name, table_model)) print(f"šŸ“ Table '{table_name}' needs to be created") if not new_tables: print("\nšŸŽ‰ All workflow and deadline tables already exist!") return True print(f"\nšŸ”Ø Creating {len(new_tables)} new tables...") try: # Create the new tables for table_name, table_model in new_tables: print(f" Creating {table_name}...") table_model.__table__.create(engine, checkfirst=True) print(f" āœ… Created {table_name}") print(f"\nšŸŽ‰ Successfully created {len(new_tables)} workflow and deadline tables!") print("\nWorkflow and deadline systems are now ready to use.") return True except Exception as e: print(f"\nāŒ Error creating workflow tables: {str(e)}") return False def main(): """Main function""" success = create_workflow_tables() if success: print("\n✨ Next steps:") print("1. Run 'python3 scripts/setup_example_workflows.py' to create example workflows") print("2. Test the workflows with 'python3 scripts/test_workflows.py'") print("3. Configure email settings for deadline reminders") else: print("\nšŸ”§ Please check the error messages above and try again.") return success if __name__ == "__main__": success = main() sys.exit(0 if success else 1)