<commit_before># -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-03-03 15:23
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.postgres.search import SearchVector
from django.db import migrations
from django.db.models.expressions import Value


def unicode_damnit(data, charset="utf-8", errors="replace"):
    """copied over from inboxen.utils.emails"""
    if isinstance(data, str):
        return data
    elif data is None:
        return u""

    try:
        return str(bytes(data), charset, errors)
    except LookupError:
        return str(bytes(data), "ascii", errors)


def combine_index(field_a, field_b):
    vector = SearchVector(Value(field_a), config=settings.SEARCH_CONFIG, weight="A") + \
             SearchVector(Value(field_b), config=settings.SEARCH_CONFIG, weight="B")

    return vector


def get_header(email_id, header_name, header_data_model):
    try:
        header = header_data_model.objects.filter(
            header__part__parent__isnull=True,
            header__name__name=header_name,
            header__part__email__id=email_id,
        ).first()

        return unicode_damnit(header.data)
    except AttributeError:
        return ""


def populate_search_index(apps, schema_editor):
    Inbox = apps.get_model("inboxen", "Inbox")
    Email = apps.get_model("inboxen", "Email")
    HeaderData = apps.get_model("inboxen", "HeaderData")

    for inbox in Inbox.objects.all().select_related("domain"):
        inbox.search_tsv = combine_index(inbox.description, "{}@{}".format(inbox.inbox, inbox.domain.domain))
        inbox.save()

    for email in Email.objects.all():
        email.search_tsv = combine_index(get_header(email.id, "Subject", HeaderData), get_header(email.id, "From", HeaderData))
        email.save()


class Migration(migrations.Migration):

    dependencies = [
        ("inboxen", "0024_auto_20190225_1606"),
    ]

    operations = [
        migrations.RunPython(populate_search_index, reverse_code=lambda x, y: None),
    ]<commit_msg>Speed up search data migration<commit_after># -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-03-03 15:23
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.postgres.search import SearchVector
from django.db import migrations
from django.db.models.expressions import Value


def unicode_damnit(data, charset="utf-8", errors="replace"):
    """copied over from inboxen.utils.emails"""
    if isinstance(data, str):
        return data
    elif data is None:
        return u""

    try:
        return str(bytes(data), charset, errors)
    except LookupError:
        return str(bytes(data), "ascii", errors)


def combine_index(field_a, field_b):
    vector = SearchVector(Value(field_a), config=settings.SEARCH_CONFIG, weight="A") + \
             SearchVector(Value(field_b), config=settings.SEARCH_CONFIG, weight="B")

    return vector


def get_header(email_id, header_name, header_data_model):
    try:
        header = header_data_model.objects.filter(
            header__part__parent__isnull=True,
            header__name__name=header_name,
            header__part__email__id=email_id,
        ).first()

        return unicode_damnit(header.data)
    except AttributeError:
        return ""


def populate_search_index(apps, schema_editor):
    Inbox = apps.get_model("inboxen", "Inbox")
    Email = apps.get_model("inboxen", "Email")
    HeaderData = apps.get_model("inboxen", "HeaderData")

    for inbox in Inbox.objects.all().select_related("domain").iterator():
        inbox.search_tsv = combine_index(inbox.description, <EMAIL>(inbox.inbox, inbox.domain.domain))
        inbox.save(update_fields=["search_tsv"])

    for email in Email.objects.all().iterator():
        email.search_tsv = combine_index(get_header(email.id, "Subject", HeaderData), get_header(email.id, "From", HeaderData))
        email.save(update_fields=["search_tsv"])


class Migration(migrations.Migration):

    dependencies = [
        ("inboxen", "0024_auto_20190225_1606"),
    ]

    operations = [
        migrations.RunPython(populate_search_index, reverse_code=lambda x, y: None),
    ]
{'fraction_non_alphanumeric': 0.11731044349070101, 'fraction_numerical': 0.016690510252742013, 'mean_word_length': 3.640486725663717, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7545747', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1277, 'n_words': 268}