Compare commits
3 commits
31f69856a6
...
475accc50c
Author | SHA1 | Date | |
---|---|---|---|
475accc50c | |||
50ee99fa88 | |||
6ee4b463e0 |
3 changed files with 56 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kosyncrs"
|
name = "kosyncrs"
|
||||||
version = "1.1.1"
|
version = "2.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
|
@ -28,14 +28,14 @@ Connect to the database as user *kosync* (with psql) and create the database tab
|
||||||
```sql
|
```sql
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
username TEXT NOT NULL,
|
username TEXT UNIQUE NOT NULL,
|
||||||
password TEXT
|
password TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE progresses (
|
CREATE TABLE progresses (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
user_id BIGINT,
|
user_id BIGINT,
|
||||||
document TEXT UNIQUE NOT NULL,
|
document TEXT NOT NULL,
|
||||||
progress TEXT NOT NULL,
|
progress TEXT NOT NULL,
|
||||||
percentage REAL NOT NULL,
|
percentage REAL NOT NULL,
|
||||||
device TEXT NOT NULL,
|
device TEXT NOT NULL,
|
||||||
|
|
52
python/redis2pg.py
Normal file
52
python/redis2pg.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
#!/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()
|
Loading…
Add table
Add a link
Reference in a new issue