Debian Codegraph @

django_pgschemas/utils.py

raw
1 import gzip
2 import os
3 import re
4 from typing import Any, Callable
5
6 from django.apps import apps
7 from django.conf import settings
8 from django.core.exceptions import ValidationError
9 from django.core.management import call_command
10 from django.db import DEFAULT_DB_ALIAS, ProgrammingError, connection, transaction
11 from django.db.models import Model
12 from django.utils.encoding import force_str
13
14
15 def get_tenant_model(require_ready: bool = True) -> Model | None:
16 "Returns the tenant model."
17 if "default" not in settings.TENANTS:
18 return None
19 return apps.get_model(settings.TENANTS["default"]["TENANT_MODEL"], require_ready=require_ready)
20
21
22 def get_domain_model(require_ready: bool = True) -> Model | None:
23 "Returns the domain model."
24 if "default" not in settings.TENANTS or "DOMAIN_MODEL" not in settings.TENANTS["default"]:
25 return None
26 return apps.get_model(settings.TENANTS["default"]["DOMAIN_MODEL"], require_ready=require_ready)
27
28
29 def get_tenant_database_alias() -> str:
30 return getattr(settings, "PGSCHEMAS_TENANT_DB_ALIAS", DEFAULT_DB_ALIAS)
31
32
33 def get_limit_set_calls() -> bool:
34 return getattr(settings, "PGSCHEMAS_LIMIT_SET_CALLS", False)
35
36
37 def get_clone_reference() -> str | None:
38 if "default" not in settings.TENANTS:
39 return None
40 return settings.TENANTS["default"].get("CLONE_REFERENCE", None)
41
42
43 def is_valid_identifier(identifier: str) -> bool:
44 "Checks the validity of identifier."
45 SQL_IDENTIFIER_RE = re.compile(r"^[_a-zA-Z][_a-zA-Z0-9]{,62}$")
46 return bool(SQL_IDENTIFIER_RE.match(identifier))
47
48
49 def is_valid_schema_name(name: str) -> bool:
50 "Checks the validity of a schema name."
51 SQL_SCHEMA_NAME_RESERVED_RE = re.compile(r"^pg_", re.IGNORECASE)
52 return is_valid_identifier(name) and not SQL_SCHEMA_NAME_RESERVED_RE.match(name)
53
54
55 def check_schema_name(name: str) -> None:
56 """
57 Checks schema name and raises `ValidationError` if `name` is not a
58 valid identifier.
59 """
60 if not is_valid_schema_name(name):
61 raise ValidationError("Invalid string used for the schema name.")
62
63
64 def remove_www(path: str) -> str:
65 if path.startswith("www."):
66 return path[4:]
67 return path
68
69
70 def django_is_in_test_mode() -> bool:
71 """
72 I know this is very ugly! I'm looking for more elegant solutions.
73 See: http://stackoverflow.com/questions/6957016/detect-django-testing-mode
74 """
75 from django.core import mail
76
77 return hasattr(mail, "outbox")
78
79
80 def run_in_public_schema(func: Callable) -> Callable:
81 "Decorator that makes decorated function to be run in the public schema."
82
83 def wrapper(*args: object, **kwargs: object) -> Any:
84 from django_pgschemas.schema import Schema
85
86 with Schema.create(schema_name="public"):
87 return func(*args, **kwargs)
88
89 return wrapper
90
91
92 def schema_exists(schema_name: str) -> bool:
93 "Checks if a schema exists in database."
94 sql = """
95 SELECT EXISTS(
96 SELECT 1
97 FROM pg_catalog.pg_namespace
98 WHERE LOWER(nspname) = LOWER(%s)
99 )
100 """
101 with connection.cursor() as cursor:
102 cursor.execute(sql, (schema_name,))
103 row = cursor.fetchone()
104 if row:
105 exists = row[0]
106 else: # pragma: no cover
107 exists = False
108
109 return exists
110
111
112 @run_in_public_schema
113 def dynamic_models_exist() -> bool:
114 "Checks if tenant model and domain model are ready to be used in the database."
115 sql = """
116 SELECT count(*)
117 FROM information_schema.tables
118 WHERE table_schema = 'public'
119 AND table_name in (%s);
120 """
121 TenantModel = get_tenant_model()
122 DomainModel = get_domain_model()
123
124 models_to_check = []
125
126 if TenantModel is not None:
127 models_to_check.append(TenantModel)
128 if DomainModel is not None:
129 models_to_check.append(DomainModel)
130
131 if not models_to_check:
132 return False
133
134 template = ", ".join(f"'{model._meta.db_table}'" for model in models_to_check)
135
136 with connection.cursor() as cursor:
137 cursor.execute(sql % template)
138 value = cursor.fetchone() == (len(models_to_check),)
139
140 return value
141
142
143 @run_in_public_schema
144 def create_schema(
145 schema_name: str,
146 check_if_exists: bool = False,
147 sync_schema: bool = True,
148 verbosity: int = 1,
149 ) -> bool:
150 """
151 Creates the schema `schema_name`. Optionally checks if the schema already
152 exists before creating it. Returns `True` if the schema was created,
153 `False` otherwise.
154 """
155 check_schema_name(schema_name)
156
157 if check_if_exists and schema_exists(schema_name):
158 return False
159
160 with connection.cursor() as cursor:
161 cursor.execute("CREATE SCHEMA %s" % schema_name)
162
163 if sync_schema:
164 call_command("migrateschema", schemas=[schema_name], verbosity=verbosity)
165
166 return True
167
168
169 @run_in_public_schema
170 def drop_schema(schema_name: str, check_if_exists: bool = True, verbosity: int = 1) -> bool:
171 """
172 Drops the schema. Optionally checks if the schema already exists before
173 dropping it.
174 """
175 if check_if_exists and not schema_exists(schema_name):
176 return False
177
178 with connection.cursor() as cursor:
179 cursor.execute("DROP SCHEMA %s CASCADE" % schema_name)
180
181 return True
182
183
184 class DryRunException(Exception):
185 pass
186
187
188 def _create_clone_schema_function() -> None:
189 """
190 Creates a postgres function `clone_schema` that copies a schema and its
191 contents. Will replace any existing `clone_schema` functions owned by the
192 `postgres` superuser.
193 """
194 with gzip.open(
195 os.path.join(os.path.dirname(os.path.abspath(__file__)), "clone_schema.gz")
196 ) as gzip_file:
197 CLONE_SCHEMA_FUNCTION = (
198 force_str(gzip_file.read())
199 .replace("RAISE NOTICE ' source schema", "RAISE EXCEPTION ' source schema")
200 .replace("RAISE NOTICE ' dest schema", "RAISE EXCEPTION ' dest schema")
201 )
202
203 with connection.cursor() as cursor:
204 cursor.execute(CLONE_SCHEMA_FUNCTION)
205
206
207 @run_in_public_schema
208 def clone_schema(base_schema_name: str, new_schema_name: str, dry_run: bool = False) -> None:
209 """
210 Creates a new schema `new_schema_name` as a clone of an existing schema
211 `base_schema_name`.
212 """
213 check_schema_name(new_schema_name)
214 cursor = connection.cursor()
215
216 # check if the clone_schema function already exists in the db
217 try:
218 cursor.execute(
219 "SELECT 'public.clone_schema(text, text, public.cloneparms[])'::regprocedure"
220 )
221 except ProgrammingError: # pragma: no cover
222 _create_clone_schema_function()
223 transaction.commit()
224
225 try:
226 with transaction.atomic():
227 cursor.callproc("clone_schema", [base_schema_name, new_schema_name, "DATA"])
228 cursor.close()
229 if dry_run:
230 raise DryRunException
231 except DryRunException:
232 cursor.close()
233
234
235 def create_or_clone_schema(schema_name: str, sync_schema: bool = True, verbosity: int = 1) -> bool:
236 """
237 Creates the schema `schema_name`. Optionally checks if the schema already
238 exists before creating it. Returns `True` if the schema was created,
239 `False` otherwise.
240 """
241 check_schema_name(schema_name)
242
243 if schema_exists(schema_name):
244 return False
245
246 clone_reference = get_clone_reference()
247
248 if (
249 clone_reference and schema_exists(clone_reference) and not django_is_in_test_mode()
250 ): # pragma: no cover
251 clone_schema(clone_reference, schema_name)
252 return True
253
254 return create_schema(schema_name, sync_schema=sync_schema, verbosity=verbosity)