From 163dd02419cbb65e474081f34638537ce4608be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Tue, 28 Jul 2026 09:56:44 +0400 Subject: [PATCH] =?UTF-8?q?v1.0.94:=20lazy=20schema=20init=20=E2=80=94=20p?= =?UTF-8?q?ool.py=20runs=20init=5Fdb=20on=20first=20DB=20access,=20not=20a?= =?UTF-8?q?t=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/app.py | 5 ----- site/db/pool.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/site/app.py b/site/app.py index d8d0572..25ade0f 100644 --- a/site/app.py +++ b/site/app.py @@ -24,11 +24,6 @@ app = Flask(__name__, template_folder="templates", static_folder="static") app.config["NUBES_API_ENDPOINT"] = os.getenv("NUBES_API_ENDPOINT", "https://lk-api-gateway-test.ngcloud.ru/api/v1/svc") app.config["NUBES_API_TOKEN"] = os.getenv("NUBES_API_TOKEN", "") app.config["VERSION"] = VERSION - -# Инициализация схемы БД (идемпотентно, безопасно для нескольких воркеров) -from db.init_db import init_db -init_db() - app.register_blueprint(main_bp) app.register_blueprint(api_bp) app.register_blueprint(api_test_bp) diff --git a/site/db/pool.py b/site/db/pool.py index 5300785..a0e7c36 100644 --- a/site/db/pool.py +++ b/site/db/pool.py @@ -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