forked from shuternay/likes_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_db.py
86 lines (66 loc) · 2.32 KB
/
move_db.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import initdb
import logging.config
import os
import sqlite3
from models import Message, Like
from project import settings
from project.db import db as peewee_db
logger = logging.getLogger('main')
DATABASE = os.environ.get('DATABASE')
def connect_db():
print(DATABASE)
rv = sqlite3.connect(DATABASE)
rv.row_factory = sqlite3.Row
return rv
sqlite_db = None
def get_db():
global sqlite_db
if sqlite_db is None:
sqlite_db = connect_db()
return sqlite_db
def move_db() -> None:
logging_settings = settings.LOGGING
logging_settings['loggers']['peewee']['level'] = 'INFO'
logging.config.dictConfig(settings.LOGGING)
sqlite_db = get_db()
initdb.create_tables()
with peewee_db:
cur = sqlite_db.execute('select count(*) AS cnt from messages')
messages_count = cur.fetchone()['cnt']
cur = sqlite_db.execute('select chat_id, message_id, user_id from messages')
logger.info('moving %s messages', messages_count)
for index, row in enumerate(cur):
if index % 100 == 0:
logger.info('%s messages...', index)
Message.get_or_create(
chat_tg_id=row['chat_id'],
message_tg_id=row['message_id'],
user_tg_id=row['user_id'],
)
cur = sqlite_db.execute('select count(*) AS cnt from likes')
likes_count = cur.fetchone()['cnt']
cur = sqlite_db.execute('select chat_id, message_id, user_id, like_type from likes')
logger.info('moving %s likes', likes_count)
for index, row in enumerate(cur):
if index % 100 == 0:
logger.info('%s likes...', index)
msg, _ = Message.get_or_create(
chat_tg_id=row['chat_id'],
message_tg_id=row['message_id'],
defaults={'user_tg_id': -1},
)
like = Like.filter(
Like.message_id == msg.id,
Like.user_tg_id == row['user_id'],
).first()
if like:
like.type = row['like_type']
like.save()
else:
Like.create(
message_id=msg.id,
user_tg_id=row['user_id'],
type=row['like_type'],
)
if __name__ == '__main__':
move_db()