
from django.db import migrations
from corehq.messaging.smsbackends.turn.models import SQLTurnWhatsAppBackend
from corehq.toggles import TURN_IO_BACKEND, NAMESPACE_DOMAIN


def auto_enable_turnio_ff_for_certain_domains(apps, schema_editor):
    for backend in SQLTurnWhatsAppBackend.active_objects.all():
        domain = backend.domain
        TURN_IO_BACKEND.set(item=domain, enabled=True, namespace=NAMESPACE_DOMAIN)


def noop(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('sms', '0048_delete_sqlicdsbackend'),
    ]

    operations = [
        migrations.RunPython(auto_enable_turnio_ff_for_certain_domains, reverse_code=noop),
    ]


from django.db import migrations
from corehq.toggles import TURN_IO_BACKEND, NAMESPACE_DOMAIN


def auto_enable_turnio_ff_for_certain_domains(apps, schema_editor):
    SQLTurnWhatsAppBackend = apps.get_model('sms', 'SQLTurnWhatsAppBackend')

    for backend in SQLTurnWhatsAppBackend.objects.all():
        # Check for backend.deleted to account for active_objects
        if not backend.deleted:
            domain = backend.domain
            TURN_IO_BACKEND.set(item=domain, enabled=True, namespace=NAMESPACE_DOMAIN)


def noop(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('sms', '0048_delete_sqlicdsbackend'),
    ]

    operations = [
        migrations.RunPython(auto_enable_turnio_ff_for_certain_domains, reverse_code=noop),
    ]

Use historical model in migration, not directly imported model
Use historical model in migration, not directly imported model

  
  from django.db import migrations
- from corehq.messaging.smsbackends.turn.models import SQLTurnWhatsAppBackend
  from corehq.toggles import TURN_IO_BACKEND, NAMESPACE_DOMAIN
  
  
  def auto_enable_turnio_ff_for_certain_domains(apps, schema_editor):
+     SQLTurnWhatsAppBackend = apps.get_model('sms', 'SQLTurnWhatsAppBackend')
+ 
-     for backend in SQLTurnWhatsAppBackend.active_objects.all():
+     for backend in SQLTurnWhatsAppBackend.objects.all():
+         # Check for backend.deleted to account for active_objects
+         if not backend.deleted:
-         domain = backend.domain
+             domain = backend.domain
-         TURN_IO_BACKEND.set(item=domain, enabled=True, namespace=NAMESPACE_DOMAIN)
+             TURN_IO_BACKEND.set(item=domain, enabled=True, namespace=NAMESPACE_DOMAIN)
  
  
  def noop(apps, schema_editor):
      pass
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('sms', '0048_delete_sqlicdsbackend'),
      ]
  
      operations = [
          migrations.RunPython(auto_enable_turnio_ff_for_certain_domains, reverse_code=noop),
      ]
  
Use historical model in migration, not directly imported model
## Code Before:

from django.db import migrations
from corehq.messaging.smsbackends.turn.models import SQLTurnWhatsAppBackend
from corehq.toggles import TURN_IO_BACKEND, NAMESPACE_DOMAIN


def auto_enable_turnio_ff_for_certain_domains(apps, schema_editor):
    for backend in SQLTurnWhatsAppBackend.active_objects.all():
        domain = backend.domain
        TURN_IO_BACKEND.set(item=domain, enabled=True, namespace=NAMESPACE_DOMAIN)


def noop(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('sms', '0048_delete_sqlicdsbackend'),
    ]

    operations = [
        migrations.RunPython(auto_enable_turnio_ff_for_certain_domains, reverse_code=noop),
    ]

## Instruction:
Use historical model in migration, not directly imported model
## Code After:

from django.db import migrations
from corehq.toggles import TURN_IO_BACKEND, NAMESPACE_DOMAIN


def auto_enable_turnio_ff_for_certain_domains(apps, schema_editor):
    SQLTurnWhatsAppBackend = apps.get_model('sms', 'SQLTurnWhatsAppBackend')

    for backend in SQLTurnWhatsAppBackend.objects.all():
        # Check for backend.deleted to account for active_objects
        if not backend.deleted:
            domain = backend.domain
            TURN_IO_BACKEND.set(item=domain, enabled=True, namespace=NAMESPACE_DOMAIN)


def noop(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('sms', '0048_delete_sqlicdsbackend'),
    ]

    operations = [
        migrations.RunPython(auto_enable_turnio_ff_for_certain_domains, reverse_code=noop),
    ]
