import mysql.connector from mysql.connector import pooling from contextlib import contextmanager from .config import settings connection_pool = None def init_db_pool(): global connection_pool connection_pool = pooling.MySQLConnectionPool( pool_name="meeting_pool", pool_size=5, host=settings.DB_HOST, port=settings.DB_PORT, user=settings.DB_USER, password=settings.DB_PASS, database=settings.DB_NAME, ) return connection_pool @contextmanager def get_db_connection(): conn = connection_pool.get_connection() try: yield conn finally: conn.close() @contextmanager def get_db_cursor(commit=False): with get_db_connection() as conn: cursor = conn.cursor(dictionary=True) try: yield cursor if commit: conn.commit() finally: cursor.close() def init_tables(): """Create all required tables if they don't exist.""" create_statements = [ """ CREATE TABLE IF NOT EXISTS meeting_users ( user_id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(100) UNIQUE NOT NULL, display_name VARCHAR(50), role ENUM('admin', 'user') DEFAULT 'user', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """, """ CREATE TABLE IF NOT EXISTS meeting_records ( meeting_id INT PRIMARY KEY AUTO_INCREMENT, uuid VARCHAR(64) UNIQUE, subject VARCHAR(200) NOT NULL, meeting_time DATETIME NOT NULL, location VARCHAR(100), chairperson VARCHAR(50), recorder VARCHAR(50), attendees TEXT, transcript_blob LONGTEXT, created_by VARCHAR(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """, """ CREATE TABLE IF NOT EXISTS meeting_conclusions ( conclusion_id INT PRIMARY KEY AUTO_INCREMENT, meeting_id INT, content TEXT, system_code VARCHAR(20), FOREIGN KEY (meeting_id) REFERENCES meeting_records(meeting_id) ON DELETE CASCADE ) """, """ CREATE TABLE IF NOT EXISTS meeting_action_items ( action_id INT PRIMARY KEY AUTO_INCREMENT, meeting_id INT, content TEXT, owner VARCHAR(50), due_date DATE, status ENUM('Open', 'In Progress', 'Done', 'Delayed') DEFAULT 'Open', system_code VARCHAR(20), FOREIGN KEY (meeting_id) REFERENCES meeting_records(meeting_id) ON DELETE CASCADE ) """, ] with get_db_cursor(commit=True) as cursor: for statement in create_statements: cursor.execute(statement)