-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmigrate_session.py
43 lines (39 loc) · 1.03 KB
/
migrate_session.py
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
import aiosqlite
import asyncio
from async_pymongo import AsyncClient
mongodb_uri = "mongodb://127.0.0.1"
mongodb_dbname = "my_sessions"
sqlite_file_path = "/path/to/my_bot.session"
async def main():
print(f"Importing sessions from {sqlite_file_path} to {mongodb_dbname} database...")
db = AsyncClient(mongodb_uri)[mongodb_dbname]
conn = await aiosqlite.connect(sqlite_file_path)
cur = await conn.cursor()
await cur.execute("select * from sessions")
cols = list(map(lambda x: x[0], cur.description))
rows = await cur.fetchall()
count = 0
for row in rows:
data = {}
data["_id"] = count
i = 0
for col in cols:
data[col] = row[i]
i = i+1
await db.session.insert_one(data)
count = count+1
await cur.execute("select * from peers")
cols = list(map(lambda x: x[0], cur.description))
rows = await cur.fetchall()
for row in rows:
data = {}
i = 0
for col in cols:
if col == "id":
col = "_id"
data[col] = row[i]
i = i+1
await db.peers.insert_one(data)
print("Done")
await conn.close()
asyncio.run(main())