
    !Oj              
       2   S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	  S\	" \R                  5       S\	" \R                  5       S\R                   S\R                   S	\R                   3
r\" \S
SSSSSS9r\" \SSSS9r\" 5       rS rS rS rS\4S jrg)a  
database.py

PostgreSQL database configuration for LeafDoctor AI.

Responsibilities
----------------
- Create SQLAlchemy engine
- Configure connection pooling
- Provide SessionLocal
- Provide Base declarative class
- Initialize database tables

No business logic should exist here.
    )create_engine)declarative_basesessionmaker)settings)
quote_pluszpostgresql+psycopg2://:@/
      Ti  F)	pool_sizemax_overflowpool_pre_pingpool_recycleechofuture)bind	autoflush
autocommitexpire_on_commitc               #   t   #    [        5       n  U v   U R                  5         g! U R                  5         f = f7f)z?
FastAPI dependency.

Usage:
    db: Session = Depends(get_db)
N)SessionLocalclose)dbs    (/var/www/html/leaf-doctor-v1/database.pyget_dbr   7   s*      
B


s   8# 858c                  N    SSK n [        R                  R                  [        S9  g)z)
Create all tables defined in models.py.
r   Nr   )modelsBasemetadata
create_allenginer   s    r   create_tablesr%   E   s     MM&)    c                  N    SSK n [        R                  R                  [        S9  g)z2
Drop all tables.
Useful only during development.
r   Nr   )r   r    r!   drop_allr#   r$   s    r   drop_tablesr)   N   s    
 MM'r&   returnc                      SSK Jn    [        R                  5        nUR	                  U " S5      5        SSS5        g! , (       d  f       g= f! [
         a  n[        SU 35         SnAgSnAff = f)z_
Test PostgreSQL connectivity.

Returns
-------
bool
    True if database connection succeeds.
r   )textzSELECT 1NTzDatabase connection failed: F)
sqlalchemyr,   r#   connectexecute	Exceptionprint)r,   
connectionexcs      r   test_connectionr4   X   sa      ^^tJ/0   ,SE23s1   A =A 
AA A 
A0A++A0N)__doc__r-   r   sqlalchemy.ormr   r   configr   urllib.parser   DB_USERDB_PASSWORDDB_HOSTDB_PORTDB_NAMEDATABASE_URLr#   r   r    r   r%   r)   boolr4    r&   r   <module>rA      s     % 9  # (""#$A(&&'(  
	
 		 *( r&   