Skip to content

Commit 9ad8c98

Browse files
committed
Speed up the community refactor migration by building the index
of the group name search rules in Python - Legacy-Id: 11188
1 parent 12079a5 commit 9ad8c98

1 file changed

Lines changed: 18 additions & 35 deletions

File tree

ietf/community/migrations/0004_cleanup_data.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
from tqdm import tqdm
4+
import re
55
from django.db import migrations, models
66

77
def port_rules_to_typed_system(apps, schema_editor):
@@ -22,11 +22,7 @@ def try_to_uniquify_person(rule, person_qs):
2222
return person_qs
2323

2424

25-
print("")
26-
print(" * Port Rules to Typed System")
27-
print(" Expect an initial rule count of around 550 here, as of May 2016:")
28-
print(" rule count: %s" % SearchRule.objects.all().count())
29-
for rule in tqdm(SearchRule.objects.all()):
25+
for rule in SearchRule.objects.all():
3026
handled = False
3127

3228
if rule.rule_type in ['wg_asociated', 'area_asociated', 'wg_asociated_rfc', 'area_asociated_rfc']:
@@ -117,14 +113,10 @@ def try_to_uniquify_person(rule, person_qs):
117113
else:
118114
rule.delete()
119115
#print "NOT HANDLED", rule.pk, rule.rule_type, rule.value
120-
print(" rule count: %s" % SearchRule.objects.all().count())
121116

122117
def delete_extra_person_rules(apps, schema_editor):
123118
SearchRule = apps.get_model("community", "SearchRule")
124-
print("")
125-
print(" * Delete Extra Person Rules")
126119
SearchRule.objects.exclude(person=None).filter(value="").delete()
127-
print(" rule count: %s" % SearchRule.objects.all().count())
128120

129121
RENAMED_RULES = [
130122
('wg_asociated', 'group'),
@@ -150,44 +142,33 @@ def rename_rule_type_forwards(apps, schema_editor):
150142

151143
renamings = dict(RENAMED_RULES)
152144

153-
print("")
154-
print(" * Rename Rule Type Forwards")
155-
for r in tqdm(SearchRule.objects.all()):
145+
for r in SearchRule.objects.all():
156146
if r.rule_type in renamings:
157147
r.rule_type = renamings[r.rule_type]
158148
r.save()
159-
print(" rule count: %s" % SearchRule.objects.all().count())
160149

161150
def rename_rule_type_backwards(apps, schema_editor):
162151
SearchRule = apps.get_model("community", "SearchRule")
163152

164153
renamings = dict((to, fro) for fro, to in RENAMED_RULES)
165154

166-
print("")
167-
print(" * Rename Rule Type Backwards")
168-
for r in tqdm(SearchRule.objects.all()):
155+
for r in SearchRule.objects.all():
169156
if r.rule_type in renamings:
170157
r.rule_type = renamings[r.rule_type]
171158
r.save()
172-
print(" rule count: %s" % SearchRule.objects.all().count())
173159

174160
def get_rid_of_empty_lists(apps, schema_editor):
175161
CommunityList = apps.get_model("community", "CommunityList")
176162

177-
print("")
178-
print(" * Get Rid of Empty Lists")
179-
for cl in tqdm(CommunityList.objects.all()):
180-
if not cl.added_docs.exists() and not cl.searchrule_set.exists() and not cl.emailsubscription_set.exists():
181-
cl.delete()
163+
for cl in CommunityList.objects.filter(added_docs=None, searchrule=None, emailsubscription=None):
164+
cl.delete()
182165

183166
def move_email_subscriptions_to_preregistered_email(apps, schema_editor):
184167
EmailSubscription = apps.get_model("community", "EmailSubscription")
185168
Email = apps.get_model("person", "Email")
186169
Person = apps.get_model("person", "Person")
187170

188-
print("")
189-
print(" * Move Email Subscriptions to Preregistered Email")
190-
for e in tqdm(EmailSubscription.objects.all()):
171+
for e in EmailSubscription.objects.all():
191172
email_obj = None
192173
try:
193174
email_obj = Email.objects.get(address=e.email)
@@ -210,38 +191,40 @@ def move_email_subscriptions_to_preregistered_email(apps, schema_editor):
210191
def fill_in_notify_on(apps, schema_editor):
211192
EmailSubscription = apps.get_model("community", "EmailSubscription")
212193

213-
# print("")
214-
# print(" * Fill In Notify On")
215194
EmailSubscription.objects.filter(significant=False).update(notify_on="all")
216195
EmailSubscription.objects.filter(significant=True).update(notify_on="significant")
217196

218197
def add_group_community_lists(apps, schema_editor):
219198
Group = apps.get_model("group", "Group")
220-
Document = apps.get_model("doc", "Document")
199+
DocAlias = apps.get_model("doc", "DocAlias")
221200
State = apps.get_model("doc", "State")
222201
CommunityList = apps.get_model("community", "CommunityList")
223202
SearchRule = apps.get_model("community", "SearchRule")
224203

225204
active_state = State.objects.get(slug="active", type="draft")
226205
rfc_state = State.objects.get(slug="rfc", type="draft")
227206

228-
print("")
229-
print(" * Add Group Community Lists")
230-
for g in tqdm(Group.objects.filter(type__in=("rg", "wg"))):
207+
draft_aliases = DocAlias.objects.filter(name__startswith="draft")
208+
209+
for g in Group.objects.filter(type__in=("rg", "wg")):
231210
clist = CommunityList.objects.filter(group=g).first()
232211
if clist:
233212
SearchRule.objects.get_or_create(community_list=clist, rule_type="group", group=g, state=active_state)
234213
SearchRule.objects.get_or_create(community_list=clist, rule_type="group_rfc", group=g, state=rfc_state)
235214
r, _ = SearchRule.objects.get_or_create(community_list=clist, rule_type="name_contains", text=r"^draft-[^-]+-%s-" % g.acronym, state=active_state)
236-
r.name_contains_index = Document.objects.filter(docalias__name__regex=r.text)
215+
216+
# do the matching manually in Python to speed up the operation
217+
#r.name_contains_index = Document.objects.filter(docalias__name__regex=r.text)
218+
name_re = re.compile(r.text)
219+
r.name_contains_index = [ a.document_id for a in draft_aliases if name_re.match(a.name) ]
237220

238221
else:
239222
clist = CommunityList.objects.create(group=g)
240223
SearchRule.objects.create(community_list=clist, rule_type="group", group=g, state=active_state)
241224
SearchRule.objects.create(community_list=clist, rule_type="group_rfc", group=g, state=rfc_state)
242225
r = SearchRule.objects.create(community_list=clist, rule_type="name_contains", text=r"^draft-[^-]+-%s-" % g.acronym, state=active_state)
243-
r.name_contains_index = Document.objects.filter(docalias__name__regex=r.text)
244-
print(" rule count: %s" % SearchRule.objects.all().count())
226+
name_re = re.compile(r.text)
227+
r.name_contains_index = [ a.document_id for a in draft_aliases if name_re.match(a.name) ]
245228

246229
def noop(apps, schema_editor):
247230
pass

0 commit comments

Comments
 (0)