数据目录导出导入:导出为「<数据源名>-数据目录.json」附件(全部状态的项、起草要求);导入先预览再写入,表名、列名按表结构的写法再忽略大小写对(带 schema 前缀的去掉前缀再对),关联关系两端换成目标的写法并重算编号;已确认、已驳回的项照带(来源、状态不变),目标里已确认、已驳回而不一样的保留目标的并记冲突,推断、有确证的只填空缺;表结构里没有的列、两端对不上的关系、目标遮罩列的码值、不合规的项跳过并写原因;逐表乐观锁、一张表写不进去不影响别的;有起草任务时 409,文件格式不对 422,没有表结构 409;起草要求只在勾选且目标还没有时写入

This commit is contained in:
ThearchyHelios
2026-10-14 08:06:45 +08:00
parent adc56da6cf
commit 2298cf848a
3 changed files with 927 additions and 1 deletions
+65 -1
View File
@@ -16,6 +16,8 @@
- GET /catalog/review-queue 审阅队列:推断的项按使用次数、风险、来源排好;POST /catalog/review/batch 批量审阅,
GET /catalog/review-progress 审阅进度,GET /catalog/{table}/usage 用过这张表的运行
(data/catalog_review.py)
- GET /catalog/export 导出数据目录(JSON 附件);POST /catalog/import/preview 导入预览(只读),
POST /catalog/import 导入(data/catalog_transfer.py)
规则都在 data/catalog.py,这里只做取源、转换形状和把异常翻成状态码:版本不符 409,结构不合规、审阅路径
不对 422,源或表不存在 404。所有写操作记录请求头 X-Actor 的署名(自报、未认证)。剖析没开启、正在剖析 409。
@@ -38,7 +40,7 @@ from app.api.coded import (
)
from app.api.runs import actor_of
from app.data import catalog, catalog_impact, catalog_jobs, catalog_profile, introspect, table_versions
from app.data import catalog_review
from app.data import catalog_review, catalog_transfer
from app.data.engine import SnapshotTampered
from app.db.base import get_session
from app.db.models import DataSource
@@ -262,6 +264,68 @@ async def catalog_review_progress(source_id: str, session: AsyncSession = Depend
return await catalog_review.review_progress(session, source)
# ---- 导出导入(导出同样要登记在 GET /catalog/{table} 前面)
@router.get("/{source_id}/catalog/export")
async def export_catalog(source_id: str, session: AsyncSession = Depends(get_session)) -> Any:
"""导出这个数据源的数据目录:JSON 附件「<数据源名>-数据目录.json」,含全部状态的项(data/catalog_transfer.py)。"""
from fastapi.responses import JSONResponse
row = await _source_row(session, source_id)
body = await catalog_transfer.export_catalog(session, row)
name = catalog_transfer.attachment_name(row.name)
return JSONResponse(body, headers={"Content-Disposition": catalog_transfer.content_disposition(name, row.id)})
class CatalogImportIn(BaseModel):
#: 导出的那个对象(格式在 data/catalog_transfer.read_file 里核对,不对 422,说明是中文)
content: Any = None
#: 目标还没有起草要求时,是否写入文件里的起草要求
include_guidance: bool = False
async def _import_source(session: AsyncSession, source_id: str) -> tuple[DataSource, Any]:
row, source = await _resolved(session, source_id)
cache = source.schema_cache or {}
if not cache.get("tables"):
raise CodedHTTPException(409, f"{introspect.why_empty(cache)},无法导入数据目录。请先在数据源卡片上点「探查结构」",
SCHEMA_MISSING)
return row, source
@router.post("/{source_id}/catalog/import/preview")
async def preview_catalog_import(source_id: str, payload: CatalogImportIn,
session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
"""导入预览(只读):每张对上的表新增、更新、保留几项,冲突和跳过的逐条列出。文件格式不对 422;没有表结构 409。"""
row, source = await _import_source(session, source_id)
try:
report = await catalog_transfer.plan_import(session, row, source, payload.content,
include_guidance=payload.include_guidance)
except catalog_transfer.TransferInvalid as e:
raise HTTPException(422, str(e)) from e
return report.as_dict()
@router.post("/{source_id}/catalog/import")
async def import_catalog(source_id: str, payload: CatalogImportIn, x_actor: str | None = Header(default=None),
session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
"""导入:规则同预览,逐表写入(乐观锁),返回同样形状的报告和每张表写入后的版本。有排队或进行中的起草任务
409 catalog_draft_busy;文件格式不对 422;没有表结构 409。"""
row, source = await _import_source(session, source_id)
try:
await catalog_jobs.ensure_idle(session, row)
except catalog_jobs.DraftBusy as e:
raise CodedHTTPException(409, str(e), CATALOG_DRAFT_BUSY) from e
try:
report = await catalog_transfer.run_import(session, row, source, payload.content,
include_guidance=payload.include_guidance,
actor=actor_of(x_actor))
except catalog_transfer.TransferInvalid as e:
raise HTTPException(422, str(e)) from e
return report.as_dict()
@router.get("/{source_id}/catalog/{table}")
async def get_catalog_table(source_id: str, table: str,
session: AsyncSession = Depends(get_session)) -> dict[str, Any]: