diff --git a/backend/app/routers/health.py b/backend/app/routers/health.py index c865291..683016f 100644 --- a/backend/app/routers/health.py +++ b/backend/app/routers/health.py @@ -1,7 +1,10 @@ """健康检查路由""" -from fastapi import APIRouter +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from sqlalchemy import text from ..config import get_settings +from ..database import get_db router = APIRouter(tags=["health"]) settings = get_settings() @@ -15,3 +18,28 @@ async def health_check(): "app": settings.APP_NAME, "version": settings.APP_VERSION } + + +@router.post("/migrate/add-corp-id") +async def migrate_add_corp_id(db: Session = Depends(get_db)): + """迁移:添加租户 corp_id 字段""" + try: + # 检查列是否存在 + result = db.execute(text( + "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " + "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'platform_tenants' AND COLUMN_NAME = 'corp_id'" + )) + exists = result.scalar() + + if exists: + return {"success": True, "message": "字段 corp_id 已存在,无需迁移"} + + # 添加列 + db.execute(text( + "ALTER TABLE platform_tenants ADD COLUMN corp_id VARCHAR(100) AFTER name" + )) + db.commit() + + return {"success": True, "message": "字段 corp_id 添加成功"} + except Exception as e: + return {"success": False, "error": str(e)} diff --git a/backend/app/routers/tenants.py b/backend/app/routers/tenants.py index 48dcffc..4efbb7c 100644 --- a/backend/app/routers/tenants.py +++ b/backend/app/routers/tenants.py @@ -83,7 +83,7 @@ async def list_tenants( "id": t.id, "code": t.code, "name": t.name, - "corp_id": t.corp_id, + "corp_id": getattr(t, 'corp_id', None), "contact_info": t.contact_info, "status": t.status, "expired_at": t.expired_at, @@ -123,7 +123,7 @@ async def get_tenant( "id": tenant.id, "code": tenant.code, "name": tenant.name, - "corp_id": tenant.corp_id, + "corp_id": getattr(tenant, 'corp_id', None), "contact_info": tenant.contact_info, "status": tenant.status, "expired_at": tenant.expired_at,