from __future__ import unicode_literals

from django.db import migrations, models


def instance_types_to_list(apps, schema_editor):
    PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
    for pool in PoolConfiguration.objects.all():
        pool.ec2_instance_types_list = [pool.ec2_instance_types]
        pool.save()


class Migration(migrations.Migration):

    dependencies = [
        ('ec2spotmanager', '0006_auto_20150625_2050'),
    ]

    operations = [
        migrations.AlterField(
            model_name='poolconfiguration',
            name='ec2_instance_type',
            field=models.CharField(blank=True, max_length=1023, null=True),
        ),
        migrations.RenameField(
            model_name='poolconfiguration',
            old_name='ec2_instance_type',
            new_name='ec2_instance_types',
        ),
        migrations.RunPython(instance_types_to_list),
    ]

from __future__ import print_function, unicode_literals
import json
import sys

from django.db import migrations, models


def instance_type_to_list(apps, schema_editor):
    PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
    for pool in PoolConfiguration.objects.all():
        if pool.ec2_instance_type:
            pool.ec2_instance_type = json.dumps([pool.ec2_instance_type])
            pool.save()


def instance_type_from_list(apps, schema_editor):
    PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
    for pool in PoolConfiguration.objects.all():
        if pool.ec2_instance_types:
            types = json.loads(pool.ec2_instance_types)
            if len(types) > 1:
                print("pool %d had instance types %r, choosing %s for reverse migration" % (pool.id, types, types[0]),
                      file=sys.stderr)
            pool.ec2_instance_types = types[0]
            pool.save()


class Migration(migrations.Migration):

    dependencies = [
        ('ec2spotmanager', '0006_auto_20150625_2050'),
    ]

    operations = [
        migrations.AlterField(
            model_name='poolconfiguration',
            name='ec2_instance_type',
            field=models.CharField(blank=True, max_length=1023, null=True),
        ),
        migrations.RunPython(
            code=instance_type_to_list,
            reverse_code=instance_type_from_list,
        ),
        migrations.RenameField(
            model_name='poolconfiguration',
            old_name='ec2_instance_type',
            new_name='ec2_instance_types',
        ),
    ]

Fix migration. Custom triggers are not run in data migrations.
Fix migration. Custom triggers are not run in data migrations.

- from __future__ import unicode_literals
+ from __future__ import print_function, unicode_literals
+ import json
+ import sys
  
  from django.db import migrations, models
  
  
- def instance_types_to_list(apps, schema_editor):
+ def instance_type_to_list(apps, schema_editor):
      PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
      for pool in PoolConfiguration.objects.all():
+         if pool.ec2_instance_type:
-         pool.ec2_instance_types_list = [pool.ec2_instance_types]
+             pool.ec2_instance_type = json.dumps([pool.ec2_instance_type])
-         pool.save()
+             pool.save()
+ 
+ 
+ def instance_type_from_list(apps, schema_editor):
+     PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
+     for pool in PoolConfiguration.objects.all():
+         if pool.ec2_instance_types:
+             types = json.loads(pool.ec2_instance_types)
+             if len(types) > 1:
+                 print("pool %d had instance types %r, choosing %s for reverse migration" % (pool.id, types, types[0]),
+                       file=sys.stderr)
+             pool.ec2_instance_types = types[0]
+             pool.save()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('ec2spotmanager', '0006_auto_20150625_2050'),
      ]
  
      operations = [
          migrations.AlterField(
              model_name='poolconfiguration',
              name='ec2_instance_type',
              field=models.CharField(blank=True, max_length=1023, null=True),
          ),
+         migrations.RunPython(
+             code=instance_type_to_list,
+             reverse_code=instance_type_from_list,
+         ),
          migrations.RenameField(
              model_name='poolconfiguration',
              old_name='ec2_instance_type',
              new_name='ec2_instance_types',
          ),
-         migrations.RunPython(instance_types_to_list),
      ]
  
Fix migration. Custom triggers are not run in data migrations.
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, models


def instance_types_to_list(apps, schema_editor):
    PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
    for pool in PoolConfiguration.objects.all():
        pool.ec2_instance_types_list = [pool.ec2_instance_types]
        pool.save()


class Migration(migrations.Migration):

    dependencies = [
        ('ec2spotmanager', '0006_auto_20150625_2050'),
    ]

    operations = [
        migrations.AlterField(
            model_name='poolconfiguration',
            name='ec2_instance_type',
            field=models.CharField(blank=True, max_length=1023, null=True),
        ),
        migrations.RenameField(
            model_name='poolconfiguration',
            old_name='ec2_instance_type',
            new_name='ec2_instance_types',
        ),
        migrations.RunPython(instance_types_to_list),
    ]

## Instruction:
Fix migration. Custom triggers are not run in data migrations.
## Code After:
from __future__ import print_function, unicode_literals
import json
import sys

from django.db import migrations, models


def instance_type_to_list(apps, schema_editor):
    PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
    for pool in PoolConfiguration.objects.all():
        if pool.ec2_instance_type:
            pool.ec2_instance_type = json.dumps([pool.ec2_instance_type])
            pool.save()


def instance_type_from_list(apps, schema_editor):
    PoolConfiguration = apps.get_model("ec2spotmanager", "PoolConfiguration")
    for pool in PoolConfiguration.objects.all():
        if pool.ec2_instance_types:
            types = json.loads(pool.ec2_instance_types)
            if len(types) > 1:
                print("pool %d had instance types %r, choosing %s for reverse migration" % (pool.id, types, types[0]),
                      file=sys.stderr)
            pool.ec2_instance_types = types[0]
            pool.save()


class Migration(migrations.Migration):

    dependencies = [
        ('ec2spotmanager', '0006_auto_20150625_2050'),
    ]

    operations = [
        migrations.AlterField(
            model_name='poolconfiguration',
            name='ec2_instance_type',
            field=models.CharField(blank=True, max_length=1023, null=True),
        ),
        migrations.RunPython(
            code=instance_type_to_list,
            reverse_code=instance_type_from_list,
        ),
        migrations.RenameField(
            model_name='poolconfiguration',
            old_name='ec2_instance_type',
            new_name='ec2_instance_types',
        ),
    ]
