def handle(self, *args, **options):
        verbosity = options.get('verbosity', 1)
        # First check if public schema is synced already
        write = self.stdout.write
        if len(args) == 0:
            raise CommandError('You need to specify schema names to sync!')

        if args[0] != conn.PUBLIC_SCHEMA:
            conn.schema = conn.PUBLIC_SCHEMA
            # There are no tables in public schema
            if not conn.introspection.table_names():
                raise CommandError('You should sync the public schema first!')

        for schema in args:
            conn.schema = schema
            if not conn.schema_exists(schema):
                conn.create_schema(schema)
            auth_user_not_in_tenant = 'auth.User' in FORCED_MODELS or 'auth' not in TENANT_APP_LABELS
            if (conn.schema_is_public() and not is_shared(auth_app.User) or
               (not conn.schema_is_public() and auth_user_not_in_tenant)):
                    if verbosity > 1:
                        write("Disconnect auth.User signal handlers, as it is not synced...")
                    post_syncdb.disconnect(create_permissions,
                                           dispatch_uid="django.contrib.auth.management.create_permissions")
                    post_syncdb.disconnect(create_superuser, sender=auth_app,
                                           dispatch_uid="django.contrib.auth.management.create_superuser")

            call_command('syncdb', **options)
Beispiel #2
0
    def save(self, create_schema=True, *args, **kwargs):
        """
        Save the tenant instance in the database after creating the related database schema.
        """
        new = self.pk is None
        if not new and conn.schema not in (self.schema, conn.PUBLIC_SCHEMA):
            raise PermissionDenied("Can't update tenant outside it's own schema or the public schema. "
                                   "Current schema is %s." % conn.schema)

        if new and create_schema:
            conn.validate_schema(self.schema)
            if conn.schema_exists(self.schema):
                raise IntegrityError('Schema %s already exists' % self.schema)

            # CREATE schema in the database, but not actually sync the tables for it
            # kind of "reservation" for the name. Put the syncdb process in a worker thread!
            conn.create_schema(self.schema)
            post_schema_create.send(sender=BaseTenant, tenant=self)

        # only save tenant instance in the database if everything was ok
        super(BaseTenant, self).save(*args, **kwargs)