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

#425: Added force_create and force_drop/force_create tests. #427

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
4 changes: 2 additions & 2 deletions tenant_schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TenantMixin(models.Model):
class Meta:
abstract = True

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

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

super(TenantMixin, self).save(*args, **kwargs)

if is_new and self.auto_create_schema:
if is_new and (self.auto_create_schema or force_create):
try:
self.create_schema(check_if_exists=True, verbosity=verbosity)
except:
Expand Down
2 changes: 1 addition & 1 deletion tenant_schemas/test/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setUpClass(cls):
cls.sync_shared()
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, force_create=True) # todo: is there any way to get the verbosity from the test command here?

connection.set_tenant(cls.tenant)

Expand Down
30 changes: 30 additions & 0 deletions tenant_schemas/tests/test_tenants.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ def test_non_auto_sync_tenant(self):
tenant.save(verbosity=BaseTestCase.get_verbosity())
self.assertFalse(schema_exists(tenant.schema_name))

def test_tenant_schema_is_force_created(self):
"""
When saving a tenant with force_create=True and has auto_create_schema
as False, the schema should be created when saving the tenant.
"""
self.assertFalse(schema_exists('force_create_tenant'))
tenant = NonAutoSyncTenant(domain_url='something.test.com',
schema_name='force_create_tenant')
tenant.save(verbosity=BaseTestCase.get_verbosity(), force_create=True)
self.assertTrue(schema_exists(tenant.schema_name))

def test_sync_tenant(self):
"""
When editing an existing tenant, all data should be kept.
Expand All @@ -79,6 +90,25 @@ def test_sync_tenant(self):
# test if data is still there
self.assertEqual(DummyModel.objects.count(), 2)

def test_force_drop_schema(self):
"""
When deleting a tenant with force_drop=True, it should delete
the schema associated with the tenant. Regardless of auto_drop_schema
"""
self.assertFalse(schema_exists('auto_drop_tenant'))
Tenant.auto_drop_schema = False
tenant = Tenant(domain_url='something.test.com',
schema_name='auto_drop_tenant')
tenant.save(verbosity=BaseTestCase.get_verbosity())
self.assertTrue(schema_exists(tenant.schema_name))
cursor = connection.cursor()

# Force pending trigger events to be executed
cursor.execute('SET CONSTRAINTS ALL IMMEDIATE')

tenant.delete(force_drop=True)
self.assertFalse(schema_exists(tenant.schema_name))

def test_auto_drop_schema(self):
"""
When deleting a tenant with auto_drop_schema=True, it should delete
Expand Down