Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using run_syncdb in unit tests on tenant schemas #556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions tenant_schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TenantMixin(models.Model):
class Meta:
abstract = True

def save(self, verbosity=1, *args, **kwargs):
def save(self, verbosity=1, run_syncdb=False, *args, **kwargs):
is_new = self.pk is None

if is_new and connection.schema_name != get_public_schema_name():
Expand All @@ -68,7 +68,7 @@ def save(self, verbosity=1, *args, **kwargs):

if is_new and self.auto_create_schema:
try:
self.create_schema(check_if_exists=True, verbosity=verbosity)
self.create_schema(check_if_exists=True, verbosity=verbosity, run_syncdb=run_syncdb)
except:
# We failed creating the tenant, delete what we created and
# re-raise the exception
Expand All @@ -94,7 +94,7 @@ def delete(self, force_drop=False, *args, **kwargs):
return super(TenantMixin, self).delete(*args, **kwargs)

def create_schema(self, check_if_exists=False, sync_schema=True,
verbosity=1):
verbosity=1, run_syncdb=False):
"""
Creates the schema 'schema_name' for this tenant. Optionally checks if
the schema already exists before creating it. Returns true if the
Expand All @@ -115,6 +115,7 @@ def create_schema(self, check_if_exists=False, sync_schema=True,
call_command('migrate_schemas',
schema_name=self.schema_name,
interactive=False,
verbosity=verbosity)
verbosity=verbosity,
run_syncdb=run_syncdb)

connection.set_schema_to_public()
4 changes: 2 additions & 2 deletions tenant_schemas/test/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setUpClass(cls):
cls.add_allowed_test_domain()
tenant_domain = 'tenant.test.com'
cls.tenant = get_tenant_model()(domain_url=tenant_domain, schema_name='test')
cls.tenant.save(verbosity=0) # todo: is there any way to get the verbosity from the test command here?
cls.tenant.save(verbosity=0, run_syncdb=True) # todo: is there any way to get the verbosity from the test command here?

connection.set_tenant(cls.tenant)

Expand Down Expand Up @@ -58,7 +58,7 @@ def setUpClass(cls):
cls.tenant = TenantModel.objects.get(domain_url=tenant_domain, schema_name='test')
except:
cls.tenant = TenantModel(domain_url=tenant_domain, schema_name='test')
cls.tenant.save(verbosity=0)
cls.tenant.save(verbosity=0, run_syncdb=True)

connection.set_tenant(cls.tenant)

Expand Down