from __future__ import unicode_literals

from django.db import migrations, models

from milestones.data import fetch_milestone_relationship_types


def seed_relationship_types(apps, schema_editor):
    """Seed the relationship types."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    db_alias = schema_editor.connection.alias
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.using(db_alias).get_or_create(
            name=name,
            description='Autogenerated milestone relationship type "{}"'.format(name),
        )


def delete_relationship_types(apps, schema_editor):
    """Clean up any relationships we made."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    db_alias = schema_editor.connection.alias
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.using(db_alias).filter(name=name).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('milestones', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(seed_relationship_types, delete_relationship_types),
    ]

from __future__ import unicode_literals

from django.db import migrations, models

from milestones.data import fetch_milestone_relationship_types


def seed_relationship_types(apps, schema_editor):
    """Seed the relationship types."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.get_or_create(
            name=name,
            description='Autogenerated milestone relationship type "{}"'.format(name),
        )


def delete_relationship_types(apps, schema_editor):
    """Clean up any relationships we made."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.filter(name=name).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('milestones', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(seed_relationship_types, delete_relationship_types),
    ]

Remove uses of using() from migrations
Remove uses of using() from migrations

This hardcoded the db_alias fetched from schema_editor and forces django
to try and migrate any second database you use, rather than routing to
the default database.  In testing a build from scratch, these do not
appear needed.

Using using() prevents us from using multiple databases behind edxapp

  from __future__ import unicode_literals
  
  from django.db import migrations, models
  
  from milestones.data import fetch_milestone_relationship_types
  
  
  def seed_relationship_types(apps, schema_editor):
      """Seed the relationship types."""
      MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
-     db_alias = schema_editor.connection.alias
      for name in fetch_milestone_relationship_types().values():
-         MilestoneRelationshipType.objects.using(db_alias).get_or_create(
+         MilestoneRelationshipType.objects.get_or_create(
              name=name,
              description='Autogenerated milestone relationship type "{}"'.format(name),
          )
  
  
  def delete_relationship_types(apps, schema_editor):
      """Clean up any relationships we made."""
      MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
-     db_alias = schema_editor.connection.alias
      for name in fetch_milestone_relationship_types().values():
-         MilestoneRelationshipType.objects.using(db_alias).filter(name=name).delete()
+         MilestoneRelationshipType.objects.filter(name=name).delete()
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('milestones', '0001_initial'),
      ]
  
      operations = [
          migrations.RunPython(seed_relationship_types, delete_relationship_types),
      ]
  
Remove uses of using() from migrations
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, models

from milestones.data import fetch_milestone_relationship_types


def seed_relationship_types(apps, schema_editor):
    """Seed the relationship types."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    db_alias = schema_editor.connection.alias
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.using(db_alias).get_or_create(
            name=name,
            description='Autogenerated milestone relationship type "{}"'.format(name),
        )


def delete_relationship_types(apps, schema_editor):
    """Clean up any relationships we made."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    db_alias = schema_editor.connection.alias
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.using(db_alias).filter(name=name).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('milestones', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(seed_relationship_types, delete_relationship_types),
    ]

## Instruction:
Remove uses of using() from migrations
## Code After:
from __future__ import unicode_literals

from django.db import migrations, models

from milestones.data import fetch_milestone_relationship_types


def seed_relationship_types(apps, schema_editor):
    """Seed the relationship types."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.get_or_create(
            name=name,
            description='Autogenerated milestone relationship type "{}"'.format(name),
        )


def delete_relationship_types(apps, schema_editor):
    """Clean up any relationships we made."""
    MilestoneRelationshipType = apps.get_model("milestones", "MilestoneRelationshipType")
    for name in fetch_milestone_relationship_types().values():
        MilestoneRelationshipType.objects.filter(name=name).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('milestones', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(seed_relationship_types, delete_relationship_types),
    ]
