from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    try:
        schema_editor.execute(
            'DROP INDEX IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.RunPython(fix_fee_product_index),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]

from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    try:
        schema_editor.execute(
            'DROP INDEX IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(fix_fee_product_index),
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]

Fix order of migration in operations
Fix order of migration in operations

  from __future__ import unicode_literals
  
  from django.db import migrations, OperationalError, ProgrammingError
  
  def fix_fee_product_index(apps, schema_editor):
      try:
          schema_editor.execute(
              'DROP INDEX IF EXISTS idx_16977_product_id;'
          )
      except (ProgrammingError, OperationalError):
          pass
      try:
          schema_editor.execute(
              'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
              'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
          )
      except (ProgrammingError, OperationalError):
          pass
      try:
          schema_editor.execute(
              'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
              'ADD CONSTRAINT idx_16977_product_id '
              'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
          )
      except (ProgrammingError, OperationalError):
          pass
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('ratechecker', '0001_initial'),
      ]
  
      operations = [
+         migrations.RunPython(fix_fee_product_index),
          migrations.AlterUniqueTogether(
              name='fee',
              unique_together=set([]),
          ),
          migrations.RemoveField(
              model_name='fee',
              name='plan',
          ),
-         migrations.RunPython(fix_fee_product_index),
          migrations.DeleteModel(
              name='Fee',
          ),
      ]
  
Fix order of migration in operations
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    try:
        schema_editor.execute(
            'DROP INDEX IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.RunPython(fix_fee_product_index),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]

## Instruction:
Fix order of migration in operations
## Code After:
from __future__ import unicode_literals

from django.db import migrations, OperationalError, ProgrammingError

def fix_fee_product_index(apps, schema_editor):
    try:
        schema_editor.execute(
            'DROP INDEX IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'DROP CONSTRAINT IF EXISTS idx_16977_product_id;'
        )
    except (ProgrammingError, OperationalError):
        pass
    try:
        schema_editor.execute(
            'ALTER TABLE IF EXISTS cfpb.ratechecker_fee '
            'ADD CONSTRAINT idx_16977_product_id '
            'UNIQUE (product_id, state_id, lender, single_family, condo, coop);'
        )
    except (ProgrammingError, OperationalError):
        pass

class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(fix_fee_product_index),
        migrations.AlterUniqueTogether(
            name='fee',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='fee',
            name='plan',
        ),
        migrations.DeleteModel(
            name='Fee',
        ),
    ]
