feat: enhance database migration handling and health check endpoint
- Updated migration logic in main.py to allow skipping migrations during local development with SKIP_DB_MIGRATE environment variable. - Improved error handling for migration failures, ensuring the application does not start if migrations are incomplete. - Added a new health check endpoint (/api/health/ready) to verify database connection and essential tables, aiding in production debugging. - Enhanced run_migrations.py to support ordered execution of migration files and improved transaction handling. - Updated requirements.txt to include sqlparse for SQL statement parsing when psql is unavailable.
This commit is contained in:
1 parent
159ac8fb71
commit
1f2c8ea0f1
6 files changed
+341
-88
No files matched your search
@@ -9,6 +9,7 @@ import json
|
||||
from typing import Any, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
||||
from psycopg2.extras import Json
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
@@ -16,6 +17,15 @@ from auth import require_auth
|
||||
router = APIRouter(prefix="/api", tags=["skills"])
|
||||
|
||||
|
||||
def _jsonb_param(val: Any) -> Any:
|
||||
"""psycopg2 benötigt Json() oder String für JSONB — rohe dict/list sonst ProgrammingError."""
|
||||
if val is None:
|
||||
return None
|
||||
if isinstance(val, (dict, list)):
|
||||
return Json(val)
|
||||
return val
|
||||
|
||||
|
||||
def _main_id_for_category(cur, category_id: Optional[int]) -> Optional[int]:
|
||||
if not category_id:
|
||||
return None
|
||||
@@ -181,7 +191,7 @@ def create_skill(data: dict, session=Depends(require_auth)):
|
||||
data.get("category"),
|
||||
data.get("description"),
|
||||
data.get("importance"),
|
||||
data.get("keywords"),
|
||||
_jsonb_param(data.get("keywords")),
|
||||
data.get("status", "active"),
|
||||
cat_id,
|
||||
main_id,
|
||||
@@ -218,7 +228,6 @@ def update_skill(skill_id: int, data: dict, session=Depends(require_auth)):
|
||||
"category",
|
||||
"description",
|
||||
"importance",
|
||||
"keywords",
|
||||
"status",
|
||||
"category_id",
|
||||
"main_category_id",
|
||||
@@ -227,6 +236,9 @@ def update_skill(skill_id: int, data: dict, session=Depends(require_auth)):
|
||||
if key in data:
|
||||
sets.append(f"{key} = %s")
|
||||
vals.append(data[key])
|
||||
if "keywords" in data:
|
||||
sets.append("keywords = %s")
|
||||
vals.append(_jsonb_param(data["keywords"]))
|
||||
if "focus_areas" in data:
|
||||
fa = data["focus_areas"]
|
||||
if isinstance(fa, (list, dict)):
|
||||
@@ -271,7 +283,6 @@ def delete_skill(skill_id: int, session=Depends(require_auth)):
|
||||
|
||||
# Delete
|
||||
cur.execute("DELETE FROM skills WHERE id = %s", (skill_id,))
|
||||
conn.commit()
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
Reference in new issue
Block a user