| 1 |
from django.db import models |
| 2 |
|
| 3 |
from django_pgschemas.postgresql.base import check_schema_name |
| 4 |
from django_pgschemas.schema import Schema |
| 5 |
from django_pgschemas.signals import ( |
| 6 |
dynamic_tenant_needs_sync, |
| 7 |
dynamic_tenant_post_sync, |
| 8 |
dynamic_tenant_pre_drop, |
| 9 |
) |
| 10 |
from django_pgschemas.utils import ( |
| 11 |
create_or_clone_schema, |
| 12 |
drop_schema, |
| 13 |
schema_exists, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class TenantModel(Schema, models.Model): |
| 18 |
""" |
| 19 |
All tenant models must inherit this class. |
| 20 |
""" |
| 21 |
|
| 22 |
auto_create_schema = True |
| 23 |
""" |
| 24 |
Set this flag to `False` on a parent class if you don't want the schema |
| 25 |
to be automatically created upon save. |
| 26 |
""" |
| 27 |
|
| 28 |
auto_drop_schema = False |
| 29 |
""" |
| 30 |
*USE THIS WITH CAUTION!* |
| 31 |
Set this flag to `True` on a parent class if you want the schema to be |
| 32 |
automatically deleted if the tenant row gets deleted. |
| 33 |
""" |
| 34 |
|
| 35 |
is_dynamic = True |
| 36 |
""" |
| 37 |
Leave this as `True`. Denotes it's a database controlled tenant. |
| 38 |
""" |
| 39 |
|
| 40 |
schema_name = models.CharField(max_length=63, unique=True, validators=[check_schema_name]) |
| 41 |
|
| 42 |
class Meta: |
| 43 |
abstract = True |
| 44 |
|
| 45 |
def save( |
| 46 |
self, |
| 47 |
force_insert: bool = False, |
| 48 |
force_update: bool = False, |
| 49 |
using: str | None = None, |
| 50 |
update_fields: list[str] | None = None, |
| 51 |
verbosity: int = 1, |
| 52 |
) -> None: |
| 53 |
is_new = self.pk is None |
| 54 |
|
| 55 |
super().save( |
| 56 |
force_insert=force_insert, |
| 57 |
force_update=force_update, |
| 58 |
using=using, |
| 59 |
update_fields=update_fields, |
| 60 |
) |
| 61 |
|
| 62 |
if is_new and self.auto_create_schema: |
| 63 |
try: |
| 64 |
self.create_schema(verbosity=verbosity) |
| 65 |
dynamic_tenant_post_sync.send(sender=TenantModel, tenant=self.serializable_fields()) |
| 66 |
except Exception: |
| 67 |
# We failed creating the tenant, delete what we created and re-raise the exception |
| 68 |
self.delete(force_drop=True) |
| 69 |
raise |
| 70 |
elif is_new: |
| 71 |
# Although we are not using the schema functions directly, the signal might be registered by a listener |
| 72 |
dynamic_tenant_needs_sync.send(sender=TenantModel, tenant=self.serializable_fields()) |
| 73 |
elif not is_new and self.auto_create_schema and not schema_exists(self.schema_name): |
| 74 |
# Create schemas for existing models, deleting only the schema on failure |
| 75 |
try: |
| 76 |
self.create_schema(verbosity=verbosity) |
| 77 |
dynamic_tenant_post_sync.send(sender=TenantModel, tenant=self.serializable_fields()) |
| 78 |
except Exception: |
| 79 |
# We failed creating the schema, delete what we created and re-raise the exception |
| 80 |
self.drop_schema() |
| 81 |
raise |
| 82 |
|
| 83 |
def delete( |
| 84 |
self, using: str | None = None, keep_parents: bool = False, force_drop: bool = False |
| 85 |
) -> None: |
| 86 |
""" |
| 87 |
Deletes this row. Drops the tenant's schema if the attribute |
| 88 |
`auto_drop_schema` is `True`. |
| 89 |
""" |
| 90 |
if force_drop or self.auto_drop_schema: |
| 91 |
dynamic_tenant_pre_drop.send(sender=TenantModel, tenant=self.serializable_fields()) |
| 92 |
self.drop_schema() |
| 93 |
|
| 94 |
super().delete(using, keep_parents) |
| 95 |
|
| 96 |
def serializable_fields(self) -> "TenantModel": |
| 97 |
""" |
| 98 |
In certain cases the model isn't serializable so you may want to only |
| 99 |
send the id. |
| 100 |
""" |
| 101 |
return self |
| 102 |
|
| 103 |
def create_schema(self, sync_schema: bool = True, verbosity: int = 1) -> bool: |
| 104 |
""" |
| 105 |
Creates or clones the schema `schema_name` for this tenant. |
| 106 |
""" |
| 107 |
return create_or_clone_schema(self.schema_name, sync_schema, verbosity) |
| 108 |
|
| 109 |
def drop_schema(self) -> bool: |
| 110 |
""" |
| 111 |
Drops the schema. |
| 112 |
""" |
| 113 |
return drop_schema(self.schema_name) |