From 6ee4b463e07c907ca65e7263e58ede73eeb709cb Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 8 Jan 2025 14:19:13 +0100 Subject: [PATCH 1/3] more on readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2a864d9..838a9e2 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,14 @@ Connect to the database as user *kosync* (with psql) and create the database tab ```sql CREATE TABLE users ( id BIGSERIAL PRIMARY KEY, - username TEXT NOT NULL, + username TEXT UNIQUE NOT NULL, password TEXT ); CREATE TABLE progresses ( id BIGSERIAL PRIMARY KEY, user_id BIGINT, - document TEXT UNIQUE NOT NULL, + document TEXT NOT NULL, progress TEXT NOT NULL, percentage REAL NOT NULL, device TEXT NOT NULL, @@ -45,4 +45,4 @@ CREATE TABLE progresses ( REFERENCES users(id) ON DELETE CASCADE, UNIQUE (user_id, document) ); -``` \ No newline at end of file +``` From 50ee99fa8898e9333ed5ba1b558e0073d1d337ae Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 8 Jan 2025 14:19:25 +0100 Subject: [PATCH 2/3] Migration Skript (Redis -> PostgreSQL) --- python/redis2pg.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 python/redis2pg.py diff --git a/python/redis2pg.py b/python/redis2pg.py new file mode 100644 index 0000000..c96efcb --- /dev/null +++ b/python/redis2pg.py @@ -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() From 475accc50cad799af39bfe9339001ef2258b54d9 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 8 Jan 2025 14:23:46 +0100 Subject: [PATCH 3/3] bump version number --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a7cecfa..ec87483 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kosyncrs" -version = "1.1.1" +version = "2.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html