diff --git a/core/migrations/0002_update_worker_id_numbers.py b/core/migrations/0002_update_worker_id_numbers.py new file mode 100644 index 0000000..5b2e6a5 --- /dev/null +++ b/core/migrations/0002_update_worker_id_numbers.py @@ -0,0 +1,65 @@ +# === DATA MIGRATION: Update Worker ID Numbers === +# One-time migration to set real SA ID numbers from the workers_list.xlsx file. +# Matches workers by first name + surname (case-insensitive contains). +# Safe: if a worker isn't found, it's skipped. If id_number is already correct, no change. + +from django.db import migrations +from django.db.models import Q + + +# Worker ID data from workers_list.xlsx +# Format: (first_name_part, surname, id_number) +# We search for workers whose name contains BOTH the first name part AND the surname +WORKER_ID_DATA = [ + ('Mpho', 'Nkoana', '9811125984089'), + ('Richard', 'Moleko', '0003185071085'), + ('Fikile', 'Masimula', '8606305407088'), + ('Clifford', 'Selemela', '0104205798085'), + ('Shane', 'Malobela', '9807046054085'), + ('Jimmy', 'Moleko', '0101176105084'), + ('Johannes', 'Laka', '9809066044087'), + ('Tumelo', 'Sinugo', '9009055943080'), + ('Goitsimang', 'Moleko', '0403135542068'), + ('Sello', 'Matloa', '0407046184088'), + ('Aphiwe', 'Dobe', '9212236112084'), + ('Tshepo', 'Moganedi', '8112175417083'), +] + + +def update_id_numbers(apps, schema_editor): + """Update worker ID numbers from the Excel spreadsheet data.""" + Worker = apps.get_model('core', 'Worker') + + for first_name, surname, id_number in WORKER_ID_DATA: + # Find worker whose name contains both the first name and surname + # This handles cases like "Soldier Aphiwe Dobe" matching ("Aphiwe", "Dobe") + # or "Clifford Jan Bobby Selemela" matching ("Clifford", "Selemela") + matches = Worker.objects.filter( + Q(name__icontains=first_name) & Q(name__icontains=surname) + ) + + if matches.count() == 1: + worker = matches.first() + worker.id_number = id_number + worker.save(update_fields=['id_number']) + elif matches.count() > 1: + # Multiple matches — skip to avoid updating the wrong worker + # (shouldn't happen with first name + surname combo) + pass + # If no match found, skip silently — worker might not exist in this env + + +def reverse_id_numbers(apps, schema_editor): + """Reverse is a no-op — we can't restore old ID numbers.""" + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ] + + operations = [ + migrations.RunPython(update_id_numbers, reverse_id_numbers), + ]