old_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.14 on 2018-08-03 17:32
  from __future__ import unicode_literals
  from __future__ import absolute_import
  
  from django.db import migrations
  
  from corehq.blobs import CODES
  from corehq.sql_db.operations import HqRunPython
  from corehq.sql_db.util import get_db_alias_for_partitioned_doc
  
  
  def move_datafile_to_blobmeta(apps, schema_editor):
      DataFile = apps.get_model('export', 'DataFile')
      BlobMeta = apps.get_model('blobs', 'BlobMeta')
  
      # At time of writing there are only 91 DataFile rows on prod, 1 on icds-new
      # this may need to be changed if envs exist having many many more
      #
      # '_default' is the bucket name from the old blob db API.
      for datafile in DataFile.objects.all():
          db = get_db_alias_for_partitioned_doc(datafile.domain)
          BlobMeta(
              domain=datafile.domain,
              parent_id=datafile.domain,
              type_code=CODES.data_file,
              path="_default/" + datafile.blob_id,
              properties={"description": datafile.description},
              content_type=datafile.content_type,
              content_length=datafile.content_length,
              expires_on=datafile.delete_after,
          ).save(using=db)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('export', '0004_datafile_delete_after'),
      ]
  
      operations = [
          HqRunPython(move_datafile_to_blobmeta),
          migrations.DeleteModel(name='DataFile'),
      ]
  
new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.14 on 2018-08-03 17:32
  from __future__ import unicode_literals
  from __future__ import absolute_import
  
  from django.db import migrations
  
  from corehq.blobs import CODES
  from corehq.sql_db.operations import HqRunPython
  from corehq.sql_db.util import get_db_alias_for_partitioned_doc
  
  
  def move_datafile_to_blobmeta(apps, schema_editor):
      DataFile = apps.get_model('export', 'DataFile')
      BlobMeta = apps.get_model('blobs', 'BlobMeta')
  
      # At time of writing there are only 91 DataFile rows on prod, 1 on icds-new
      # this may need to be changed if envs exist having many many more
      #
      # '_default' is the bucket name from the old blob db API.
      for datafile in DataFile.objects.all():
          db = get_db_alias_for_partitioned_doc(datafile.domain)
          BlobMeta(
              domain=datafile.domain,
              parent_id=datafile.domain,
              type_code=CODES.data_file,
              key="_default/" + datafile.blob_id,
              properties={"description": datafile.description},
              content_type=datafile.content_type,
              content_length=datafile.content_length,
              expires_on=datafile.delete_after,
          ).save(using=db)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('export', '0004_datafile_delete_after'),
      ]
  
      operations = [
          HqRunPython(move_datafile_to_blobmeta),
          migrations.DeleteModel(name='DataFile'),
      ]
  
subject: Fix migration script