From 6b5a6f79aa65903bfbfa2616c4c502563708135d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Sat, 13 Jun 2026 12:48:33 +0400 Subject: [PATCH] =?UTF-8?q?feat:=20/test=20createdb=20=E2=80=94=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=91=D0=94=20?= =?UTF-8?q?=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/app.py | 1 - site/test_routes.py | 26 +++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/site/app.py b/site/app.py index 70c3e9e..062f8d3 100644 --- a/site/app.py +++ b/site/app.py @@ -9,7 +9,6 @@ load_dotenv() class ContractsApp: def __init__(self): self.app = Flask(__name__) - db.ensure_db() self.add_routes() def add_routes(self): diff --git a/site/test_routes.py b/site/test_routes.py index 1a2125a..858d728 100644 --- a/site/test_routes.py +++ b/site/test_routes.py @@ -1,3 +1,4 @@ +import os from flask import Blueprint, jsonify, request import db @@ -14,9 +15,10 @@ def test(): return jsonify({ "db": "ok" if conn else f"fail: {err}", "actions": [ - "GET /test — статус БД", - "POST /test SQL:... — выполнить запрос", - "POST /test tables — список таблиц", + "GET /test — статус БД", + "POST /test createdb — создать БД", + "POST /test tables — список таблиц", + "POST /test SQL:... — выполнить запрос", ], }) @@ -36,6 +38,24 @@ def test(): return jsonify({"error": err}), 500 return jsonify({"tables": [r[0] for r in result["rows"]]}) + if action == "createdb": + try: + conn = db._pg_connect("postgres") + conn.autocommit = True + cur = conn.cursor() + db_name = data.get("name") or os.environ.get("DB_NAME", "contracts") + cur.execute("SELECT 1 FROM pg_database WHERE datname = %s", (db_name,)) + if cur.fetchone(): + msg = f"DB '{db_name}' already exists" + else: + cur.execute(db.sql.SQL("CREATE DATABASE {}").format(db.sql.Identifier(db_name))) + msg = f"DB '{db_name}' created" + cur.close() + conn.close() + return jsonify({"result": msg}) + except Exception as e: + return jsonify({"error": str(e)}), 500 + if action == "sql": sql_text = data.get("sql", "") if not sql_text: