-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrate.py
68 lines (52 loc) · 1.96 KB
/
migrate.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
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
import asyncio
import os
import shutil
import aiosqlite
from dotenv import load_dotenv
load_dotenv()
CHAT_ID = os.getenv("CHAT_ID")
if CHAT_ID == "" or None:
print("Please fill the .env from .env.example first.")
exit(1)
async def main():
v3_to_v4 = await check_v3_to_v4()
if v3_to_v4:
print("Done! If there were no errors, you can now remove the 'static' folder and run the bot.")
else:
print("'static' folder is missing. Perhaps you have already migrated?")
async def check_v3_to_v4():
run_dir = os.path.dirname(os.path.realpath(__file__))
static_dir = os.path.join(run_dir, 'static')
if os.path.exists(static_dir):
print("Found v3 files. Start migrating to v4.")
await v3_to_v4()
return True
else:
return False
async def v3_to_v4():
run_dir = os.path.dirname(os.path.realpath(__file__))
static_dir = os.path.join(run_dir, 'static')
data_dir = os.path.join(run_dir, 'data')
# static -> data
shutil.copytree(static_dir, data_dir, dirs_exist_ok=True)
# Update older databases for compatibility with new versions.
# msg_stats (messages): Adds 'chat_id' column and assigns it to 'CHAT_ID' for old entries.
async with aiosqlite.connect('./data/msg_stats.db') as db:
await db.execute('ALTER TABLE messages ADD COLUMN chat_id INTEGER')
await db.commit()
await db.execute('''
UPDATE messages SET chat_id = ?
WHERE chat_id IS NULL
''', (CHAT_ID,))
await db.commit()
# reminders: Drops the 'username' column, and renames 'remind_time' column to 'time'.
async with aiosqlite.connect('./data/reminders.db') as db:
await db.execute('ALTER TABLE reminders DROP COLUMN username')
await db.commit()
await db.execute('''
ALTER TABLE reminders
RENAME COLUMN remind_time to time
''')
await db.commit()
if __name__ == '__main__':
asyncio.run(main())