From 2e9d92d9c79c2444214d70e61b12c4b8df3a2c0e Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 5 Apr 2024 11:38:52 -0400 Subject: [PATCH] [scrobbles] Add json log migration script --- .../commands/migrate_page_to_scrobbledpage.py | 2 +- .../commands/migrate_scrobble_log_to_json.py | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 vrobbler/apps/scrobbles/management/commands/migrate_scrobble_log_to_json.py diff --git a/vrobbler/apps/scrobbles/management/commands/migrate_page_to_scrobbledpage.py b/vrobbler/apps/scrobbles/management/commands/migrate_page_to_scrobbledpage.py index c171774..e10715c 100644 --- a/vrobbler/apps/scrobbles/management/commands/migrate_page_to_scrobbledpage.py +++ b/vrobbler/apps/scrobbles/management/commands/migrate_page_to_scrobbledpage.py @@ -1,5 +1,5 @@ from books.models import Book -from django.core.management.base import BaseCommand, no_translations +from django.core.management.base import BaseCommand from scrobbles.models import ScrobbledPage diff --git a/vrobbler/apps/scrobbles/management/commands/migrate_scrobble_log_to_json.py b/vrobbler/apps/scrobbles/management/commands/migrate_scrobble_log_to_json.py new file mode 100644 index 0000000..bf36391 --- /dev/null +++ b/vrobbler/apps/scrobbles/management/commands/migrate_scrobble_log_to_json.py @@ -0,0 +1,48 @@ +from django.core.management.base import BaseCommand +from scrobbles.models import Scrobble + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument( + "--commit", + action="store_true", + help="Actually migrate data", + ) + + def handle(self, *args, **options): + dry_run = True + if options["commit"]: + dry_run = False + + scrobbles_with_logs = ( + Scrobble.objects.filter(scrobble_log__isnull=False) + .exclude(scrobble_log="") + .exclude(scrobble_log="\n") + ) + updated_scrobble_count = 0 + + for scrobble in scrobbles_with_logs: + if "\n" in scrobble.scrobble_log: + lines = scrobble.scrobble_log.split("\n") + else: + lines = [scrobble.scrobble_log] + + old_data = {} + for line_num, line in enumerate(lines): + if line_num == 0 or line == "": + continue + old_data[str(line_num)] = line + + if old_data: + scrobble.scrobble_log = {"legacy_data": old_data} + updated_scrobble_count += 1 + + if not dry_run: + scrobble.save(update_fields="scrobble_log") + else: + print( + f"Scrobble {scrobble} scrobble_log updated to {old_data}" + ) + + print(f"Migrated scrobble logs for {updated_scrobble_count} scrobbles")