Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-26 08:40:25

0001 """Store the composed (tag-based) dataset identity in its own indexed column.
0002 
0003 Previously ``Dataset.composed_name`` was a property that rebuilt the name from
0004 the tag FKs on every access, forcing every list/detail view to prefetch five
0005 tag joins just to render or link a task. The name is now a stored column, kept
0006 current on every ``Dataset.save()`` and read directly. This backfill replicates
0007 ``build_dataset_name`` for the existing rows (historical models carry no custom
0008 methods); ``tag_label`` is a stored field on each tag model, so the composition
0009 is reproduced exactly.
0010 """
0011 from django.db import migrations, models
0012 
0013 
0014 def backfill_composed_name(apps, schema_editor):
0015     Dataset = apps.get_model('pcs', 'Dataset')
0016     rows = list(
0017         Dataset.objects.select_related(
0018             'physics_tag', 'evgen_tag', 'simu_tag', 'reco_tag', 'background_tag'
0019         )
0020     )
0021     for ds in rows:
0022         name = (
0023             f"{ds.scope}.{ds.detector_version}.{ds.detector_config}"
0024             f".{ds.physics_tag.tag_label}.{ds.evgen_tag.tag_label}"
0025             f".{ds.simu_tag.tag_label}.{ds.reco_tag.tag_label}"
0026         )
0027         if ds.background_tag_id:
0028             name = f"{name}.{ds.background_tag.tag_label}"
0029         if ds.sample_name:
0030             name = f"{name}.{ds.sample_name}"
0031         ds.composed_name = name
0032     if rows:
0033         Dataset.objects.bulk_update(rows, ['composed_name'], batch_size=500)
0034 
0035 
0036 class Migration(migrations.Migration):
0037 
0038     dependencies = [
0039         ('pcs', '0016_dataset_sample_name'),
0040     ]
0041 
0042     operations = [
0043         migrations.AddField(
0044             model_name='dataset',
0045             name='composed_name',
0046             field=models.CharField(blank=True, db_index=True, default='', max_length=255),
0047         ),
0048         migrations.RunPython(backfill_composed_name, migrations.RunPython.noop),
0049     ]