File size: 3,189 Bytes
09c6a5b
 
67ac4d2
 
 
09c6a5b
67ac4d2
09c6a5b
ebee7b7
 
 
 
 
 
 
09c6a5b
 
 
 
 
 
 
 
ebee7b7
09c6a5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67ac4d2
09c6a5b
67ac4d2
 
 
544faf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67ac4d2
 
 
 
 
 
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
import logging
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker, declarative_base
from app.core.config import settings

logger = logging.getLogger(__name__)

db_url = settings.clean_database_url
connect_args = {}
pool_pre_ping = True

if db_url.startswith("sqlite"):
    connect_args = {"check_same_thread": False}
    pool_pre_ping = False

def create_safe_engine(target_url: str):
    is_sqlite = target_url.startswith("sqlite")
    c_args = {"check_same_thread": False} if is_sqlite else {}
    
    eng = create_engine(
        target_url,
        connect_args=c_args,
        pool_pre_ping=not is_sqlite
    )
    
    # Eagerly test connection if remote
    if not is_sqlite:
        try:
            with eng.connect() as conn:
                conn.execute(text("SELECT 1"))
            logger.info("Database connection established successfully.")
            return eng
        except Exception as conn_err:
            print(f"⚠️ Remote Database Connection Failed ({target_url}): {conn_err}")
            print("🔄 Falling back to local SQLite engine (attendx.db) to ensure high availability.")
            return create_engine(
                "sqlite:///./attendx.db",
                connect_args={"check_same_thread": False}
            )
    return eng

engine = create_safe_engine(db_url)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

def run_auto_migrations(target_engine):
    """Safely apply non-destructive schema additions to existing databases without dropping tables."""
    try:
        with target_engine.connect() as conn:
            # Check users table columns
            if target_engine.dialect.name == "sqlite":
                res = conn.execute(text("PRAGMA table_info(users)")).fetchall()
                existing_cols = {row[1] for row in res}
                if "is_approved" not in existing_cols and len(existing_cols) > 0:
                    conn.execute(text("ALTER TABLE users ADD COLUMN is_approved BOOLEAN DEFAULT 1"))
                    conn.commit()
                if "approved_by" not in existing_cols and len(existing_cols) > 0:
                    conn.execute(text("ALTER TABLE users ADD COLUMN approved_by INTEGER"))
                    conn.commit()
                if "approved_at" not in existing_cols and len(existing_cols) > 0:
                    conn.execute(text("ALTER TABLE users ADD COLUMN approved_at TIMESTAMP"))
                    conn.commit()
            elif target_engine.dialect.name == "postgresql":
                conn.execute(text("ALTER TABLE users ADD COLUMN IF NOT EXISTS is_approved BOOLEAN DEFAULT TRUE"))
                conn.execute(text("ALTER TABLE users ADD COLUMN IF NOT EXISTS approved_by INTEGER"))
                conn.execute(text("ALTER TABLE users ADD COLUMN IF NOT EXISTS approved_at TIMESTAMP"))
                conn.commit()
    except Exception as e:
        print(f"Auto-migration notice (non-fatal): {e}")

# Run schema sync safely
try:
    run_auto_migrations(engine)
except Exception:
    pass

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()