-
Notifications
You must be signed in to change notification settings - Fork 6
/
migrate-db.py
50 lines (42 loc) · 1.78 KB
/
migrate-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
from telegram.ext import PicklePersistence
import psycopg2, psycopg2.extras
import sys
data = PicklePersistence(filename="lokisnbot.data", store_user_data=True, store_chat_data=False, on_flush=True)
pgsql = psycopg2.connect(dbname='lokisnbot', connection_factory=psycopg2.extras.LoggingConnection)
pgsql.initialize(sys.stderr)
for telegram_id, d in data.get_user_data().items():
cur = pgsql.cursor()
cur.execute("INSERT INTO users (telegram_id) VALUES (%s) RETURNING id", (telegram_id,))
uid = cur.fetchone()[0]
for sn in (d['sn'] if 'sn' in d else []):
sn_row = {
'pubkey': None,
'active': False,
'complete': False,
'expires_soon': False, # True now, but was false before the PGSQL rewrite
'last_contributions': None,
'last_reward_block_height': None,
'alias': None,
'note': None,
'notified_dereg': False,
'notified_uptime_age': None,
'rewards': False,
'expiry_notified': None,
'notified_age': None,
}
if 'lrbh' in sn:
sn['last_reward_block_height'] = sn['lrbh']
for k in sn_row.keys():
if k in sn:
sn_row[k] = sn[k]
ins_cols, ins_vals = ['uid'], [uid]
for k, v in sn_row.items():
ins_cols.append(k)
ins_vals.append(v)
ins_cols = ", ".join(ins_cols)
ins_vals = tuple(ins_vals)
cur.execute("INSERT INTO service_nodes (" + ins_cols + ") VALUES %s",
(ins_vals,))
for w in (d['wallets'] if 'wallets' in d else set()):
cur.execute("INSERT INTO wallet_prefixes (uid, wallet) VALUES (%s, %s)", (uid, w))
pgsql.commit()