v1.0.94: lazy schema init — pool.py runs init_db on first DB access, not at import
This commit is contained in:
@@ -17,6 +17,7 @@ import psycopg2
|
||||
from psycopg2 import pool
|
||||
|
||||
_pool = None
|
||||
_initialized = False
|
||||
|
||||
|
||||
def _dsn():
|
||||
@@ -30,12 +31,26 @@ def _dsn():
|
||||
)
|
||||
|
||||
|
||||
def _ensure_schema():
|
||||
"""Инициализировать схему при первом обращении к БД."""
|
||||
global _initialized
|
||||
if _initialized:
|
||||
return
|
||||
_initialized = True
|
||||
try:
|
||||
from db.init_db import init_db
|
||||
init_db()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def get_pool():
|
||||
global _pool
|
||||
if _pool is None:
|
||||
if not os.getenv("DB_USER"):
|
||||
return None
|
||||
_pool = pool.ThreadedConnectionPool(1, 5, _dsn())
|
||||
_ensure_schema()
|
||||
return _pool
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user