
    Xjf                        d Z ddlZddlmZ ddlZddlmZ ej                  j                  ej                  j                  e
      dd      Z ee        ej                  dd      Z e ej                  d	d
            Z ej                  dd      Z ej                  dd      Z ej                  dd      Zdedej(                  fdZedefd       Zed        Zy)a  
Database connection helper for the Scheduler service.

Mirrors the PHP DBConnectionHelper: reads DB_MASTER_* credentials from the
environment and creates per-request connections to the organisation-specific
database whose name arrives in the X-DB-Name header.
    N)contextmanager)load_dotenvz..z.envDB_MASTER_HOSTz	127.0.0.1DB_MASTER_PORT3306DB_MASTER_USERNAME DB_MASTER_PASSWORDDB_MASTER_DATABASE
MYL_MASTERdatabasereturnc           	          t        j                  t        t        t        t
        | dt         j                  j                        S )zCReturn a new pymysql connection to *database* on the master server.utf8mb4)hostportuserpasswordr   charsetcursorclass)pymysqlconnectr   r   r   r
   cursors
DictCursor)r   s    -/var/www/html/myl_app/scheduler-service/db.py_get_connectionr      s2    ??#OO..     db_namec              #   v   K   t        |       }	 | |j                          y# |j                          w xY ww)z
    Context manager that yields a pymysql connection to the organisation
    database identified by *db_name*.  The connection is closed on exit.
    N)r   close)r   conns     r   get_dynamic_connectionr"   (   s.      7#D




s   9$ 969c               #   ~   K   t        t              } 	 |  | j                          y# | j                          w xY ww)zr
    Context manager that yields a pymysql connection to MYL_MASTER.
    Used for auth lookups (users table).
    N)r   r   r    )r!   s    r   get_master_connectionr$   5   s/      -.D




s   =( =:=)__doc__os
contextlibr   r   dotenvr   pathjoindirname__file__	_ENV_PATHgetenvr   intr   r   r
   r   str
Connectionr   r"   r$    r   r   <module>r3      s    
 %   GGLL2D&A	 I  +[9YRYY/89RYY3R8 RYY3R8 RYY3\B 
c 
g&8&8 
 	C 	 	 	 	r   