Spaces:
Sleeping
Sleeping
Commit ·
c18f351
1
Parent(s): fb5eec5
Add PR persistence feature
Browse files- requirements.txt +1 -1
- scripts/migrate_to_turso.py +112 -14
requirements.txt
CHANGED
|
@@ -44,5 +44,5 @@ asyncio-throttle>=1.0.2
|
|
| 44 |
PyJWT>=2.8.0
|
| 45 |
|
| 46 |
# Turso (libsql) Database
|
| 47 |
-
libsql-
|
| 48 |
pyspark
|
|
|
|
| 44 |
PyJWT>=2.8.0
|
| 45 |
|
| 46 |
# Turso (libsql) Database
|
| 47 |
+
libsql-experimental>=0.0.55
|
| 48 |
pyspark
|
scripts/migrate_to_turso.py
CHANGED
|
@@ -6,7 +6,7 @@ Migrates messages, mentorships, and mentorship_requests from MongoDB to Turso.
|
|
| 6 |
Run this script once to copy existing data.
|
| 7 |
|
| 8 |
Usage:
|
| 9 |
-
cd /Users/krishna./Desktop/Projects/
|
| 10 |
python scripts/migrate_to_turso.py
|
| 11 |
"""
|
| 12 |
|
|
@@ -21,8 +21,11 @@ sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
| 21 |
from dotenv import load_dotenv
|
| 22 |
load_dotenv()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
from config.database import db
|
| 25 |
-
from config.turso import turso_db
|
| 26 |
|
| 27 |
logging.basicConfig(
|
| 28 |
level=logging.INFO,
|
|
@@ -31,7 +34,25 @@ logging.basicConfig(
|
|
| 31 |
logger = logging.getLogger(__name__)
|
| 32 |
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"""Migrate all messages from MongoDB to Turso."""
|
| 36 |
logger.info("Migrating messages...")
|
| 37 |
|
|
@@ -40,14 +61,35 @@ async def migrate_messages():
|
|
| 40 |
|
| 41 |
success_count = 0
|
| 42 |
for msg in messages:
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
success_count += 1
|
|
|
|
|
|
|
| 45 |
|
| 46 |
logger.info(f"Migrated {success_count}/{len(messages)} messages")
|
| 47 |
return success_count
|
| 48 |
|
| 49 |
|
| 50 |
-
async def migrate_mentorships():
|
| 51 |
"""Migrate all mentorships from MongoDB to Turso."""
|
| 52 |
logger.info("Migrating mentorships...")
|
| 53 |
|
|
@@ -56,14 +98,38 @@ async def migrate_mentorships():
|
|
| 56 |
|
| 57 |
success_count = 0
|
| 58 |
for m in mentorships:
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
success_count += 1
|
|
|
|
|
|
|
| 61 |
|
| 62 |
logger.info(f"Migrated {success_count}/{len(mentorships)} mentorships")
|
| 63 |
return success_count
|
| 64 |
|
| 65 |
|
| 66 |
-
async def migrate_mentorship_requests():
|
| 67 |
"""Migrate all mentorship requests from MongoDB to Turso."""
|
| 68 |
logger.info("Migrating mentorship requests...")
|
| 69 |
|
|
@@ -72,8 +138,33 @@ async def migrate_mentorship_requests():
|
|
| 72 |
|
| 73 |
success_count = 0
|
| 74 |
for req in requests:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
success_count += 1
|
|
|
|
|
|
|
| 77 |
|
| 78 |
logger.info(f"Migrated {success_count}/{len(requests)} mentorship requests")
|
| 79 |
return success_count
|
|
@@ -85,14 +176,21 @@ async def main():
|
|
| 85 |
logger.info("MongoDB to Turso Migration")
|
| 86 |
logger.info("=" * 50)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
logger.info("
|
| 90 |
-
|
| 91 |
|
| 92 |
# Run migrations
|
| 93 |
-
msg_count = await migrate_messages()
|
| 94 |
-
mentorship_count = await migrate_mentorships()
|
| 95 |
-
request_count = await migrate_mentorship_requests()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
logger.info("=" * 50)
|
| 98 |
logger.info("Migration Complete!")
|
|
|
|
| 6 |
Run this script once to copy existing data.
|
| 7 |
|
| 8 |
Usage:
|
| 9 |
+
cd /Users/krishna./Desktop/Projects/opentriage/ai-engine
|
| 10 |
python scripts/migrate_to_turso.py
|
| 11 |
"""
|
| 12 |
|
|
|
|
| 21 |
from dotenv import load_dotenv
|
| 22 |
load_dotenv()
|
| 23 |
|
| 24 |
+
import os
|
| 25 |
+
import libsql_experimental as libsql
|
| 26 |
+
|
| 27 |
+
# MongoDB imports
|
| 28 |
from config.database import db
|
|
|
|
| 29 |
|
| 30 |
logging.basicConfig(
|
| 31 |
level=logging.INFO,
|
|
|
|
| 34 |
logger = logging.getLogger(__name__)
|
| 35 |
|
| 36 |
|
| 37 |
+
def get_turso_connection():
|
| 38 |
+
"""Get a direct Turso connection with FK checks disabled."""
|
| 39 |
+
url = os.environ.get('TURSO_DATABASE_URL', '')
|
| 40 |
+
auth_token = os.environ.get('TURSO_AUTH_TOKEN', '')
|
| 41 |
+
|
| 42 |
+
conn = libsql.connect(
|
| 43 |
+
"migration_opentriage.db",
|
| 44 |
+
sync_url=url,
|
| 45 |
+
auth_token=auth_token
|
| 46 |
+
)
|
| 47 |
+
conn.sync()
|
| 48 |
+
|
| 49 |
+
# Disable foreign key checks for migration
|
| 50 |
+
conn.execute("PRAGMA foreign_keys = OFF")
|
| 51 |
+
|
| 52 |
+
return conn
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
async def migrate_messages(conn):
|
| 56 |
"""Migrate all messages from MongoDB to Turso."""
|
| 57 |
logger.info("Migrating messages...")
|
| 58 |
|
|
|
|
| 61 |
|
| 62 |
success_count = 0
|
| 63 |
for msg in messages:
|
| 64 |
+
try:
|
| 65 |
+
timestamp = msg.get('timestamp')
|
| 66 |
+
if timestamp and not isinstance(timestamp, str):
|
| 67 |
+
timestamp = timestamp.isoformat()
|
| 68 |
+
|
| 69 |
+
conn.execute(
|
| 70 |
+
"""
|
| 71 |
+
INSERT OR REPLACE INTO messages (id, sender_id, receiver_id, content, read, timestamp)
|
| 72 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 73 |
+
""",
|
| 74 |
+
(
|
| 75 |
+
msg.get('id'),
|
| 76 |
+
msg.get('sender_id'),
|
| 77 |
+
msg.get('receiver_id'),
|
| 78 |
+
msg.get('content'),
|
| 79 |
+
1 if msg.get('read') else 0,
|
| 80 |
+
timestamp
|
| 81 |
+
)
|
| 82 |
+
)
|
| 83 |
+
conn.commit()
|
| 84 |
success_count += 1
|
| 85 |
+
except Exception as e:
|
| 86 |
+
logger.error(f"Failed to insert message {msg.get('id')}: {e}")
|
| 87 |
|
| 88 |
logger.info(f"Migrated {success_count}/{len(messages)} messages")
|
| 89 |
return success_count
|
| 90 |
|
| 91 |
|
| 92 |
+
async def migrate_mentorships(conn):
|
| 93 |
"""Migrate all mentorships from MongoDB to Turso."""
|
| 94 |
logger.info("Migrating mentorships...")
|
| 95 |
|
|
|
|
| 98 |
|
| 99 |
success_count = 0
|
| 100 |
for m in mentorships:
|
| 101 |
+
try:
|
| 102 |
+
created_at = m.get('created_at')
|
| 103 |
+
if created_at and not isinstance(created_at, str):
|
| 104 |
+
created_at = created_at.isoformat()
|
| 105 |
+
|
| 106 |
+
conn.execute(
|
| 107 |
+
"""
|
| 108 |
+
INSERT OR REPLACE INTO mentorships
|
| 109 |
+
(id, mentor_id, mentor_username, mentee_id, mentee_username, status, created_at, disconnected_at)
|
| 110 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
| 111 |
+
""",
|
| 112 |
+
(
|
| 113 |
+
m.get('id'),
|
| 114 |
+
m.get('mentor_id'),
|
| 115 |
+
m.get('mentor_username'),
|
| 116 |
+
m.get('mentee_id'),
|
| 117 |
+
m.get('mentee_username'),
|
| 118 |
+
m.get('status', 'active'),
|
| 119 |
+
created_at,
|
| 120 |
+
m.get('disconnected_at')
|
| 121 |
+
)
|
| 122 |
+
)
|
| 123 |
+
conn.commit()
|
| 124 |
success_count += 1
|
| 125 |
+
except Exception as e:
|
| 126 |
+
logger.error(f"Failed to insert mentorship {m.get('id')}: {e}")
|
| 127 |
|
| 128 |
logger.info(f"Migrated {success_count}/{len(mentorships)} mentorships")
|
| 129 |
return success_count
|
| 130 |
|
| 131 |
|
| 132 |
+
async def migrate_mentorship_requests(conn):
|
| 133 |
"""Migrate all mentorship requests from MongoDB to Turso."""
|
| 134 |
logger.info("Migrating mentorship requests...")
|
| 135 |
|
|
|
|
| 138 |
|
| 139 |
success_count = 0
|
| 140 |
for req in requests:
|
| 141 |
+
try:
|
| 142 |
+
created_at = req.get('created_at')
|
| 143 |
+
if created_at and not isinstance(created_at, str):
|
| 144 |
+
created_at = created_at.isoformat()
|
| 145 |
+
|
| 146 |
+
conn.execute(
|
| 147 |
+
"""
|
| 148 |
+
INSERT OR REPLACE INTO mentorship_requests
|
| 149 |
+
(id, mentee_id, mentee_username, mentor_id, mentor_username, issue_id, message, status, created_at)
|
| 150 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 151 |
+
""",
|
| 152 |
+
(
|
| 153 |
+
req.get('id'),
|
| 154 |
+
req.get('mentee_id'),
|
| 155 |
+
req.get('mentee_username'),
|
| 156 |
+
req.get('mentor_id'),
|
| 157 |
+
req.get('mentor_username'),
|
| 158 |
+
req.get('issue_id'),
|
| 159 |
+
req.get('message'),
|
| 160 |
+
req.get('status', 'pending'),
|
| 161 |
+
created_at
|
| 162 |
+
)
|
| 163 |
+
)
|
| 164 |
+
conn.commit()
|
| 165 |
success_count += 1
|
| 166 |
+
except Exception as e:
|
| 167 |
+
logger.error(f"Failed to insert mentorship request {req.get('id')}: {e}")
|
| 168 |
|
| 169 |
logger.info(f"Migrated {success_count}/{len(requests)} mentorship requests")
|
| 170 |
return success_count
|
|
|
|
| 176 |
logger.info("MongoDB to Turso Migration")
|
| 177 |
logger.info("=" * 50)
|
| 178 |
|
| 179 |
+
# Get connection with FK checks disabled
|
| 180 |
+
logger.info("Connecting to Turso (with FK checks disabled)...")
|
| 181 |
+
conn = get_turso_connection()
|
| 182 |
|
| 183 |
# Run migrations
|
| 184 |
+
msg_count = await migrate_messages(conn)
|
| 185 |
+
mentorship_count = await migrate_mentorships(conn)
|
| 186 |
+
request_count = await migrate_mentorship_requests(conn)
|
| 187 |
+
|
| 188 |
+
# Re-enable foreign key checks
|
| 189 |
+
conn.execute("PRAGMA foreign_keys = ON")
|
| 190 |
+
|
| 191 |
+
# Sync all changes to remote
|
| 192 |
+
logger.info("Syncing changes to remote Turso...")
|
| 193 |
+
conn.sync()
|
| 194 |
|
| 195 |
logger.info("=" * 50)
|
| 196 |
logger.info("Migration Complete!")
|