# 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
                self.delete(force_drop=True)
                raise
            else:
                post_schema_sync.send(sender=TenantMixin, tenant=self)

    def delete(self, force_drop=False, *args, **kwargs):
        """
        Deletes this row. Drops the tenant's schema if the attribute
        auto_drop_schema set to True.
        """
        if connection.schema_name not in (self.schema_name, get_public_schema_name()):
            raise Exception("Can't delete tenant outside it's own schema or "
                            "the public schema. Current schema is %s."
                            % connection.schema_name)

        if schema_exists(self.schema_name) and (self.auto_drop_schema or force_drop):
            cursor = connection.cursor()
            cursor.execute('DROP SCHEMA IF EXISTS %s CASCADE' % self.schema_name)

        return super(TenantMixin, self).delete(*args, **kwargs)

    def create_schema(self, check_if_exists=False, sync_schema=True,
                      verbosity=1):
        """
        Creates the schema 'schema_name' for this tenant. Optionally checks if
        the schema already exists before creating it. Returns true if the
        schema was created, false otherwise.
        """

        # safety check

# 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

                self.delete(force_drop=True)
                raise
            else:
                post_schema_sync.send(sender=TenantMixin, tenant=self)

    def delete(self, force_drop=False, *args, **kwargs):
        """
        Deletes this row. Drops the tenant's schema if the attribute
        auto_drop_schema set to True.
        """
        if connection.schema_name not in (self.schema_name, get_public_schema_name()):
            raise Exception("Can't delete tenant outside it's own schema or "
                            "the public schema. Current schema is %s."
                            % connection.schema_name)

        if schema_exists(self.schema_name) and (self.auto_drop_schema or force_drop):
            cursor = connection.cursor()
            cursor.execute('DROP SCHEMA IF EXISTS %s CASCADE' % self.schema_name)

        return super(TenantMixin, self).delete(*args, **kwargs)

    def create_schema(self, check_if_exists=False, sync_schema=True,
                      verbosity=1):
        """
        Creates the schema 'schema_name' for this tenant. Optionally checks if
        the schema already exists before creating it. Returns true if the
        schema was created, false otherwise.
        """

        # safety check

        _check_schema_name(self.schema_name)