🔧 Maintenance 数据维护
多维数据维护 API,用于扫描全库孤儿节点(外键引用缺失的记录)并执行清理操作。
接口列表
1. 扫描脏数据 / 孤儿节点
GET /api/admin/maintenance/dirty-data
扫描全库 26 张表的外键引用完整性,返回各类孤儿记录的数量。用于「数据维护」页面顶部概览。
认证:需要 Bearer Token(Admin 权限)
响应:
{
"orphaned_refresh_tokens": 0,
"orphaned_bill_members_user": 0,
"orphaned_ai_reports_user": 0,
"orphaned_api_keys_user": 0,
"orphaned_api_usage_logs_user": 0,
"orphaned_audit_logs_user": 0,
"orphaned_notifications_user": 0,
"orphaned_payments_user": 0,
"orphaned_subscription_history_user": 0,
"orphaned_user_sessions_user": 0,
"orphaned_accounts_user": 0,
"orphaned_transactions_user": 0,
"orphaned_bill_invitations_user": 0,
"orphaned_recurring_user": 0,
"orphaned_bills_owner": 0,
"orphaned_bill_members_bill": 0,
"orphaned_transactions_bill": 0,
"orphaned_categories_bill": 0,
"orphaned_budgets_bill": 0,
"orphaned_bill_csv_uploads_bill": 0,
"orphaned_attachments_bill": 0,
"orphaned_attachments_transaction": 0,
"orphaned_accounts_bill": 0,
"orphaned_bill_invitations_bill": 0,
"orphaned_transactions_category": 0,
"orphaned_budgets_category": 0,
"orphaned_recurring_category": 0,
"orphaned_categories_parent": 0,
"orphaned_transactions_account": 0,
"orphaned_budget_alerts_budget": 0
}
2. 执行脏数据修复
POST /api/admin/maintenance/fix-dirty-data
根据指定类型执行孤儿节点清理。在事务中执行,失败自动回滚。
认证:需要 Bearer Token(Admin 权限)
请求体:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | string | 必填 | 修复类型,详见下表 |
支持的 type 值:
| type | 说明 |
|---|---|
orphaned_refresh_tokens | 清理残留刷新令牌(用户已删除但令牌残留) |
orphaned_bill_members_user | 清理无主账单成员(用户已删除) |
orphaned_api_keys_user | 清理无主 API Keys(用户已删除) |
orphaned_ai_reports_user | 清理无主 AI 报告 |
orphaned_api_usage_logs_user | 清理无主 API 使用日志 |
orphaned_audit_logs_user | 清理审计日志中用户缺失记录 |
orphaned_notifications_user | 清理无主通知记录 |
orphaned_payments_user | 清理无主支付记录 |
orphaned_subscription_history_user | 清理无主订阅历史 |
orphaned_user_sessions_user | 清理无主用户会话 |
orphaned_accounts_user | 清理无主资产账户 |
orphaned_transactions_user | 清理无主交易记录(上传者) |
orphaned_bill_invitations_user | 清理无主账单邀请(发件人) |
orphaned_recurring_user | 清理无主定期账单 |
orphaned_bills_owner | 清理无主账单(所有者缺失) |
orphaned_bill_members_bill | 清理无主账单成员(账单缺失) |
orphaned_transactions_bill | 清理无主交易(账单缺失) |
orphaned_categories_bill | 清理无主类目(账单缺失) |
orphaned_budgets_bill | 清理无主预算(账单缺失) |
orphaned_accounts_bill | 清理无主资产账户(账单缺失) |
orphaned_bill_invitations_bill | 清理无主账单邀请(账单缺失) |
orphaned_transactions_category | 清理无主交易(类目缺失) |
orphaned_budgets_category | 清理无主预算(类目缺失) |
orphaned_recurring_category | 清理无主定期账单(类目缺失) |
orphaned_categories_parent | 清理孤儿父分类引用 |
orphaned_transactions_account | 清理无主交易(账户缺失) |
orphaned_budget_alerts_budget | 清理无主预算预警(预算缺失) |
all | 执行全库一键修复(遍历以上所有类型) |
响应:
{
"deleted": 42,
"message": "成功清理 42 条记录"
}
错误响应:
{"error": "修复类型不能为空"}