Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions isic/ingest/migrations/0043_alter_rcmcase_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 5.2.3 on 2026-06-09 21:07

from django.db import migrations, models
from django.db.models import F, Value
from django.db.models.functions import Concat

import isic.ingest.models.rcm_case


def add_prefix(apps, schema_editor):
Accession = apps.get_model("ingest", "Accession")
RcmCase = apps.get_model("ingest", "RcmCase")
MetadataVersion = apps.get_model("ingest", "MetadataVersion")

Accession.objects.filter(rcm_case__isnull=False).exclude(
rcm_case__id__startswith="IRCM_"
).update(rcm_case_id=Concat(Value("IRCM_"), F("rcm_case_id")))

RcmCase.objects.exclude(id__startswith="IRCM_").update(id=Concat(Value("IRCM_"), F("id")))

# MetadataVersion snapshots store the public (generated) id under
# rcm_case->>'external'. The 'internal' key holds the internal id and is
# unchanged.
metadata_versions = MetadataVersion.objects.filter(rcm_case__has_key="external").exclude(
rcm_case__external__startswith="IRCM_"
)
to_update = []
for metadata_version in metadata_versions.iterator():
metadata_version.rcm_case["external"] = f"IRCM_{metadata_version.rcm_case['external']}"
to_update.append(metadata_version)
MetadataVersion.objects.bulk_update(to_update, ["rcm_case"], batch_size=1000)


class Migration(migrations.Migration):
dependencies = [
("ingest", "0042_blob_verbose_names"),
]

operations = [
migrations.AlterField(
model_name="rcmcase",
name="id",
field=models.CharField(
default=isic.ingest.models.rcm_case._default_id, # noqa: SLF001
max_length=12,
primary_key=True,
serialize=False,
),
),
migrations.RunPython(add_prefix, migrations.RunPython.noop, elidable=True),
]
4 changes: 2 additions & 2 deletions isic/ingest/models/rcm_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def _default_id():
while True:
rcm_case_id = f"{secrets.randbelow(9999999):07}"
rcm_case_id = f"IRCM_{secrets.randbelow(9999999):07}"
# This has a race condition, so the actual creation should be retried or wrapped
# in a select for update on the rcm_case table
if not RcmCase.objects.filter(id=rcm_case_id).exists():
Expand All @@ -17,7 +17,7 @@ class RcmCase(models.Model):
id = models.CharField(
primary_key=True,
default=_default_id,
max_length=7,
max_length=12,
)
cohort = models.ForeignKey("Cohort", on_delete=models.CASCADE, related_name="rcm_cases")
private_rcm_case_id = models.CharField(max_length=255)
Expand Down