old_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.10.3 on 2016-11-21 20:07
  from __future__ import unicode_literals
  
  from django.db import migrations, models
  import django.db.models.deletion
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('snoop', '0009_collection_ocr'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='ocr',
              name='collection',
              field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='ocr_documents', to='snoop.Collection'),
              preserve_default=False,
          ),
          migrations.AlterUniqueTogether(
              name='ocr',
              unique_together=set([('collection', 'tag', 'md5')]),
          ),
      ]
  
new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.10.3 on 2016-11-21 20:07
  from __future__ import unicode_literals
  
  from pathlib import Path
  from django.conf import settings
  from django.db import migrations, models
  import django.db.models.deletion
  
  
  def generate_default_ocr_sets(apps, schema_editor):
      if not hasattr(settings, 'SNOOP_OCR_ROOT'):
          return
      old_root = settings.SNOOP_OCR_ROOT
  
      db_alias = schema_editor.connection.alias
      Ocr = apps.get_model('snoop', 'Ocr')
      Collection = apps.get_model('snoop', 'Collection')
  
      try:
          collection = Collection.objects.using(db_alias).order_by('id').get()
      except Collection.NotFound:
          return
  
      tags = {ocr.tag for ocr in Ocr.objects.using(db_alias).distinct('tag')}
      for tag in tags:
          path = Path(old_root) / tag
          collection.ocr[tag] = str(path.resolve())
          collection.save()
      Ocr.objects.using(db_alias).update(collection_id=collection.id)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('snoop', '0009_collection_ocr'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='ocr',
              name='collection',
              field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='ocr_documents', to='snoop.Collection'),
              preserve_default=False,
          ),
          migrations.AlterUniqueTogether(
              name='ocr',
              unique_together=set([('collection', 'tag', 'md5')]),
          ),
          migrations.RunPython(generate_default_ocr_sets),
      ]
  
subject: migrate all existing ocr documents to a collection