
    hip	                        d Z ddlZddlZddlmZmZmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ  ej$                  e      Z e         ej*                  d	d
      Zej/                  d      rej1                  ddd      Z eeddd      Z edde      Zd Zdee	ddf   fdZdefdZ ej>                  ed      d        Z  ej>                  ed      d        Z!y)uA   
Configuration de la base de données PostgreSQL avec SQLAlchemy
    N)create_engineeventtext)sessionmakerSession)NullPool)	Generator)load_dotenv)BaseDATABASE_URLz=postgresql://postgres:postgres@localhost:5432/whatsapp_cv_botzpostgres://zpostgresql://   TF)pool_pre_pingechofuture)
autocommit	autoflushbindc                      	 t         j                  j                  t               t        j                  d       y# t        $ r} t        j                  d|          d} ~ ww xY w)u0   Créer toutes les tables si elles n'existent pas)r   u7   ✅ Tables PostgreSQL créées/vérifiées avec succèsu   ❌ Erreur création tables: N)r   metadata
create_allengineloggerinfo	Exceptionerror)es    4/home/www/therecruiter.miabetepe.com/app/database.pycreate_tablesr   +   sP      f -MN 4QC89s   9< 	A#AA#returnc               #   t   K   t               } 	 |  | j                          y# | j                          w xY ww)uh   
    Dependency pour obtenir une session de base de données
    À utiliser avec FastAPI Depends()
    N)SessionLocalclose)dbs    r   get_dbr$   5   s+     
 
B


s   8# 858c                     	 t         j                         5 } | j                  t        d             ddd       t        j                  d       y# 1 sw Y   xY w# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)u$   Vérifier la connexion à PostgreSQLzSELECT 1Nu   ✅ Connexion PostgreSQL OKTu!   ❌ Échec connexion PostgreSQL: F)r   connectexecuter   r   r   r   r   )
connectionr   s     r   check_connectionr)   A   ss    ^^ 	1tJ/0	112	1 	1  8<=s-   A AA AA 	B$BBr&   c                 .    t         j                  d       y)u$   Log quand une connexion est établieu+   📡 Nouvelle connexion PostgreSQL établieNr   debug
dbapi_connconnection_records     r   receive_connectr0   N   s     LL>?    r"   c                 .    t         j                  d       y)u#   Log quand une connexion est ferméeu!   🔌 Connexion PostgreSQL ferméeNr+   r-   s     r   receive_closer3   T   s     LL45r1   )"__doc__oslogging
sqlalchemyr   r   r   sqlalchemy.ormr   r   sqlalchemy.poolr   typingr	   dotenvr
   
app.modelsr   	getLogger__name__r   getenvr   
startswithreplacer   r!   r   r$   boolr)   listens_forr0   r3    r1   r   <module>rE      s   
  1 1 0 $   			8	$  ryyC =)''JL 
		
 			'4-. 		$ 	 69%@ &@
 67#6 $6r1   