| import mysql.connector |
| import sys |
| import os |
| from pathlib import Path |
|
|
| |
| sys.path.append(str(Path(__file__).resolve().parent.parent)) |
|
|
| from backend.database.connection import get_connection |
| from backend.config import DB_NAME |
|
|
| def run_setup(): |
| print("Starting Database Setup...") |
| |
| schema_path = Path(__file__).resolve().parent.parent / "backend" / "database" / "schema.sql" |
| if not schema_path.exists(): |
| print(f"Error: Schema file not found: {schema_path}") |
| return |
|
|
| try: |
| conn = get_connection() |
| cursor = conn.cursor() |
|
|
| |
| print(f"Reading {schema_path.name}...") |
| with open(schema_path, 'r', encoding='utf-8') as f: |
| schema_sql = f.read() |
|
|
| |
| statements = schema_sql.split(';') |
| for statement in statements: |
| stmt = statement.strip() |
| if stmt and not stmt.startswith('--'): |
| try: |
| cursor.execute(stmt) |
| except mysql.connector.Error as err: |
| if "already exists" in str(err).lower() or "Duplicate" in str(err): |
| continue |
| |
| if "Unknown database" in str(err): |
| continue |
| print(f"Warning on stmt: {stmt[:50]}... \n Error: {err}") |
|
|
| |
| print("Running additional safety checks...") |
| |
| |
| try: |
| cursor.execute("ALTER TABLE conversations ADD COLUMN conv_type VARCHAR(20) DEFAULT 'general'") |
| print("Column 'conv_type' added to 'conversations'.") |
| except mysql.connector.Error as err: |
| if "Duplicate column name" not in str(err): |
| print(f"Note (conv_type): {err}") |
|
|
| |
| try: |
| cursor.execute("ALTER TABLE chunks ADD COLUMN url_ref TEXT DEFAULT NULL") |
| print("Column 'url_ref' added to 'chunks'.") |
| except mysql.connector.Error as err: |
| if "Duplicate column name" not in str(err): |
| print(f"Note (url_ref): {err}") |
|
|
| |
| try: |
| cursor.execute("ALTER TABLE chat_history ADD COLUMN conversation_id VARCHAR(36) DEFAULT NULL") |
| print("Column 'conversation_id' added to 'chat_history'.") |
| except mysql.connector.Error as err: |
| if "Duplicate column name" not in str(err): |
| print(f"Note (conversation_id): {err}") |
|
|
| |
| try: |
| cursor.execute("ALTER TABLE image_jobs MODIFY COLUMN source_id VARCHAR(36) NULL") |
| print("Column 'image_jobs.source_id' modified to NULLable.") |
| except mysql.connector.Error as err: |
| pass |
|
|
| conn.commit() |
| cursor.close() |
| conn.close() |
| print("\nDatabase setup complete!") |
|
|
| except Exception as e: |
| print(f"Critical error during setup: {e}") |
|
|
| if __name__ == "__main__": |
| run_setup() |
|
|