Spaces:
Running
Running
File size: 2,428 Bytes
6da2710 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#!/usr/bin/env python3
"""
Cleanup script to fix corrupted conversation participants
Finds conversations where participants is a string instead of an array and fixes them
"""
import asyncio
import sys
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from app.database import connect_db, disconnect_db, get_db
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def fix_corrupted_conversations():
"""Fix conversations with participants as string instead of array"""
try:
# Connect to database
await connect_db()
db = await get_db()
# Find conversations where participants is a string (corrupted)
corrupted = await db.conversations.find({
"participants": {"$type": "string"} # MongoDB type check
}).to_list(length=None)
logger.info(f"Found {len(corrupted)} corrupted conversations")
if not corrupted:
logger.info("No corrupted conversations found. Database is clean!")
return
# Fix each corrupted conversation
fixed_count = 0
for conv in corrupted:
conv_id = conv["_id"]
old_participants = conv["participants"]
logger.info(f"Fixing conversation {conv_id}: participants = '{old_participants}'")
# Delete the corrupted conversation
# (We'll let the system recreate it properly when needed)
result = await db.conversations.delete_one({"_id": conv_id})
if result.deleted_count > 0:
fixed_count += 1
logger.info(f" ✅ Deleted corrupted conversation {conv_id}")
else:
logger.error(f" ❌ Failed to delete conversation {conv_id}")
logger.info(f"\n{'='*60}")
logger.info(f"Cleanup complete!")
logger.info(f"Fixed {fixed_count} corrupted conversations")
logger.info(f"They will be recreated correctly when alerts are sent")
logger.info(f"{'='*60}\n")
except Exception as e:
logger.error(f"Cleanup failed: {e}")
raise
finally:
await disconnect_db()
if __name__ == "__main__":
logger.info("Starting conversation cleanup...")
asyncio.run(fix_corrupted_conversations())
|