# Path: django_sharding_library/sql.py

# 
# Path: django_sharding_library/exceptions.py
# class DjangoShardingException(Exception):
#     pass


from django.db import connections, DatabaseError, transaction
from django.conf import settings
from django_sharding_library.sql import postgres_shard_id_function_sql
from django.db.models import signals
from django_sharding_library.exceptions import DjangoShardingException
    cursor = connections[db_alias].cursor()
    cursor.execute(
        "SELECT count(*) FROM pg_class c WHERE c.relkind = '%s' and c.relname = '%s';" % ('S', sequence_name)
    )

    if cursor.fetchone()[0] == 0:
        cursor.close()
        return False

    cursor.execute(
        "SELECT count(*) from pg_proc p where p.proname = '%s';" % (function_name,)
    )

    if cursor.fetchone()[0] == 0:
        cursor.close()
        return False

    cursor.close()
    return True


def register_migration_signal_for_model_receiver(model, function, dispatch_uid=None):
    signals.pre_migrate.connect(function, sender=model, dispatch_uid=dispatch_uid)


def is_model_class_on_database(model, database):
    specific_database = getattr(model, 'django_sharding__database', None)
    is_sharded = getattr(model, 'django_sharding__is_sharded', False)

    if specific_database and is_sharded:

# Path: django_sharding_library/sql.py

# 
# Path: django_sharding_library/exceptions.py
# class DjangoShardingException(Exception):
#     pass


# Path: django_sharding_library/utils.py
from django.db import connections, DatabaseError, transaction
from django.conf import settings
from django_sharding_library.sql import postgres_shard_id_function_sql
from django.db.models import signals
from django_sharding_library.exceptions import DjangoShardingException

    cursor = connections[db_alias].cursor()
    cursor.execute(
        "SELECT count(*) FROM pg_class c WHERE c.relkind = '%s' and c.relname = '%s';" % ('S', sequence_name)
    )

    if cursor.fetchone()[0] == 0:
        cursor.close()
        return False

    cursor.execute(
        "SELECT count(*) from pg_proc p where p.proname = '%s';" % (function_name,)
    )

    if cursor.fetchone()[0] == 0:
        cursor.close()
        return False

    cursor.close()
    return True


def register_migration_signal_for_model_receiver(model, function, dispatch_uid=None):
    signals.pre_migrate.connect(function, sender=model, dispatch_uid=dispatch_uid)


def is_model_class_on_database(model, database):
    specific_database = getattr(model, 'django_sharding__database', None)
    is_sharded = getattr(model, 'django_sharding__is_sharded', False)

    if specific_database and is_sharded:

        raise DjangoShardingException('Model marked as both sharded and on a single database, unable to determine where to run migrations for {}.'.format(model.__class__.__name__))