from fastapi import APIRouter, Depends from sqlalchemy import text from sqlalchemy.orm import Session from ..config import DB_NAME from ..database import get_session from ..services import dq as svc router = APIRouter(prefix="/api/governance", tags=["治理与稽核"]) @router.post("/dq-check") def dq_check(db: Session = Depends(get_session)): return svc.run_checks(db) @router.get("/tables") def tables(db: Session = Depends(get_session)): """从 information_schema 读取库表元数据(DataHub 元数据中枢的本地替身)。""" tables = db.execute( text( "SELECT table_name AS table_name, table_comment AS table_comment " "FROM information_schema.tables " "WHERE table_schema = :s ORDER BY table_name" ), {"s": DB_NAME}, ).fetchall() result = [] for t in tables: cols = db.execute( text( "SELECT column_name AS column_name, column_type AS column_type, " "is_nullable AS is_nullable, column_default AS column_default, " "column_comment AS column_comment " "FROM information_schema.columns " "WHERE table_schema = :s AND table_name = :t ORDER BY ordinal_position" ), {"s": DB_NAME, "t": t.table_name}, ).fetchall() result.append( { "table_name": t.table_name, "table_comment": t.table_comment, "columns": [ { "name": c.column_name, "type": c.column_type, "nullable": c.is_nullable == "YES", "default": c.column_default, "comment": c.column_comment, } for c in cols ], } ) return {"database": DB_NAME, "tables": result}