File size: 3,281 Bytes
b2b6341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86be40b
 
 
b2b6341
 
86be40b
b2b6341
 
86be40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2b6341
 
 
 
 
 
86be40b
 
b2b6341
 
 
 
 
 
86be40b
b2b6341
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import mysql.connector
import sys
import os
from pathlib import Path

# Add the project root to sys.path so we can import backend
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()

        # 1. Read and Execute schema.sql
        print(f"Reading {schema_path.name}...")
        with open(schema_path, 'r', encoding='utf-8') as f:
            schema_sql = f.read()

        # Split by semicolon to execute one by one
        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
                    # Ignore USE error on Aiven
                    if "Unknown database" in str(err):
                        continue
                    print(f"Warning on stmt: {stmt[:50]}... \n Error: {err}")

        # 2. Run additional safety checks/migrations
        print("Running additional safety checks...")
        
        # Ensure conv_type column exists in conversations
        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}")

        # Ensure url_ref column exists in chunks
        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}")

        # Ensure conversation_id column in chat_history
        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}")

        # Modify image_jobs.source_id to be NULLable
        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()