From c7fbc4dc07cff73ac6a297883651bb6ea212f251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Sat, 13 Jun 2026 12:34:55 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20db.connect()=20=D0=B2=D0=BE=D0=B7=D0=B2?= =?UTF-8?q?=D1=80=D0=B0=D1=89=D0=B0=D0=B5=D1=82=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D1=83,=20/test=20=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=B5=D1=82=20=D0=B4=D0=B5=D1=82=D0=B0=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/app.py | 2 +- site/db.py | 17 +++++++++-------- site/test_routes.py | 8 ++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/site/app.py b/site/app.py index a2429e3..062f8d3 100644 --- a/site/app.py +++ b/site/app.py @@ -17,7 +17,7 @@ class ContractsApp: self.app.register_blueprint(test_bp) def index(self): - conn = db.connect() + conn, _ = db.connect() db_status = "connected" if conn else "no DB" return render_template("index.html", db_status=db_status) diff --git a/site/db.py b/site/db.py index a4db369..2a22546 100644 --- a/site/db.py +++ b/site/db.py @@ -4,9 +4,9 @@ from psycopg2 import sql def connect(): - """Подключение к БД. Возвращает connection или None.""" + """Подключение к БД. Возвращает (connection, None) или (None, error).""" try: - return psycopg2.connect( + conn = psycopg2.connect( host=os.getenv("DB_HOST"), port=os.getenv("DB_PORT", 5432), dbname=os.getenv("DB_NAME"), @@ -14,15 +14,16 @@ def connect(): password=os.getenv("DB_PASS"), sslmode=os.getenv("DB_SSLMODE", "disable"), ) - except Exception: - return None + return conn, None + except Exception as e: + return None, str(e) def query(sql_text, params=None): - """Выполнить запрос, вернуть (rows, columns) или (None, error).""" - conn = connect() - if not conn: - return None, "no connection" + """Выполнить запрос, вернуть (result, error).""" + conn, err = connect() + if err: + return None, f"connect: {err}" try: cur = conn.cursor() cur.execute(sql_text, params) diff --git a/site/test_routes.py b/site/test_routes.py index 19176b9..1a2125a 100644 --- a/site/test_routes.py +++ b/site/test_routes.py @@ -10,9 +10,9 @@ def test(): # GET — статус if request.method == "GET": - conn = db.connect() + conn, err = db.connect() return jsonify({ - "db": "ok" if conn else "fail", + "db": "ok" if conn else f"fail: {err}", "actions": [ "GET /test — статус БД", "POST /test SQL:... — выполнить запрос", @@ -25,8 +25,8 @@ def test(): action = data.get("action", "status") if action == "status": - conn = db.connect() - return jsonify({"db": "ok" if conn else "fail"}) + conn, err = db.connect() + return jsonify({"db": "ok" if conn else f"fail: {err}"}) if action == "tables": result, err = db.query(