old_contents: # -*- 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),
      ]
  
new_contents: # -*- 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, "{}@{}".format(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),
      ]
  
subject: Speed up search data migration