Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-12 09:36:15

0001 import django.db.models.deletion
0002 from django.db import migrations, models
0003 
0004 
0005 def populate_fastmon_file(apps, schema_editor):
0006     TFSlice = apps.get_model('monitor_app', 'TFSlice')
0007     FastMonFile = apps.get_model('monitor_app', 'FastMonFile')
0008 
0009     fastmon_id_by_tf_filename = dict(
0010         FastMonFile.objects.values_list('tf_filename', 'tf_file_id')
0011     )
0012 
0013     unmatched = []
0014     for slice_obj in TFSlice.objects.all():
0015         fastmon_id = fastmon_id_by_tf_filename.get(slice_obj.tf_filename)
0016         if fastmon_id is None:
0017             unmatched.append(slice_obj.pk)
0018             continue
0019         slice_obj.fastmon_file_id = fastmon_id
0020         slice_obj.save(update_fields=['fastmon_file'])
0021 
0022     if unmatched:
0023         raise RuntimeError(
0024             f"Cannot migrate {len(unmatched)} TFSlice row(s) with no matching "
0025             f"FastMonFile by tf_filename (ids: "
0026             f"{unmatched[:20]}{'...' if len(unmatched) > 20 else ''})"
0027         )
0028 
0029 
0030 def restore_flat_filenames(apps, schema_editor):
0031     TFSlice = apps.get_model('monitor_app', 'TFSlice')
0032     for slice_obj in TFSlice.objects.select_related('fastmon_file', 'fastmon_file__stf_file').all():
0033         slice_obj.tf_filename = slice_obj.fastmon_file.tf_filename
0034         slice_obj.stf_filename = slice_obj.fastmon_file.stf_file.stf_filename
0035         slice_obj.save(update_fields=['tf_filename', 'stf_filename'])
0036 
0037 
0038 class Migration(migrations.Migration):
0039 
0040     dependencies = [
0041         ('monitor_app', '0004_capcomnotice'),
0042     ]
0043 
0044     operations = [
0045         migrations.RemoveIndex(
0046             model_name='tfslice',
0047             name='swf_tf_slic_stf_fil_45fc7b_idx',
0048         ),
0049         migrations.AlterUniqueTogether(
0050             name='tfslice',
0051             unique_together=set(),
0052         ),
0053         migrations.AddField(
0054             model_name='tfslice',
0055             name='fastmon_file',
0056             field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tf_slices', to='monitor_app.fastmonfile'),
0057         ),
0058         migrations.RunPython(populate_fastmon_file, restore_flat_filenames),
0059         migrations.AlterField(
0060             model_name='tfslice',
0061             name='fastmon_file',
0062             field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tf_slices', to='monitor_app.fastmonfile'),
0063         ),
0064         migrations.AddIndex(
0065             model_name='tfslice',
0066             index=models.Index(fields=['fastmon_file', 'status'], name='swf_tf_slic_fastmon_status_idx'),
0067         ),
0068         migrations.AlterUniqueTogether(
0069             name='tfslice',
0070             unique_together={('fastmon_file', 'slice_id')},
0071         ),
0072     ]