def store_tenant(self, clone_schema_from, **fields): connection.set_schema_to_public() cursor = connection.cursor() try: tenant = get_tenant_model()(**fields) tenant.auto_create_schema = False tenant.save() clone_schema = CloneSchema(cursor) clone_schema.clone(clone_schema_from, tenant.schema_name) return tenant except exceptions.ValidationError as e: self.stderr.write("Error: %s" % '; '.join(e.messages)) return None except IntegrityError: return None
def store_tenant(self, clone_schema_from, **fields): connections[DEFAULT_DB_ALIAS].set_schema_to_public() cursor = connections[settings.TENANT_DATABASE].cursor() try: tenant = get_tenant_model()(**fields) tenant.auto_create_schema = False tenant.save() clone_schema = CloneSchema(cursor) clone_schema.clone(clone_schema_from, tenant.schema_name) return tenant except exceptions.ValidationError as e: self.stderr.write("Error: %s" % '; '.join(e.messages)) return None except IntegrityError: return None
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 connection = connections[get_tenant_database_alias()] _check_schema_name(self.schema_name) cursor = connection.cursor() if check_if_exists and schema_exists(self.schema_name): return False fake_migrations = get_creation_fakes_migrations() if sync_schema: if fake_migrations: # copy tables and data from provided model schema base_schema = get_tenant_base_schema() clone_schema = CloneSchema() clone_schema.clone_schema(base_schema, self.schema_name) call_command('migrate_schemas', tenant=True, fake=True, schema_name=self.schema_name, interactive=False, verbosity=verbosity) else: # create the schema cursor.execute('CREATE SCHEMA %s' % self.schema_name) call_command('migrate_schemas', tenant=True, schema_name=self.schema_name, interactive=False, verbosity=verbosity) connection.set_schema_to_public()
def store_tenant(self, clone_schema_from, clone_tenant_fields, **fields): connection.set_schema_to_public() try: if clone_tenant_fields: tenant = get_tenant_model().objects.get(schema_name=clone_schema_from) tenant.pk = None tenant.schema_name = fields['schema_name'] else: tenant = get_tenant_model()(**fields) tenant.auto_create_schema = False tenant.save() clone_schema = CloneSchema() clone_schema.clone_schema(clone_schema_from, tenant.schema_name, set_connection=False) return tenant except exceptions.ValidationError as e: self.stderr.write("Error: %s" % '; '.join(e.messages)) return None except IntegrityError as e: self.stderr.write("Error: " + str(e)) return None
def store_tenant(self, clone_schema_from, **fields): connection.set_schema_to_public() cursor = connection.cursor() tm = get_tenant_model() try: if schema_exists(fields["schema_name"]): raise exceptions.ValidationError("Error: Schema %s already exists." % fields["schema_name"]) try: tenant = tm.objects.get(schema_name=fields["schema_name"]) # IMPORTANT: Enter here, only if the row in table 'Tenant Model' exists # and the Schema Postgre not. # If the schema exists one exception will be thrown and # one new row on table 'Tenants' will be created. tm.objects.filter(id=tenant.id).update(**fields) except tm.DoesNotExist: tenant = tm(**fields) tenant.auto_create_schema = False tenant.save() clone_schema = CloneSchema(cursor) clone_schema.clone(clone_schema_from, tenant.schema_name) return tenant # try: # tenant = get_tenant_model()(**fields) # tenant.auto_create_schema = False # tenant.save() # clone_schema = CloneSchema(cursor) # clone_schema.clone(clone_schema_from, tenant.schema_name) # return tenant except exceptions.ValidationError as e: self.stderr.write("Error: %s" % '; '.join(e.messages)) return None except IntegrityError as e: self.stderr.write("Error: %s" % e.message) return None
def store_tenant(self, clone_schema_from, **fields): connection.set_schema_to_public() cursor = connection.cursor() tm = get_tenant_model() try: if schema_exists(fields["schema_name"]): raise exceptions.ValidationError( "Error: Schema %s already exists." % fields["schema_name"]) try: tenant = tm.objects.get(schema_name=fields["schema_name"]) # IMPORTANT: Enter here, only if the row in table 'Tenant Model' exists # and the Schema Postgre not. # If the schema exists one exception will be thrown and # one new row on table 'Tenants' will be created. tm.objects.filter(id=tenant.id).update(**fields) except tm.DoesNotExist: tenant = tm(**fields) tenant.auto_create_schema = False tenant.save() clone_schema = CloneSchema(cursor) clone_schema.clone(clone_schema_from, tenant.schema_name) return tenant # try: # tenant = get_tenant_model()(**fields) # tenant.auto_create_schema = False # tenant.save() # clone_schema = CloneSchema(cursor) # clone_schema.clone(clone_schema_from, tenant.schema_name) # return tenant except exceptions.ValidationError as e: self.stderr.write("Error: %s" % '; '.join(e.messages)) return None except IntegrityError as e: self.stderr.write("Error: %s" % e.message) return None