v1.0.94: lazy schema init — pool.py runs init_db on first DB access, not at import

This commit is contained in:
2026-07-28 09:56:44 +04:00
parent 203c12638d
commit 163dd02419
2 changed files with 15 additions and 5 deletions
+15
View File
@@ -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