Esempio n. 1
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,
                         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
Esempio n. 2
0
    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
Esempio n. 3
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)
        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
Esempio n. 4
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)

        return True