поправлены удаления, и поправлено отображение в настройках SQL
This commit is contained in:
@@ -6,7 +6,7 @@ from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import and_, delete, select, text
|
||||
from sqlalchemy import and_, delete, inspect, select, text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from .auth_bridge import HockeyUser
|
||||
@@ -229,6 +229,7 @@ class MappingDataService:
|
||||
def __init__(self, database: HockeyDatabase, settings: Any | None = None) -> None:
|
||||
self.database = database
|
||||
self.settings = settings
|
||||
self._database_schema_cache: dict[str, Any] | None = None
|
||||
|
||||
def _current_language(self, supplied: dict[str, Any] | None = None) -> str:
|
||||
supplied = supplied or {}
|
||||
@@ -383,6 +384,44 @@ class MappingDataService:
|
||||
rows = list(session.scalars(select(MappingSqlDataSource).order_by(MappingSqlDataSource.sort_order, MappingSqlDataSource.id)))
|
||||
return {"sources": [self._source_payload(row) for row in rows]}
|
||||
|
||||
def database_schema_reference(self) -> dict[str, Any]:
|
||||
"""Return a compact read-only reference of hockey database tables.
|
||||
|
||||
This is intentionally metadata-only: table/column names and SQL types,
|
||||
never row data or connection credentials. The SQL editor uses it as an
|
||||
expandable cheat sheet so operators do not have to inspect PostgreSQL
|
||||
manually just to remember a table name.
|
||||
"""
|
||||
if self._database_schema_cache is not None:
|
||||
return self._database_schema_cache
|
||||
try:
|
||||
inspector = inspect(self.database.engine)
|
||||
table_names = sorted(
|
||||
name for name in inspector.get_table_names()
|
||||
if str(name).startswith("hockey_")
|
||||
)
|
||||
tables: list[dict[str, Any]] = []
|
||||
for table_name in table_names:
|
||||
columns = []
|
||||
for column in inspector.get_columns(table_name):
|
||||
columns.append({
|
||||
"name": str(column.get("name") or ""),
|
||||
"type": str(column.get("type") or ""),
|
||||
"nullable": bool(column.get("nullable", True)),
|
||||
})
|
||||
tables.append({
|
||||
"name": table_name,
|
||||
"column_count": len(columns),
|
||||
"columns": columns,
|
||||
})
|
||||
payload = {"tables": tables, "table_count": len(tables)}
|
||||
self._database_schema_cache = payload
|
||||
return payload
|
||||
except SQLAlchemyError as exc:
|
||||
return {"tables": [], "table_count": 0, "error": str(exc)}
|
||||
except Exception as exc:
|
||||
return {"tables": [], "table_count": 0, "error": str(exc)}
|
||||
|
||||
def create_source(self, payload: Any, user: HockeyUser) -> dict[str, Any]:
|
||||
self.ensure_defaults()
|
||||
code = str(payload.code or "").strip()
|
||||
|
||||
Reference in New Issue
Block a user