|
6 | 6 | def create_missing_tags_and_map_topics(apps, schema_editor): |
7 | 7 | """ |
8 | 8 | Create missing tags and map existing topics to appropriate tags. |
9 | | - Only applies to patches in active (open or in-progress) commitfests. |
10 | 9 | """ |
11 | 10 | Tag = apps.get_model("commitfest", "Tag") |
12 | 11 | Patch = apps.get_model("commitfest", "Patch") |
13 | 12 | Topic = apps.get_model("commitfest", "Topic") |
14 | | - PatchOnCommitFest = apps.get_model("commitfest", "PatchOnCommitFest") |
15 | | - |
16 | | - # CommitFest status constants (matching models.py) |
17 | | - STATUS_OPEN = 2 |
18 | | - STATUS_INPROGRESS = 3 |
| 13 | + PatchTag = Patch.tags.through |
19 | 14 |
|
20 | 15 | # Create missing tags |
21 | 16 | Tag.objects.get_or_create( |
@@ -55,30 +50,25 @@ def create_missing_tags_and_map_topics(apps, schema_editor): |
55 | 50 | # chosen there. |
56 | 51 | } |
57 | 52 |
|
58 | | - # Get patch IDs that are in active commitfests (open or in-progress) |
59 | | - active_patch_ids = set( |
60 | | - PatchOnCommitFest.objects.filter( |
61 | | - commitfest__status__in=[STATUS_OPEN, STATUS_INPROGRESS] |
62 | | - ).values_list("patch_id", flat=True) |
63 | | - ) |
64 | | - |
65 | 53 | # Apply tags to existing patches based on their topics |
66 | 54 | for topic_name, tag_name in topic_tag_mapping.items(): |
67 | 55 | try: |
68 | 56 | topic = Topic.objects.get(topic=topic_name) |
69 | 57 | tag = Tag.objects.get(name=tag_name) |
70 | 58 |
|
71 | | - # Get patches with this topic that are in active commitfests |
72 | | - patches_with_topic = Patch.objects.filter( |
73 | | - topic=topic, id__in=active_patch_ids |
| 59 | + # Get all patch IDs with this topic |
| 60 | + patch_ids = list( |
| 61 | + Patch.objects.filter(topic=topic).values_list("id", flat=True) |
74 | 62 | ) |
75 | 63 |
|
76 | | - # Add the corresponding tag to each patch |
77 | | - for patch in patches_with_topic: |
78 | | - patch.tags.add(tag) |
| 64 | + # Bulk create the patch-tag relationships |
| 65 | + PatchTag.objects.bulk_create( |
| 66 | + [PatchTag(patch_id=pid, tag_id=tag.id) for pid in patch_ids], |
| 67 | + ignore_conflicts=True, |
| 68 | + ) |
79 | 69 |
|
80 | 70 | print( |
81 | | - f"Mapped {patches_with_topic.count()} patches from topic '{topic_name}' to tag '{tag_name}'" |
| 71 | + f"Mapped {len(patch_ids)} patches from topic '{topic_name}' to tag '{tag_name}'" |
82 | 72 | ) |
83 | 73 |
|
84 | 74 | except Topic.DoesNotExist: |
|
0 commit comments