Spaces:
Sleeping
Sleeping
File size: 6,358 Bytes
fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 c18f351 fb5eec5 | 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #!/usr/bin/env python3
"""
MongoDB to Turso Migration Script
Migrates messages, mentorships, and mentorship_requests from MongoDB to Turso.
Run this script once to copy existing data.
Usage:
cd /Users/krishna./Desktop/Projects/opentriage/ai-engine
python scripts/migrate_to_turso.py
"""
import asyncio
import logging
import sys
from pathlib import Path
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
from dotenv import load_dotenv
load_dotenv()
import os
import libsql_experimental as libsql
# MongoDB imports
from config.database import db
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def get_turso_connection():
"""Get a direct Turso connection with FK checks disabled."""
url = os.environ.get('TURSO_DATABASE_URL', '')
auth_token = os.environ.get('TURSO_AUTH_TOKEN', '')
conn = libsql.connect(
"migration_opentriage.db",
sync_url=url,
auth_token=auth_token
)
conn.sync()
# Disable foreign key checks for migration
conn.execute("PRAGMA foreign_keys = OFF")
return conn
async def migrate_messages(conn):
"""Migrate all messages from MongoDB to Turso."""
logger.info("Migrating messages...")
cursor = db.messages.find({}, {"_id": 0})
messages = await cursor.to_list(length=None)
success_count = 0
for msg in messages:
try:
timestamp = msg.get('timestamp')
if timestamp and not isinstance(timestamp, str):
timestamp = timestamp.isoformat()
conn.execute(
"""
INSERT OR REPLACE INTO messages (id, sender_id, receiver_id, content, read, timestamp)
VALUES (?, ?, ?, ?, ?, ?)
""",
(
msg.get('id'),
msg.get('sender_id'),
msg.get('receiver_id'),
msg.get('content'),
1 if msg.get('read') else 0,
timestamp
)
)
conn.commit()
success_count += 1
except Exception as e:
logger.error(f"Failed to insert message {msg.get('id')}: {e}")
logger.info(f"Migrated {success_count}/{len(messages)} messages")
return success_count
async def migrate_mentorships(conn):
"""Migrate all mentorships from MongoDB to Turso."""
logger.info("Migrating mentorships...")
cursor = db.mentorships.find({}, {"_id": 0})
mentorships = await cursor.to_list(length=None)
success_count = 0
for m in mentorships:
try:
created_at = m.get('created_at')
if created_at and not isinstance(created_at, str):
created_at = created_at.isoformat()
conn.execute(
"""
INSERT OR REPLACE INTO mentorships
(id, mentor_id, mentor_username, mentee_id, mentee_username, status, created_at, disconnected_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
m.get('id'),
m.get('mentor_id'),
m.get('mentor_username'),
m.get('mentee_id'),
m.get('mentee_username'),
m.get('status', 'active'),
created_at,
m.get('disconnected_at')
)
)
conn.commit()
success_count += 1
except Exception as e:
logger.error(f"Failed to insert mentorship {m.get('id')}: {e}")
logger.info(f"Migrated {success_count}/{len(mentorships)} mentorships")
return success_count
async def migrate_mentorship_requests(conn):
"""Migrate all mentorship requests from MongoDB to Turso."""
logger.info("Migrating mentorship requests...")
cursor = db.mentorship_requests.find({}, {"_id": 0})
requests = await cursor.to_list(length=None)
success_count = 0
for req in requests:
try:
created_at = req.get('created_at')
if created_at and not isinstance(created_at, str):
created_at = created_at.isoformat()
conn.execute(
"""
INSERT OR REPLACE INTO mentorship_requests
(id, mentee_id, mentee_username, mentor_id, mentor_username, issue_id, message, status, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
req.get('id'),
req.get('mentee_id'),
req.get('mentee_username'),
req.get('mentor_id'),
req.get('mentor_username'),
req.get('issue_id'),
req.get('message'),
req.get('status', 'pending'),
created_at
)
)
conn.commit()
success_count += 1
except Exception as e:
logger.error(f"Failed to insert mentorship request {req.get('id')}: {e}")
logger.info(f"Migrated {success_count}/{len(requests)} mentorship requests")
return success_count
async def main():
"""Run the migration."""
logger.info("=" * 50)
logger.info("MongoDB to Turso Migration")
logger.info("=" * 50)
# Get connection with FK checks disabled
logger.info("Connecting to Turso (with FK checks disabled)...")
conn = get_turso_connection()
# Run migrations
msg_count = await migrate_messages(conn)
mentorship_count = await migrate_mentorships(conn)
request_count = await migrate_mentorship_requests(conn)
# Re-enable foreign key checks
conn.execute("PRAGMA foreign_keys = ON")
# Sync all changes to remote
logger.info("Syncing changes to remote Turso...")
conn.sync()
logger.info("=" * 50)
logger.info("Migration Complete!")
logger.info(f" Messages: {msg_count}")
logger.info(f" Mentorships: {mentorship_count}")
logger.info(f" Mentorship Requests: {request_count}")
logger.info("=" * 50)
if __name__ == "__main__":
asyncio.run(main())
|