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_identifier(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            call_command('sync_schemas',
                         schema_name=self.schema_name,
                         tenant=True,
                         public=False,
                         interactive=False,  # don't ask to create an admin user
                         migrate_all=True,  # migrate all apps directly to last version
                         verbosity=verbosity,
                         )

            # fake all migrations
            if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode():
                call_command('migrate_schemas', fake=True, schema_name=self.schema_name, verbosity=verbosity)

        connection.set_schema_to_public()
        return True
Exemple #2
0
    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_identifier(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists \
                and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            call_command('sync_schemas',
                schema_name=self.schema_name,
                interactive=False, # don't ask to create an admin user
                migrate_all=True, # migrate all apps directly to last version
                verbosity=verbosity,
            )

            # fake all migrations
            if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode():
                call_command('migrate_schemas', fake=True, schema_name=self.schema_name, verbosity=verbosity)

        return True
    def create_schema(self, check_if_exists = False, sync_schema = True):
        """
        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_identifier(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists:
            # check if this schema already exists in the db
            sql = 'SELECT schema_name FROM information_schema.schemata '\
                  'WHERE schema_name = %s'
            cursor.execute(sql, (self.schema_name,))

            if len(cursor.fetchall()) > 0:
                # we already have a row
                return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            call_command('sync_schemas', schema_name=self.schema_name,
                    interactive=False) # don't ask to create an admin user

            # make sure you have SOUTH_TESTS_MIGRATE = false
            if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode():
                call_command('migrate_schemas', schema_name=self.schema_name)

        return True
 def handle(self, *args, **options):
     database = options.get('database', 'default')
     if (settings.DATABASES[database]['ENGINE'] == 'tenant_schemas.postgresql_backend' and not
             django_is_in_test_mode()):
         raise CommandError("syncdb has been disabled, for database '{0}'. "
                            "Use sync_schemas instead. Please read the "
                            "documentation if you don't know why "
                            "you shouldn't call syncdb directly!".format(database))
     super(Command, self).handle(*args, **options)
Exemple #5
0
 def handle(self, *args, **options):
     database = options.get('database', 'default')
     if (settings.DATABASES[database]['ENGINE']
             == 'tenant_schemas.postgresql_backend'
             and not django_is_in_test_mode()):
         raise CommandError(
             "syncdb has been disabled, for database '{0}'. "
             "Use sync_schemas instead. Please read the "
             "documentation if you don't know why "
             "you shouldn't call syncdb directly!".format(database))
     super(Command, self).handle(*args, **options)
Exemple #6
0
    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)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            if django.VERSION >= (
                    1,
                    7,
                    0,
            ):
                call_command('migrate_schemas',
                             schema_name=self.schema_name,
                             interactive=False,
                             verbosity=verbosity)
            else:
                # default is faking all migrations and syncing directly to the current models state
                fake_all_migrations = getattr(
                    settings, 'TENANT_CREATION_FAKES_MIGRATIONS', True)
                call_command('sync_schemas',
                             schema_name=self.schema_name,
                             tenant=True,
                             public=False,
                             interactive=False,
                             migrate_all=fake_all_migrations,
                             verbosity=verbosity)

                # run/fake all migrations
                if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode(
                ):
                    call_command('migrate_schemas',
                                 fake=fake_all_migrations,
                                 schema_name=self.schema_name,
                                 verbosity=verbosity)

        connection.set_schema_to_public()
        post_schema_sync.send(sender=TenantMixin, tenant=self)
    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_identifier(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)
        transaction.commit_unless_managed()

        if sync_schema:
            # default is faking all migrations and syncing directly to the current models state
            fake_all_migrations = getattr(settings,
                                          'TENANT_CREATION_FAKES_MIGRATIONS',
                                          True)

            call_command(
                'sync_schemas',
                schema_name=self.schema_name,
                tenant=True,
                public=False,
                interactive=False,  # don't ask to create an admin user
                migrate_all=fake_all_migrations,
                verbosity=verbosity,
            )

            # run/fake all migrations
            if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode(
            ):
                call_command('migrate_schemas',
                             fake=fake_all_migrations,
                             schema_name=self.schema_name,
                             verbosity=verbosity)

        connection.set_schema_to_public()
        return True
    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)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            if django.VERSION >= (1, 7, 0,):
                call_command('migrate_schemas',
                             schema_name=self.schema_name,
                             interactive=False,
                             verbosity=verbosity)
            else:
                # default is faking all migrations and syncing directly to the current models state
                fake_all_migrations = getattr(settings, 'TENANT_CREATION_FAKES_MIGRATIONS', True)
                call_command('sync_schemas',
                             schema_name=self.schema_name,
                             tenant=True,
                             public=False,
                             interactive=False,
                             migrate_all=fake_all_migrations,
                             verbosity=verbosity)

                # run/fake all migrations
                if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode():
                    call_command('migrate_schemas',
                                 fake=fake_all_migrations,
                                 schema_name=self.schema_name,
                                 verbosity=verbosity)

        connection.set_schema_to_public()
        post_schema_sync.send(sender=TenantMixin, tenant=self)
    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_identifier(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute("CREATE SCHEMA %s" % self.schema_name)
        transaction.commit_unless_managed()

        if sync_schema:
            # default is faking all migrations and syncing directly to the current models state
            fake_all_migrations = getattr(settings, "TENANT_CREATION_FAKES_MIGRATIONS", True)

            call_command(
                "sync_schemas",
                schema_name=self.schema_name,
                tenant=True,
                public=False,
                interactive=False,  # don't ask to create an admin user
                migrate_all=fake_all_migrations,
                verbosity=verbosity,
            )

            # run/fake all migrations
            if "south" in settings.INSTALLED_APPS and not django_is_in_test_mode():
                call_command(
                    "migrate_schemas", fake=fake_all_migrations, schema_name=self.schema_name, verbosity=verbosity
                )

        connection.set_schema_to_public()
        return True
Exemple #10
0
import django
from django.conf import settings
from django.core.management.base import CommandError, BaseCommand
from tenant_schemas.utils import django_is_in_test_mode

try:
    from south.management.commands.migrate import Command as MigrateCommand
except ImportError:
    MigrateCommand = BaseCommand


class Command(MigrateCommand):

    def handle(self, *args, **options):
        database = options.get('database', 'default')
        if (settings.DATABASES[database]['ENGINE'] == 'tenant_schemas.postgresql_backend' or
                MigrateCommand is BaseCommand):
            raise CommandError("migrate has been disabled, for database '{0}'. Use migrate_schemas "
                               "instead. Please read the documentation if you don't know why you "
                               "shouldn't call migrate directly!".format(database))
        super(Command, self).handle(*args, **options)


if django.VERSION >= (1, 7, 0) and django_is_in_test_mode():
    from .migrate_schemas import MigrateSchemasCommand
    Command = MigrateSchemasCommand
Exemple #11
0
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
"""command overrides."""
from tenant_schemas.management.commands import migrate
from tenant_schemas.utils import django_is_in_test_mode

from .migrate_schemas import Command as MigrateSchemasCommand


class Command(migrate.Command):
    """Override the migrate command from django-tenant-schemas.

    This override is here to workaround a dead upstream.
    This enables django-tenant_schemas to work with Django 3.1.x
    """

    requires_system_checks = []


if django_is_in_test_mode():
    Command = MigrateSchemasCommand  # noqa: F811
from django.conf import settings
from django.core.management.base import CommandError, BaseCommand

from tenant_schemas.management.commands.migrate_schemas import Command as MigrateSchemasCommand
from tenant_schemas.utils import django_is_in_test_mode


class Command(BaseCommand):

    def handle(self, *args, **options):
        database = options.get('database', 'default')
        if (settings.DATABASES[database]['ENGINE'] == 'tenant_schemas.postgresql_backend' or
                MigrateCommand is BaseCommand):
            raise CommandError("migrate has been disabled, for database '{0}'. Use migrate_schemas "
                               "instead. Please read the documentation if you don't know why you "
                               "shouldn't call migrate directly!".format(database))
        super(Command, self).handle(*args, **options)


if django_is_in_test_mode():
    Command = MigrateSchemasCommand