# Path: tenant_schemas/postgresql_backend/base.py
# def _check_schema_name(name):
#     if not _is_valid_schema_name(name):
#         raise ValidationError("Invalid string used for the schema name.")
# 
# Path: tenant_schemas/utils.py
# def get_public_schema_name():
#     return getattr(settings, 'PUBLIC_SCHEMA_NAME', 'public')
# 
# def schema_exists(schema_name):
#     cursor = connection.cursor()
# 
#     # check if this schema already exists in the db
#     sql = 'SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_namespace WHERE LOWER(nspname) = LOWER(%s))'
#     cursor.execute(sql, (schema_name, ))
# 
#     row = cursor.fetchone()
#     if row:
#         exists = row[0]
#     else:
#         exists = False
# 
#     cursor.close()
# 
#     return exists


from django.core.management import call_command
from django.db import connection, models
from tenant_schemas.postgresql_backend.base import _check_schema_name
from tenant_schemas.signals import post_schema_sync
from tenant_schemas.utils import get_public_schema_name, schema_exists
    All tenant models must inherit this class.
    """

    auto_drop_schema = False
    """
    USE THIS WITH CAUTION!
    Set this flag to true on a parent class if you want the schema to be
    automatically deleted if the tenant row gets deleted.
    """

    auto_create_schema = True
    """
    Set this flag to false on a parent class if you don't want the schema
    to be automatically created upon save.
    """

    domain_url = models.CharField(max_length=128, unique=True)
    schema_name = models.CharField(max_length=63, unique=True,
                                   validators=[_check_schema_name])
    objects = TenantQueryset.as_manager()

    class Meta:
        abstract = True

    def save(self, verbosity=1, *args, **kwargs):
        is_new = self.pk is None

        if is_new and connection.schema_name != get_public_schema_name():
            raise Exception("Can't create tenant outside the public schema. "
                            "Current schema is %s." % connection.schema_name)

# Path: tenant_schemas/postgresql_backend/base.py
# def _check_schema_name(name):
#     if not _is_valid_schema_name(name):
#         raise ValidationError("Invalid string used for the schema name.")
# 
# Path: tenant_schemas/utils.py
# def get_public_schema_name():
#     return getattr(settings, 'PUBLIC_SCHEMA_NAME', 'public')
# 
# def schema_exists(schema_name):
#     cursor = connection.cursor()
# 
#     # check if this schema already exists in the db
#     sql = 'SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_namespace WHERE LOWER(nspname) = LOWER(%s))'
#     cursor.execute(sql, (schema_name, ))
# 
#     row = cursor.fetchone()
#     if row:
#         exists = row[0]
#     else:
#         exists = False
# 
#     cursor.close()
# 
#     return exists


# Path: tenant_schemas/models.py
from django.core.management import call_command
from django.db import connection, models
from tenant_schemas.postgresql_backend.base import _check_schema_name
from tenant_schemas.signals import post_schema_sync
from tenant_schemas.utils import get_public_schema_name, schema_exists

    All tenant models must inherit this class.
    """

    auto_drop_schema = False
    """
    USE THIS WITH CAUTION!
    Set this flag to true on a parent class if you want the schema to be
    automatically deleted if the tenant row gets deleted.
    """

    auto_create_schema = True
    """
    Set this flag to false on a parent class if you don't want the schema
    to be automatically created upon save.
    """

    domain_url = models.CharField(max_length=128, unique=True)
    schema_name = models.CharField(max_length=63, unique=True,
                                   validators=[_check_schema_name])
    objects = TenantQueryset.as_manager()

    class Meta:
        abstract = True

    def save(self, verbosity=1, *args, **kwargs):
        is_new = self.pk is None

        if is_new and connection.schema_name != get_public_schema_name():
            raise Exception("Can't create tenant outside the public schema. "
                            "Current schema is %s." % connection.schema_name)

        elif not is_new and connection.schema_name not in (self.schema_name, get_public_schema_name()):