File size: 3,442 Bytes
04858f5
 
 
 
 
 
 
 
 
 
 
 
 
1fb9097
04858f5
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb9097
04858f5
 
1fb9097
04858f5
 
 
 
 
 
 
 
 
1fb9097
04858f5
 
 
 
 
 
 
 
 
 
1fb9097
04858f5
 
 
1fb9097
04858f5
 
1fb9097
04858f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb9097
04858f5
 
1fb9097
04858f5
 
1fb9097
04858f5
 
 
 
 
 
 
 
 
 
 
1fb9097
04858f5
 
 
1fb9097
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))

import os
from config.settings import DB_URL
from sqlalchemy import text, create_engine

def init_database():
    print("Initializing database schema...")
    
    schema_file = Path(__file__).parent / "database" / "schema.sql"
    if not schema_file.exists():
        print(f"Schema file not found: {schema_file}")
        return False
    
    with open(schema_file, 'r') as f:
        schema_sql = f.read()
    
    try:
        engine = create_engine(DB_URL)
        with engine.connect() as conn:
            for statement in schema_sql.split(';'):
                statement = statement.strip()
                if statement:
                    print(f"Executing: {statement[:80]}...")
                    conn.execute(text(statement))
            conn.commit()
        print("Schema created successfully")
        return True
    except Exception as e:
        print(f"Error creating schema: {e}")
        return False

def load_seed_data():
    """Load seed data from seed_data.sql"""
    print("\nLoading seed data...")
    
    # Read seed data
    seed_file = Path(__file__).parent / "database" / "seed_data.sql"
    if not seed_file.exists():
        print(f"Seed data file not found: {seed_file}")
        return False
    
    with open(seed_file, 'r') as f:
        seed_sql = f.read()
    
    try:
        engine = create_engine(DB_URL)
        with engine.connect() as conn:
            for statement in seed_sql.split(';'):
                statement = statement.strip()
                if statement and not statement.startswith('--'):  
                    print(f"Executing: {statement[:80]}...")
                    conn.execute(text(statement))
            conn.commit()
        print("Seed data loaded successfully")
        return True
    except Exception as e:
        print(f"Error loading seed data: {e}")
        return False

def verify_data():
    print("\nVerifying data...")
    try:
        from database.connection import get_session
        from database.models import MarketData, NewsSentiment, Anomaly, Forecast
        
        with get_session() as session:
            market_count = session.query(MarketData).count()
            sentiment_count = session.query(NewsSentiment).count()
            anomaly_count = session.query(Anomaly).count()
            forecast_count = session.query(Forecast).count()
            
            print(f"MarketData rows: {market_count}")
            print(f"NewsSentiment rows: {sentiment_count}")
            print(f"Anomaly rows: {anomaly_count}")
            print(f"Forecast rows: {forecast_count}")
            
            if market_count > 0:
                print("Data verification successful!")
                return True
            else:
                print("No data found - check seed_data.sql is being executed correctly")
                return False
    except Exception as e:
        print(f"Error verifying data: {e}")
        return False

if __name__ == "__main__":
    print(f"Database URL: {DB_URL}")
    print("=" * 60)
    
    success = init_database() and load_seed_data()
    
    if success:
        verify_data()
        print("\n" + "=" * 60)
        print("Database initialization complete!")
        print("You can now start the API and dashboard")
    else:
        print("\n" + "=" * 60)
        print("Database initialization failed!")