|
| 1 | +# Generated by Django 4.2.27 on 2026-01-19 11:57 |
| 2 | + |
| 3 | +from django.db import migrations, models |
| 4 | + |
| 5 | + |
| 6 | +def forwards_populate_audit_trail_fks(apps, schema_editor): |
| 7 | + DirectAwardAuditTrail = apps.get_model('directaward', 'DirectAwardAuditTrail') |
| 8 | + DirectAward = apps.get_model('directaward', 'DirectAward') |
| 9 | + BadgeClass = apps.get_model('issuer', 'BadgeClass') |
| 10 | + |
| 11 | + for audit in DirectAwardAuditTrail.objects.all().iterator(): |
| 12 | + if audit.direct_award_entity_id and not audit.direct_award: |
| 13 | + audit.direct_award = ( |
| 14 | + DirectAward.objects |
| 15 | + .filter(entity_id=audit.direct_award_entity_id) |
| 16 | + .first() |
| 17 | + ) |
| 18 | + |
| 19 | + if audit.badgeclass_entity_id and not audit.badgeclass: |
| 20 | + audit.badgeclass = ( |
| 21 | + BadgeClass.objects |
| 22 | + .filter(entity_id=audit.badgeclass_entity_id) |
| 23 | + .first() |
| 24 | + ) |
| 25 | + |
| 26 | + audit.save(update_fields=['direct_award', 'badgeclass']) |
| 27 | + |
| 28 | + |
| 29 | +def backwards_restore_entity_ids(apps, schema_editor): |
| 30 | + DirectAwardAuditTrail = apps.get_model('directaward', 'DirectAwardAuditTrail') |
| 31 | + |
| 32 | + for audit in DirectAwardAuditTrail.objects.all().iterator(): |
| 33 | + if audit.direct_award and not audit.direct_award_entity_id: |
| 34 | + audit.direct_award_entity_id = audit.direct_award.entity_id |
| 35 | + |
| 36 | + if audit.badgeclass and not audit.badgeclass_entity_id: |
| 37 | + audit.badgeclass_entity_id = audit.badgeclass.entity_id |
| 38 | + |
| 39 | + audit.save(update_fields=[ |
| 40 | + 'direct_award_entity_id', |
| 41 | + 'badgeclass_entity_id', |
| 42 | + ]) |
| 43 | + |
| 44 | +class Migration(migrations.Migration): |
| 45 | + |
| 46 | + dependencies = [ |
| 47 | + ('directaward', '0023_directawardbundle_direct_award_removed_count'), |
| 48 | + ('issuer', |
| 49 | + '0117_rename_badgeinstance_recipient_identifier_badgeclass_revoked_issuer_badg_recipie_6a2cd8_idx_and_more'), |
| 50 | + ] |
| 51 | + |
| 52 | + operations = [ |
| 53 | + migrations.RenameField( |
| 54 | + model_name='directawardaudittrail', |
| 55 | + old_name='badgeclass_id', |
| 56 | + new_name='badgeclass_entity_id', |
| 57 | + ), |
| 58 | + migrations.RenameField( |
| 59 | + model_name='directawardaudittrail', |
| 60 | + old_name='direct_award_id', |
| 61 | + new_name='direct_award_entity_id', |
| 62 | + ), |
| 63 | + migrations.AddField( |
| 64 | + model_name='directawardaudittrail', |
| 65 | + name='badgeclass', |
| 66 | + field=models.ForeignKey(blank=True, null=True, on_delete=models.deletion.SET_NULL, to='issuer.badgeclass'), |
| 67 | + ), |
| 68 | + migrations.AddField( |
| 69 | + model_name='directawardaudittrail', |
| 70 | + name='direct_award', |
| 71 | + field=models.ForeignKey(blank=True, null=True, on_delete=models.deletion.SET_NULL, to='directaward.directaward'), |
| 72 | + ), |
| 73 | + migrations.RunPython( |
| 74 | + forwards_populate_audit_trail_fks, |
| 75 | + backwards_restore_entity_ids, |
| 76 | + ), |
| 77 | + migrations.RemoveField( |
| 78 | + model_name='directawardaudittrail', |
| 79 | + name='badgeclass_entity_id', |
| 80 | + ), |
| 81 | + migrations.RemoveField( |
| 82 | + model_name='directawardaudittrail', |
| 83 | + name='direct_award_entity_id', |
| 84 | + ), |
| 85 | + ] |
0 commit comments