File: base.md

package info (click to toggle)
python-django-pgschemas 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: python: 3,887; makefile: 33; sh: 10; sql: 2
file content (81 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
## Schema

Base representation of a Postgres schema.

```python

class Schema:
    schema_name: str
    routing: RoutingInfo = None

```

Routing contains information on the routing method used (e.g. domain, session, header). It's filled automatically via middleware but may be missing in other contexts (e.g. management commands).

### Routing info

Information on the routing method.

```python

class DomainInfo:
    domain: str
    folder: str | None = None

class SessionInfo:
    reference: str

class HeadersInfo:
    reference: str

RoutingInfo: TypeAlias = DomainInfo | SessionInfo | HeadersInfo | None

```

## Tenant model

Abstract base class for the tenant model.

```python

class TenantModel(Schema, models.Model):
    auto_create_schema = True
    auto_drop_schema = False

    schema_name = models.CharField(max_length=63, unique=True)

    class Meta:
        abstract = True

```

`auto_create_schema` controls whether a schema is automatically created when a instance of the tenant model is created. `auto_drop_schema` controls whether the schema is automatically deleted when the instance is deleted.

## Domain model

Abstract base class for the domain model. Optional when domain routing is not used.

```python

class DomainModel(models.Model):

    tenant = models.ForeignKey(
        settings.TENANTS["default"]["TENANT_MODEL"],
        db_index=True,
        related_name="domains",
        on_delete=models.CASCADE,
    )

    domain = models.CharField(max_length=253, db_index=True)
    folder = models.SlugField(max_length=253, blank=True, db_index=True)

    is_primary = models.BooleanField(default=True)
    redirect_to_primary = models.BooleanField(default=False)

    class Meta:
        abstract = True
        unique_together = ("domain", "folder")

```

There should only be one instance per tenant with `is_primary` set to `True`. If `redirect_to_primary` is `True` the routing middleware will perform a permanent redirect to whatever domain and folder is marked as primary.