File size: 2,051 Bytes
13933bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
from dotenv import load_dotenv
import logging
import urllib.parse

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

# Load environment variables
load_dotenv()

# Get database credentials from environment variables
DB_USER = os.getenv("DB_USER", "dev_cbs_admin")
DB_PASSWORD = os.getenv("DB_PASSWORD", "password")
DB_HOST = os.getenv("DB_HOST", "13.126.242.31")
DB_PORT = os.getenv("DB_PORT", "5432")
DB_NAME = os.getenv("DB_NAME", "vst")

# URL encode the password to handle special characters like @
encoded_password = urllib.parse.quote_plus(DB_PASSWORD)

# Log the connection parameters (without password)
logger.info(f"Attempting to connect to PostgreSQL database at {DB_HOST}:{DB_PORT}/{DB_NAME} as {DB_USER}")

# Create database URL for PostgreSQL with encoded password
SQLALCHEMY_DATABASE_URL = f"postgresql://{DB_USER}:{encoded_password}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
# Log the URL with password masked
masked_url = SQLALCHEMY_DATABASE_URL.replace(encoded_password, "****")
logger.info(f"Connection URL: {masked_url}")

try:
    # Create engine
    engine = create_engine(SQLALCHEMY_DATABASE_URL)
    
    # Test connection
    with engine.connect() as conn:
        logger.info("Database connection successful")
except Exception as e:
    logger.error(f"Database connection error: {e}")
    # Create a SQLite engine as fallback for development/testing
    logger.info("Using SQLite as fallback database")
    SQLALCHEMY_DATABASE_URL = "sqlite:///./visitor_management.db"
    engine = create_engine(
        SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    )

# Create session
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Create base class
Base = declarative_base()

# Dependency to get DB session
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()