From 50ee99fa8898e9333ed5ba1b558e0073d1d337ae Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 8 Jan 2025 14:19:25 +0100 Subject: [PATCH] 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()