cleanup
This commit is contained in:
@@ -12,6 +12,7 @@ from typing import Any, Dict
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.infrastructure.security.database_integrity import DatabaseIntegrityChecker
|
||||
from src.server.services.monitoring_service import get_monitoring_service
|
||||
from src.server.utils.dependencies import get_database_session
|
||||
from src.server.utils.system import get_system_utilities
|
||||
@@ -373,3 +374,86 @@ async def full_health_check(
|
||||
except Exception as e:
|
||||
logger.error(f"Health check failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/integrity/check")
|
||||
async def check_database_integrity(
|
||||
db: AsyncSession = Depends(get_database_session),
|
||||
) -> Dict[str, Any]:
|
||||
"""Check database integrity.
|
||||
|
||||
Verifies:
|
||||
- No orphaned records
|
||||
- Valid foreign key references
|
||||
- No duplicate keys
|
||||
- Data consistency
|
||||
|
||||
Args:
|
||||
db: Database session dependency.
|
||||
|
||||
Returns:
|
||||
dict: Integrity check results with issues found.
|
||||
"""
|
||||
try:
|
||||
# Convert async session to sync for the checker
|
||||
# Note: This is a temporary solution. In production,
|
||||
# consider implementing async version of integrity checker.
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
sync_session = Session(bind=db.sync_session.bind)
|
||||
|
||||
checker = DatabaseIntegrityChecker(sync_session)
|
||||
results = checker.check_all()
|
||||
|
||||
if results["total_issues"] > 0:
|
||||
logger.warning(
|
||||
f"Database integrity check found {results['total_issues']} "
|
||||
f"issues"
|
||||
)
|
||||
else:
|
||||
logger.info("Database integrity check passed")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"timestamp": None, # Add timestamp if needed
|
||||
"results": results,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Integrity check failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/integrity/repair")
|
||||
async def repair_database_integrity(
|
||||
db: AsyncSession = Depends(get_database_session),
|
||||
) -> Dict[str, Any]:
|
||||
"""Repair database integrity by removing orphaned records.
|
||||
|
||||
**Warning**: This operation will delete orphaned records permanently.
|
||||
|
||||
Args:
|
||||
db: Database session dependency.
|
||||
|
||||
Returns:
|
||||
dict: Repair results with count of records removed.
|
||||
"""
|
||||
try:
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
sync_session = Session(bind=db.sync_session.bind)
|
||||
|
||||
checker = DatabaseIntegrityChecker(sync_session)
|
||||
removed_count = checker.repair_orphaned_records()
|
||||
|
||||
logger.info(f"Removed {removed_count} orphaned records")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"removed_records": removed_count,
|
||||
"message": (
|
||||
f"Successfully removed {removed_count} orphaned records"
|
||||
),
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Integrity repair failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user