|
| 1 | +# Generated by Django 4.2.19 on 2025-08-07 17:25 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | + |
| 6 | +def create_missing_tags_and_map_topics(apps, schema_editor): |
| 7 | + """ |
| 8 | + Create missing tags and map existing topics to appropriate tags |
| 9 | + """ |
| 10 | + Tag = apps.get_model("commitfest", "Tag") |
| 11 | + Patch = apps.get_model("commitfest", "Patch") |
| 12 | + Topic = apps.get_model("commitfest", "Topic") |
| 13 | + |
| 14 | + # Create missing tags |
| 15 | + Tag.objects.get_or_create( |
| 16 | + name="SQL Commands", |
| 17 | + defaults={ |
| 18 | + "color": "#198754", |
| 19 | + "description": "SQL command implementation and enhancement", |
| 20 | + }, |
| 21 | + ) |
| 22 | + |
| 23 | + Tag.objects.get_or_create( |
| 24 | + name="System Administration", |
| 25 | + defaults={ |
| 26 | + "color": "#495057", |
| 27 | + "description": "System administration and configuration", |
| 28 | + }, |
| 29 | + ) |
| 30 | + |
| 31 | + # Topic to Tag mapping |
| 32 | + topic_tag_mapping = { |
| 33 | + "Testing": "Testing", |
| 34 | + "Refactoring": "Refactoring Only", |
| 35 | + "Documentation": "Docs Only", |
| 36 | + "Code Comments": "Comments Only", |
| 37 | + "Bug Fixes": "Bugfix", |
| 38 | + "Performance": "Performance", |
| 39 | + "Security": "Security", |
| 40 | + "Monitoring & Control": "Monitoring", |
| 41 | + "Procedural Languages": "PL/pgSQL", |
| 42 | + "Replication & Recovery": "Physical Replication", |
| 43 | + "Clients": "libpq", |
| 44 | + "SQL Commands": "SQL Commands", |
| 45 | + "System Administration": "System Administration", |
| 46 | + # 'Miscellaneous' and 'Server Features' are left untagged |
| 47 | + } |
| 48 | + |
| 49 | + # Apply tags to existing patches based on their topics |
| 50 | + for topic_name, tag_name in topic_tag_mapping.items(): |
| 51 | + try: |
| 52 | + topic = Topic.objects.get(topic=topic_name) |
| 53 | + tag = Tag.objects.get(name=tag_name) |
| 54 | + |
| 55 | + # Get all patches with this topic |
| 56 | + patches_with_topic = Patch.objects.filter(topic=topic) |
| 57 | + |
| 58 | + # Add the corresponding tag to each patch |
| 59 | + for patch in patches_with_topic: |
| 60 | + patch.tags.add(tag) |
| 61 | + |
| 62 | + print( |
| 63 | + f"Mapped {patches_with_topic.count()} patches from topic '{topic_name}' to tag '{tag_name}'" |
| 64 | + ) |
| 65 | + |
| 66 | + except Topic.DoesNotExist: |
| 67 | + print(f"Topic '{topic_name}' not found, skipping...") |
| 68 | + except Tag.DoesNotExist: |
| 69 | + print(f"Tag '{tag_name}' not found, skipping...") |
| 70 | + |
| 71 | + |
| 72 | +def reverse_mapping(apps, schema_editor): |
| 73 | + """ |
| 74 | + Reverse the migration by removing topic-based tags from patches |
| 75 | + """ |
| 76 | + Tag = apps.get_model("commitfest", "Tag") |
| 77 | + |
| 78 | + # List of tags that were added by this migration |
| 79 | + topic_based_tag_names = [ |
| 80 | + "Testing", |
| 81 | + "Refactoring Only", |
| 82 | + "Docs Only", |
| 83 | + "Comments Only", |
| 84 | + "Bugfix", |
| 85 | + "Performance", |
| 86 | + "Security", |
| 87 | + "Monitoring", |
| 88 | + "PL/pgSQL", |
| 89 | + "Physical Replication", |
| 90 | + "libpq", |
| 91 | + "SQL Commands", |
| 92 | + "System Administration", |
| 93 | + ] |
| 94 | + |
| 95 | + for tag_name in topic_based_tag_names: |
| 96 | + try: |
| 97 | + tag = Tag.objects.get(name=tag_name) |
| 98 | + # Remove this tag from all patches |
| 99 | + tag.patches.clear() |
| 100 | + except Tag.DoesNotExist: |
| 101 | + pass |
| 102 | + |
| 103 | + |
| 104 | +class Migration(migrations.Migration): |
| 105 | + dependencies = [ |
| 106 | + ("commitfest", "0014_add_paused_cfbot_task_state"), |
| 107 | + ] |
| 108 | + |
| 109 | + operations = [ |
| 110 | + migrations.RunPython( |
| 111 | + create_missing_tags_and_map_topics, |
| 112 | + reverse_mapping, |
| 113 | + ), |
| 114 | + ] |
0 commit comments