-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
40 lines (31 loc) · 1.16 KB
/
database.py
File metadata and controls
40 lines (31 loc) · 1.16 KB
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
"""
database.py — Configuration PostgreSQL + session SQLAlchemy
CV Generator App
"""
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# URL de base de données :
# - En production (Docker) : DATABASE_URL ou PostgreSQL via POSTGRES_PASSWORD
# - En développement local : SQLite (fallback si ni DATABASE_URL ni POSTGRES_PASSWORD)
_pw = os.getenv("POSTGRES_PASSWORD")
if os.getenv("DATABASE_URL"):
DATABASE_URL = os.getenv("DATABASE_URL")
elif _pw:
DATABASE_URL = f"postgresql+psycopg2://cvgen:{_pw}@db:5432/cvgen"
else:
DATABASE_URL = "sqlite:///./cv_generator.db"
_connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
engine = create_engine(DATABASE_URL, connect_args=_connect_args)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
"""Dépendance FastAPI — fournit une session DB et la ferme après la requête."""
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
"""Crée toutes les tables si elles n'existent pas."""
from models import Base
Base.metadata.create_all(bind=engine)