53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import psycopg2
|
||
|
import re
|
||
|
import redis
|
||
|
|
||
|
con = redis.Redis()
|
||
|
|
||
|
pgcon = psycopg2.connect("postgresql://kosync:mypassword@localhost/kosync")
|
||
|
pgcur = pgcon.cursor()
|
||
|
|
||
|
userkeys = con.keys('user:*:key')
|
||
|
|
||
|
for key in userkeys:
|
||
|
key = key.decode('utf-8')
|
||
|
match = re.search('user:(.*):key', key)
|
||
|
username = match.group(1)
|
||
|
password = con.get(key).decode('utf-8')
|
||
|
|
||
|
documentkeys = con.keys('user:' + username + ':document:*')
|
||
|
if len(documentkeys) == 0:
|
||
|
print("Skipped:", username)
|
||
|
continue
|
||
|
|
||
|
pgcur.execute("INSERT INTO users (username, password) VALUES (%s, %s) ON CONFLICT (username) DO UPDATE SET username = %s, password = %s",
|
||
|
(username, password, username, password))
|
||
|
|
||
|
pgcur.execute("SELECT id FROM users WHERE username = %s", (username,))
|
||
|
res = pgcur.fetchone()
|
||
|
user_id = res[0]
|
||
|
|
||
|
for dockey in documentkeys:
|
||
|
dockey = dockey.decode('utf-8')
|
||
|
|
||
|
match = re.search("user:.*:(.*)", dockey)
|
||
|
document = match.group(1)
|
||
|
|
||
|
docvals = con.hgetall(dockey)
|
||
|
|
||
|
progress = docvals[b"progress"].decode()
|
||
|
percentage = docvals[b"percentage"].decode()
|
||
|
device = docvals[b"device"].decode()
|
||
|
device_id = docvals[b"device_id"].decode()
|
||
|
timestamp = docvals[b"timestamp"].decode()
|
||
|
|
||
|
pgcur.execute("INSERT INTO progresses (user_id, document, progress, percentage, device, device_id, timestamp) VALUES (%s, %s, %s, %s, %s, %s, %s) ON CONFLICT (user_id, document) DO UPDATE SET user_id = %s, document = %s, progress = %s, percentage = %s, device = %s, device_id = %s, timestamp = %s",
|
||
|
(user_id, document, progress, percentage, device, device_id, timestamp, user_id, document, progress, percentage, device, device_id, timestamp))
|
||
|
|
||
|
pgcon.commit()
|
||
|
|
||
|
pgcur.close()
|
||
|
pgcon.close()
|