fix: 兼容 corp_id 字段不存在 + 添加迁移 API
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -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 ..config import get_settings
|
||||||
|
from ..database import get_db
|
||||||
|
|
||||||
router = APIRouter(tags=["health"])
|
router = APIRouter(tags=["health"])
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
@@ -15,3 +18,28 @@ async def health_check():
|
|||||||
"app": settings.APP_NAME,
|
"app": settings.APP_NAME,
|
||||||
"version": settings.APP_VERSION
|
"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)}
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ async def list_tenants(
|
|||||||
"id": t.id,
|
"id": t.id,
|
||||||
"code": t.code,
|
"code": t.code,
|
||||||
"name": t.name,
|
"name": t.name,
|
||||||
"corp_id": t.corp_id,
|
"corp_id": getattr(t, 'corp_id', None),
|
||||||
"contact_info": t.contact_info,
|
"contact_info": t.contact_info,
|
||||||
"status": t.status,
|
"status": t.status,
|
||||||
"expired_at": t.expired_at,
|
"expired_at": t.expired_at,
|
||||||
@@ -123,7 +123,7 @@ async def get_tenant(
|
|||||||
"id": tenant.id,
|
"id": tenant.id,
|
||||||
"code": tenant.code,
|
"code": tenant.code,
|
||||||
"name": tenant.name,
|
"name": tenant.name,
|
||||||
"corp_id": tenant.corp_id,
|
"corp_id": getattr(tenant, 'corp_id', None),
|
||||||
"contact_info": tenant.contact_info,
|
"contact_info": tenant.contact_info,
|
||||||
"status": tenant.status,
|
"status": tenant.status,
|
||||||
"expired_at": tenant.expired_at,
|
"expired_at": tenant.expired_at,
|
||||||
|
|||||||
Reference in New Issue
Block a user