From e2ea8fe42fe68636ae6a4f563c9900e22d77dff1 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 8 Jun 2024 15:53:02 +0100 Subject: [PATCH] migrate local user to V3 --- oml/setup.py | 21 +++++++++++++++++++++ oml/utils.py | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/oml/setup.py b/oml/setup.py index e676292..ee62cb6 100644 --- a/oml/setup.py +++ b/oml/setup.py @@ -423,6 +423,27 @@ def upgrade_db(old, new=None): )''') run_sql('CREATE UNIQUE INDEX IF NOT EXISTS user_metadata_index ON user_metadata(id, user_id)') run_sql('CREATE INDEX ix_user_metadata_data_hash ON user_metadata (data_hash)') + if old <= '20240608-1469-647a8b9': + old_hostname = os.path.join(settings.data_path, 'tor/hostname') + if os.path.exists(old_hostname): + with open(old_hostname) as fd: + OLD_USER_ID = fd.read().split('.')[0] + statements = [ + "UPDATE user SET id = '{nid}' WHERE id = '{oid}'", + "UPDATE list SET user_id = '{nid}' WHERE user_id = '{oid}'", + "UPDATE useritem SET user_id = '{nid}' WHERE user_id = '{oid}'", + "UPDATE changelog SET user_id = '{nid}' WHERE user_id = '{oid}'", + ] + run_sql([ + sql.format(oid=OLD_USER_ID, nid=settings.USER_ID) + for sql in statements + ]) + for ext in ('log', 'db', 'json'): + old_log = os.path.join(settings.data_path, 'peers/%s.%s' % (OLD_USER_ID, ext)) + new_log = os.path.join(settings.data_path, 'peers/%s.%s' % (USER_ID, ext)) + if os.path.exists(old_log) and not os.path.exists(new_log): + os.rename(old_log, new_log) + def create_default_lists(user_id=None): with db.session(): diff --git a/oml/utils.py b/oml/utils.py index 80c82dc..51295a4 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -596,3 +596,22 @@ def time_cache(max_age, maxsize=128, typed=False): return _wrapped return _decorator + +def migrate_userid(old_id, new_id): + from db import run_sql + import settings + statements = [ + "UPDATE user SET id = '{nid}' WHERE id = '{oid}'", + "UPDATE list SET user_id = '{nid}' WHERE user_id = '{oid}'", + "UPDATE useritem SET user_id = '{nid}' WHERE user_id = '{oid}'", + "UPDATE changelog SET user_id = '{nid}' WHERE user_id = '{oid}'", + ] + run_sql([ + sql.format(oid=old_id, nid=new_id) + for sql in statements + ]) + for ext in ('log', 'db', 'json'): + old_file = os.path.join(settings.data_path, 'peers/%s.%s' % (old_id, ext)) + new_file = os.path.join(settings.data_path, 'peers/%s.%s' % (new_id, ext)) + if os.path.exists(old_file) and not os.path.exists(new_file): + os.rename(old_file, new_file)