from __future__ import unicode_literals

from django.db import migrations, models


def set_data_relation(apps, schema_editor):
    Data = apps.get_model('flow', 'Data')
    Storage = apps.get_model('flow', 'Storage')

    for data in Data.objects.all():
        storage = Storage.objects.filter(data_migration_temporary=data).first()
        if storage:
            storage.data.add(data)


class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0028_add_data_location'),
    ]

    operations = [
        migrations.RenameField(
            model_name='storage',
            old_name='data',
            new_name='data_migration_temporary',
        ),
        migrations.AddField(
            model_name='storage',
            name='data',
            field=models.ManyToManyField(related_name='storages', to='flow.Data'),
        ),
        migrations.RunPython(set_data_relation),
        migrations.RemoveField(
            model_name='storage',
            name='data_migration_temporary',
        ),
    ]

from __future__ import unicode_literals

from django.db import migrations, models


def set_data_relation(apps, schema_editor):
    Storage = apps.get_model('flow', 'Storage')

    for storage in Storage.objects.all():
        storage.data.add(storage.data_migration_temporary)


class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0028_add_data_location'),
    ]

    operations = [
        migrations.RenameField(
            model_name='storage',
            old_name='data',
            new_name='data_migration_temporary',
        ),
        migrations.AddField(
            model_name='storage',
            name='data',
            field=models.ManyToManyField(related_name='storages', to='flow.Data'),
        ),
        migrations.RunPython(set_data_relation),
        migrations.RemoveField(
            model_name='storage',
            name='data_migration_temporary',
        ),
    ]

Fix storage migration to process all storages
Fix storage migration to process all storages

  from __future__ import unicode_literals
  
  from django.db import migrations, models
  
  
  def set_data_relation(apps, schema_editor):
-     Data = apps.get_model('flow', 'Data')
      Storage = apps.get_model('flow', 'Storage')
  
-     for data in Data.objects.all():
+     for storage in Storage.objects.all():
+         storage.data.add(storage.data_migration_temporary)
-         storage = Storage.objects.filter(data_migration_temporary=data).first()
-         if storage:
-             storage.data.add(data)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('flow', '0028_add_data_location'),
      ]
  
      operations = [
          migrations.RenameField(
              model_name='storage',
              old_name='data',
              new_name='data_migration_temporary',
          ),
          migrations.AddField(
              model_name='storage',
              name='data',
              field=models.ManyToManyField(related_name='storages', to='flow.Data'),
          ),
          migrations.RunPython(set_data_relation),
          migrations.RemoveField(
              model_name='storage',
              name='data_migration_temporary',
          ),
      ]
  
Fix storage migration to process all storages
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, models


def set_data_relation(apps, schema_editor):
    Data = apps.get_model('flow', 'Data')
    Storage = apps.get_model('flow', 'Storage')

    for data in Data.objects.all():
        storage = Storage.objects.filter(data_migration_temporary=data).first()
        if storage:
            storage.data.add(data)


class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0028_add_data_location'),
    ]

    operations = [
        migrations.RenameField(
            model_name='storage',
            old_name='data',
            new_name='data_migration_temporary',
        ),
        migrations.AddField(
            model_name='storage',
            name='data',
            field=models.ManyToManyField(related_name='storages', to='flow.Data'),
        ),
        migrations.RunPython(set_data_relation),
        migrations.RemoveField(
            model_name='storage',
            name='data_migration_temporary',
        ),
    ]

## Instruction:
Fix storage migration to process all storages
## Code After:
from __future__ import unicode_literals

from django.db import migrations, models


def set_data_relation(apps, schema_editor):
    Storage = apps.get_model('flow', 'Storage')

    for storage in Storage.objects.all():
        storage.data.add(storage.data_migration_temporary)


class Migration(migrations.Migration):

    dependencies = [
        ('flow', '0028_add_data_location'),
    ]

    operations = [
        migrations.RenameField(
            model_name='storage',
            old_name='data',
            new_name='data_migration_temporary',
        ),
        migrations.AddField(
            model_name='storage',
            name='data',
            field=models.ManyToManyField(related_name='storages', to='flow.Data'),
        ),
        migrations.RunPython(set_data_relation),
        migrations.RemoveField(
            model_name='storage',
            name='data_migration_temporary',
        ),
    ]
