File size: 2,133 Bytes
e86dfae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Database Initialization Script
Run this once to create all tables and seed default data.

Usage:
    python -m scripts.init_db
    # or from backend root:
    python scripts/init_db.py
"""

import logging
import os
import sys

# Add the parent directory to path so app imports work
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def main():
    logger.info("Starting MiningNiti database initialization...")

    try:
        from app.config import settings
        from app.db.session import check_db_connection, engine, init_db

        # Check connection
        logger.info(f"Connecting to database...")
        if not check_db_connection():
            logger.error("❌ Cannot connect to database. Check your DATABASE_URL.")
            sys.exit(1)

        logger.info("✅ Database connection OK")

        # Try to enable pgvector extension
        try:
            from sqlalchemy import text

            with engine.connect() as conn:
                conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
                conn.commit()
            logger.info("✅ pgvector extension enabled")
        except Exception as e:
            logger.warning(f"⚠️  Could not enable pgvector extension: {e}")
            logger.warning(
                "   Vector search will not work. Install pgvector or use Supabase."
            )

        # Create all tables
        logger.info("Creating database tables...")
        init_db()
        logger.info("✅ All tables created successfully")

        # Verify tables exist
        from sqlalchemy import inspect

        inspector = inspect(engine)
        tables = inspector.get_table_names()
        logger.info(f"✅ Tables in database: {', '.join(sorted(tables))}")

        logger.info("\n🚀 MiningNiti database is ready!")

    except Exception as e:
        logger.error(f"❌ Initialization failed: {e}")
        import traceback

        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()