File size: 1,238 Bytes
c5e0c45
 
 
f489a18
 
544fb75
 
f489a18
 
 
c5e0c45
 
 
5b1b0d1
 
d404857
f489a18
5b1b0d1
f489a18
 
9d1c071
5b1b0d1
15524ff
 
 
 
 
 
 
 
9d1c071
 
 
 
 
 
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
import os
from dotenv import load_dotenv

# Tentative d'import SQLAlchemy uniquement si disponible 
try: 
    from sqlalchemy import create_engine 
    from sqlalchemy.orm import sessionmaker, declarative_base 
    SQLALCHEMY_AVAILABLE = True 
except ModuleNotFoundError: 
    SQLALCHEMY_AVAILABLE = False

load_dotenv()

# Détection si on est en CI (GitHub Actions) ou en test 
IS_CI = os.getenv("CI") == "true"
IS_PYTEST = "pytest" in os.getenv("PYTHONPATH", "") or os.getenv("PYTEST_CURRENT_TEST") is not None 
IS_HF = os.getenv("SPACE_ID") is not None # Hugging Face 

SKIP_DB = IS_CI or IS_PYTEST or IS_HF or not SQLALCHEMY_AVAILABLE
Base = declarative_base() if SQLALCHEMY_AVAILABLE else None

if not SKIP_DB:
    DB_USER = os.getenv("DB_USER", "postgres")
    DB_PASSWORD = os.getenv("DB_PASSWORD", "password")
    DB_HOST = os.getenv("DB_HOST", "localhost")
    DB_PORT = os.getenv("DB_PORT", "5432")
    DB_NAME = os.getenv("DB_NAME", "test_db")

    DATABASE_URL = (f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}"f"@{DB_HOST}:{DB_PORT}/{DB_NAME}")

    engine = create_engine(DATABASE_URL)
    SessionLocal = sessionmaker(autocommit = False, autoflush = False, bind = engine)

else: 
    engine = None 
    SessionLocal = None