from django.apps import AppConfig
from django.db.models.signals import pre_migrate, post_migrate

from .utils import create_index_table, reindex_content


def create_index(sender, **kwargs):
    if isinstance(sender, SearchConfig):
        create_index_table(force=True)


def reindex(sender, **kwargs):
    if isinstance(sender, SearchConfig):
        reindex_content(force=False)


class SearchConfig(AppConfig):
    name = 'ideascube.search'
    verbose_name = 'Search'

    def ready(self):
        pre_migrate.connect(create_index, sender=self)
        post_migrate.connect(reindex, sender=self)

from django.apps import AppConfig
from django.db.models.signals import pre_migrate, post_migrate

from .utils import create_index_table, reindex_content


def create_index(sender, **kwargs):
    if (kwargs['using'] == 'transient' and isinstance(sender, SearchConfig)):
        create_index_table(force=True)


def reindex(sender, **kwargs):
    if (kwargs['using'] == 'transient' and isinstance(sender, SearchConfig)):
        reindex_content(force=False)


class SearchConfig(AppConfig):
    name = 'ideascube.search'
    verbose_name = 'Search'

    def ready(self):
        pre_migrate.connect(create_index, sender=self)
        post_migrate.connect(reindex, sender=self)

Make (pre|post)_migrate scripts for the index table only if working on 'transient'.
Make (pre|post)_migrate scripts for the index table only if working on 'transient'.

Django run (pre|post)_migrate script once per database.
As we have two databases, the create_index is launch twice with different
kwargs['using'] ('default' and 'transient'). We should try to create
the index table only when we are working on the transient database.

Most of the time, this is not important and create a new index table
twice is not important.
However, if we run tests, the database are configured and migrate
one after the other and the 'transient' database may be miss-configured
at a time. By creating the table only at the right time, we ensure that
everything is properly configured.

  from django.apps import AppConfig
  from django.db.models.signals import pre_migrate, post_migrate
  
  from .utils import create_index_table, reindex_content
  
  
  def create_index(sender, **kwargs):
-     if isinstance(sender, SearchConfig):
+     if (kwargs['using'] == 'transient' and isinstance(sender, SearchConfig)):
          create_index_table(force=True)
  
  
  def reindex(sender, **kwargs):
-     if isinstance(sender, SearchConfig):
+     if (kwargs['using'] == 'transient' and isinstance(sender, SearchConfig)):
          reindex_content(force=False)
  
  
  class SearchConfig(AppConfig):
      name = 'ideascube.search'
      verbose_name = 'Search'
  
      def ready(self):
          pre_migrate.connect(create_index, sender=self)
          post_migrate.connect(reindex, sender=self)
  
Make (pre|post)_migrate scripts for the index table only if working on 'transient'.
## Code Before:
from django.apps import AppConfig
from django.db.models.signals import pre_migrate, post_migrate

from .utils import create_index_table, reindex_content


def create_index(sender, **kwargs):
    if isinstance(sender, SearchConfig):
        create_index_table(force=True)


def reindex(sender, **kwargs):
    if isinstance(sender, SearchConfig):
        reindex_content(force=False)


class SearchConfig(AppConfig):
    name = 'ideascube.search'
    verbose_name = 'Search'

    def ready(self):
        pre_migrate.connect(create_index, sender=self)
        post_migrate.connect(reindex, sender=self)

## Instruction:
Make (pre|post)_migrate scripts for the index table only if working on 'transient'.
## Code After:
from django.apps import AppConfig
from django.db.models.signals import pre_migrate, post_migrate

from .utils import create_index_table, reindex_content


def create_index(sender, **kwargs):
    if (kwargs['using'] == 'transient' and isinstance(sender, SearchConfig)):
        create_index_table(force=True)


def reindex(sender, **kwargs):
    if (kwargs['using'] == 'transient' and isinstance(sender, SearchConfig)):
        reindex_content(force=False)


class SearchConfig(AppConfig):
    name = 'ideascube.search'
    verbose_name = 'Search'

    def ready(self):
        pre_migrate.connect(create_index, sender=self)
        post_migrate.connect(reindex, sender=self)
