|
| 1 | +"""Convert dataset authors from old string format to new object format. |
| 2 | +
|
| 3 | +Old format: ["Author One", "Author Two"] (list of strings) |
| 4 | +New format: [{"name": "Author One", "orcid_id": ""}, ...] (list of dicts) |
| 5 | +
|
| 6 | +This is a one-time data fix for datasets that were created or versioned |
| 7 | +before the model-level auto-normalization was added to Dataset.save(). |
| 8 | +""" |
| 9 | + |
| 10 | +from django.core.management.base import BaseCommand |
| 11 | +from loguru import logger |
| 12 | + |
| 13 | +from sds_gateway.api_methods.models import Dataset |
| 14 | + |
| 15 | + |
| 16 | +class Command(BaseCommand): |
| 17 | + help = ( |
| 18 | + "Convert dataset authors from old string format ['Name'] " |
| 19 | + "to new object format [{'name': 'Name', 'orcid_id': ''}]" |
| 20 | + ) |
| 21 | + |
| 22 | + def add_arguments(self, parser): |
| 23 | + parser.add_argument( |
| 24 | + "--dry-run", |
| 25 | + action="store_true", |
| 26 | + help="Only count affected datasets without modifying them", |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--uuid", |
| 30 | + type=str, |
| 31 | + help="Migrate only a specific dataset by UUID", |
| 32 | + ) |
| 33 | + |
| 34 | + def handle(self, *args, **options): |
| 35 | + dry_run = options["dry_run"] |
| 36 | + specific_uuid = options.get("uuid") |
| 37 | + |
| 38 | + # Query datasets with non-empty authors |
| 39 | + queryset = Dataset.objects.filter(authors__isnull=False).exclude(authors="") |
| 40 | + if specific_uuid: |
| 41 | + queryset = queryset.filter(uuid=specific_uuid) |
| 42 | + |
| 43 | + total = queryset.count() |
| 44 | + updated = 0 |
| 45 | + skipped = 0 |
| 46 | + errors = 0 |
| 47 | + |
| 48 | + self.stdout.write(f"Scanning {total} dataset(s) with authors...") |
| 49 | + |
| 50 | + for dataset in queryset: |
| 51 | + try: |
| 52 | + # from_db already deserialized authors from JSON string to Python object |
| 53 | + authors = dataset.authors |
| 54 | + |
| 55 | + # Skip if not a list |
| 56 | + if not isinstance(authors, list): |
| 57 | + skipped += 1 |
| 58 | + continue |
| 59 | + |
| 60 | + # Skip empty lists |
| 61 | + if not authors: |
| 62 | + skipped += 1 |
| 63 | + continue |
| 64 | + |
| 65 | + # Skip if already in new format (list of dicts with 'name' key) |
| 66 | + if isinstance(authors[0], dict): |
| 67 | + skipped += 1 |
| 68 | + continue |
| 69 | + |
| 70 | + # Convert old format: list of strings → list of dicts |
| 71 | + if isinstance(authors[0], str): |
| 72 | + new_authors = [ |
| 73 | + {"name": author, "orcid_id": ""} |
| 74 | + for author in authors |
| 75 | + if isinstance(author, str) |
| 76 | + ] |
| 77 | + |
| 78 | + if not new_authors: |
| 79 | + logger.warning( |
| 80 | + f"Dataset {dataset.uuid}: no valid author strings found, " |
| 81 | + "skipping", |
| 82 | + ) |
| 83 | + skipped += 1 |
| 84 | + continue |
| 85 | + |
| 86 | + if dry_run: |
| 87 | + logger.info( |
| 88 | + f"[DRY-RUN] Would convert dataset {dataset.uuid}: " |
| 89 | + f"{len(authors)} author(s) in old format" |
| 90 | + ) |
| 91 | + else: |
| 92 | + dataset.authors = new_authors |
| 93 | + dataset.save(update_fields=["authors"]) |
| 94 | + logger.info( |
| 95 | + f"Converted dataset {dataset.uuid}: " |
| 96 | + f"{len(authors)} author(s) → new format" |
| 97 | + ) |
| 98 | + updated += 1 |
| 99 | + else: |
| 100 | + # Unexpected format |
| 101 | + logger.warning( |
| 102 | + f"Dataset {dataset.uuid}: unexpected author type " |
| 103 | + f"{type(authors[0]).__name__}, skipping" |
| 104 | + ) |
| 105 | + skipped += 1 |
| 106 | + |
| 107 | + except Exception as e: # noqa: BLE001 # intentional: continue processing remaining datasets |
| 108 | + logger.error(f"Error processing dataset {dataset.uuid}: {e}") |
| 109 | + errors += 1 |
| 110 | + |
| 111 | + self.stdout.write( |
| 112 | + f"Done: {updated} updated, {skipped} skipped, {errors} errors" |
| 113 | + + (" (dry run)" if dry_run else "") |
| 114 | + ) |
0 commit comments