old_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.9.7 on 2016-07-02 20:45
  from __future__ import unicode_literals
  
  import hashlib
  import json
  
  from django.conf import settings
  from django.db import migrations
  from django.utils.crypto import get_random_string, pbkdf2
  
  from callisto.delivery.models import _encrypt_report, _pepper
  from callisto.delivery.report_delivery import MatchReportContent
  
  
  def encrypt_match_report(apps, schema_editor):
      MatchReport = apps.get_model('delivery', 'MatchReport')
      for match_report in MatchReport.objects.all():
          match_report_content = MatchReportContent(identifier=match_report.identifier,
                                                    perp_name=match_report.name,
                                                    email=match_report.contact_email,
                                                    phone=match_report.contact_phone,
                                                    contact_name=match_report.contact_name,
                                                    voicemail=match_report.contact_voicemail,
                                                    notes=match_report.contact_notes)
          match_report.salt = get_random_string()
          stretched_identifier = pbkdf2(match_report.identifier, match_report.salt,
                                        settings.ORIGINAL_KEY_ITERATIONS, digest=hashlib.sha256)
          encrypted_match_report = _pepper(_encrypt_report(stretched_key=stretched_identifier,
                                                           report_text=json.dumps(match_report_content.__dict__)))
          match_report.encrypted = encrypted_match_report
          if match_report.seen:
              match_report.identifier = None
          match_report.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('delivery', '0003_allow_deletion_of_identifier'),
      ]
  
      operations = [
           migrations.RunPython(encrypt_match_report, reverse_code=migrations.RunPython.noop),
      ]
  
new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.9.7 on 2016-07-02 20:45
  from __future__ import unicode_literals
  
  import hashlib
  import json
  
  from django.conf import settings
  from django.db import migrations
  from django.utils.crypto import get_random_string, pbkdf2
  
  from callisto.delivery.models import _encrypt_report, _pepper
  from callisto.delivery.report_delivery import MatchReportContent
  
  
  def encrypt_match_report(apps, schema_editor):
      db_alias = schema_editor.connection.alias
      MatchReport = apps.get_model('delivery', 'MatchReport')
      for match_report in MatchReport.objects.using(db_alias).all():
          match_report_content = MatchReportContent(identifier=match_report.identifier,
                                                    perp_name=match_report.name,
                                                    email=match_report.contact_email,
                                                    phone=match_report.contact_phone,
                                                    contact_name=match_report.contact_name,
                                                    voicemail=match_report.contact_voicemail,
                                                    notes=match_report.contact_notes)
          match_report.salt = get_random_string()
          stretched_identifier = pbkdf2(match_report.identifier, match_report.salt,
                                        settings.ORIGINAL_KEY_ITERATIONS, digest=hashlib.sha256)
          encrypted_match_report = _pepper(_encrypt_report(stretched_key=stretched_identifier,
                                                           report_text=json.dumps(match_report_content.__dict__)))
          match_report.encrypted = encrypted_match_report
          if match_report.seen:
              match_report.identifier = None
          match_report.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('delivery', '0003_allow_deletion_of_identifier'),
      ]
  
      operations = [
           migrations.RunPython(encrypt_match_report, reverse_code=migrations.RunPython.noop),
      ]
  
subject: use db_alias in migration query