Debian Codegraph @

django_pgschemas/management/commands/__init__.py

raw
1 import enum
2 from typing import Any, Callable, Self
3
4 from django.conf import settings
5 from django.core.management.base import BaseCommand, CommandError, CommandParser
6 from django.db.models import Case, CharField, Q, Value, When
7 from django.db.models.functions import Concat
8 from django.db.utils import ProgrammingError
9
10 from django_pgschemas.management.commands._executors import parallel, sequential
11 from django_pgschemas.schema import Schema, get_current_schema
12 from django_pgschemas.utils import (
13 create_schema,
14 dynamic_models_exist,
15 get_clone_reference,
16 get_domain_model,
17 get_tenant_model,
18 )
19
20
21 class CommandScope(enum.Enum):
22 ALL = "all"
23 DYNAMIC = "dynamic"
24 STATIC = "static"
25
26 @classmethod
27 def allow_static(cls) -> list[Self]:
28 return [cls.ALL, cls.STATIC]
29
30 @classmethod
31 def allow_dynamic(cls) -> list[Self]:
32 return [cls.ALL, cls.DYNAMIC]
33
34
35 EXECUTORS = {
36 "sequential": sequential,
37 "parallel": parallel,
38 }
39
40
41 class WrappedSchemaOption:
42 scope = CommandScope.ALL
43 specific_schemas = None
44
45 allow_interactive = True
46 allow_wildcards = True
47
48 def add_arguments(self, parser: CommandParser) -> None:
49 if self.allow_interactive:
50 parser.add_argument(
51 "--noinput",
52 "--no-input",
53 action="store_false",
54 dest="interactive",
55 help="Tells Django to NOT prompt the user for input of any kind.",
56 )
57 parser.add_argument(
58 "-s",
59 "--schema",
60 nargs="+",
61 dest="schemas",
62 help="Schema(s) to execute the current command",
63 )
64 parser.add_argument(
65 "-x",
66 "--exclude-schema",
67 nargs="+",
68 dest="excluded_schemas",
69 help="Schema(s) to exclude when executing the current command",
70 )
71
72 if self.allow_wildcards:
73 parser.add_argument(
74 "-as",
75 "--include-all-schemas",
76 action="store_true",
77 dest="all_schemas",
78 help="Include all schemas when executing the current command",
79 )
80 parser.add_argument(
81 "-ss",
82 "--include-static-schemas",
83 action="store_true",
84 dest="static_schemas",
85 help="Include all static schemas when executing the current command",
86 )
87 parser.add_argument(
88 "-ds",
89 "--include-dynamic-schemas",
90 action="store_true",
91 dest="dynamic_schemas",
92 help="Include all dynamic schemas when executing the current command",
93 )
94 parser.add_argument(
95 "-ts",
96 "--include-tenant-schemas",
97 action="store_true",
98 dest="tenant_schemas",
99 help="Include all tenant-like schemas when executing the current command",
100 )
101
102 parser.add_argument(
103 "--parallel",
104 dest="parallel",
105 action="store_true",
106 help="Run command in parallel mode",
107 )
108 parser.add_argument(
109 "--no-create-schemas",
110 dest="skip_schema_creation",
111 action="store_true",
112 help="Skip automatic creation of non-existing schemas",
113 )
114
115 def get_schemas_from_options(self, **options: Any) -> list[str]:
116 skip_schema_creation = options.get("skip_schema_creation", False)
117 try:
118 schemas = self._get_schemas_from_options(**options)
119 except ProgrammingError:
120 # This happens with unmigrated database.
121 # It can also happen when the tenant model contains unapplied migrations that break.
122 raise CommandError(
123 "Error while attempting to retrieve dynamic schemas. "
124 "Perhaps you need to migrate the 'public' schema first?"
125 )
126 if self.specific_schemas is not None:
127 schemas = [x for x in schemas if x in self.specific_schemas]
128 if not schemas:
129 raise CommandError("This command can only run in %s" % self.specific_schemas)
130 if not skip_schema_creation:
131 for schema in schemas:
132 create_schema(schema, check_if_exists=True, sync_schema=False, verbosity=0)
133 return schemas
134
135 def get_executor_from_options(self, **options: Any) -> Callable[..., list[str]]:
136 return EXECUTORS["parallel"] if options.get("parallel") else EXECUTORS["sequential"]
137
138 def get_scope_display(self) -> str:
139 return "|".join(self.specific_schemas or []) or self.scope.value
140
141 def _get_schemas_from_options(self, **options: Any) -> list[str]:
142 schemas = options.get("schemas") or []
143 excluded_schemas = options.get("excluded_schemas") or []
144 include_all_schemas = options.get("all_schemas") or False
145 include_static_schemas = options.get("static_schemas") or False
146 include_dynamic_schemas = options.get("dynamic_schemas") or False
147 include_tenant_schemas = options.get("tenant_schemas") or False
148 dynamic_ready = dynamic_models_exist()
149 allow_static = self.scope in CommandScope.allow_static()
150 allow_dynamic = self.scope in CommandScope.allow_dynamic()
151 clone_reference = get_clone_reference()
152
153 if (
154 not schemas
155 and not include_all_schemas
156 and not include_static_schemas
157 and not include_dynamic_schemas
158 and not include_tenant_schemas
159 ):
160 if not self.allow_interactive:
161 include_all_schemas = True
162 elif options.get("interactive", True):
163 schema = input(
164 "Enter schema to run command (leave blank for running on '%s' schemas): "
165 % self.get_scope_display()
166 ).strip()
167
168 if schema:
169 schemas.append(schema)
170 else:
171 include_all_schemas = True
172 else:
173 raise CommandError("No schema provided")
174
175 TenantModel = get_tenant_model()
176 has_domains = get_domain_model() is not None
177
178 static_schemas = (
179 [x for x in settings.TENANTS.keys() if x != "default"] if allow_static else []
180 )
181 dynamic_schemas = (
182 TenantModel.objects.values_list("schema_name", flat=True)
183 if TenantModel is not None and dynamic_ready and allow_dynamic
184 else []
185 )
186 if clone_reference and allow_static:
187 static_schemas.append(clone_reference)
188
189 schemas_to_return: set[str] = set()
190
191 if include_all_schemas:
192 if not allow_static and not allow_dynamic:
193 raise CommandError("Including all schemas is NOT allowed")
194 schemas_to_return = schemas_to_return.union(static_schemas + list(dynamic_schemas))
195 if include_static_schemas:
196 if not allow_static:
197 raise CommandError("Including static schemas is NOT allowed")
198 schemas_to_return = schemas_to_return.union(static_schemas)
199 if include_dynamic_schemas:
200 if not allow_dynamic:
201 raise CommandError("Including dynamic schemas is NOT allowed")
202 schemas_to_return = schemas_to_return.union(dynamic_schemas)
203 if include_tenant_schemas:
204 if not allow_dynamic:
205 raise CommandError("Including tenant-like schemas is NOT allowed")
206 schemas_to_return = schemas_to_return.union(dynamic_schemas)
207 if clone_reference:
208 schemas_to_return.add(clone_reference)
209
210 def find_schema_by_reference(reference: str, as_excluded: bool = False) -> str:
211 if reference in settings.TENANTS and reference != "default" and allow_static:
212 return reference
213 elif reference == clone_reference:
214 return reference
215 elif (
216 TenantModel is not None
217 and dynamic_ready
218 and TenantModel.objects.filter(schema_name=reference).exists()
219 and allow_dynamic
220 ):
221 return reference
222 else:
223 local = []
224 if allow_static:
225 local += [
226 schema_name
227 for schema_name, data in settings.TENANTS.items()
228 if schema_name not in ["public", "default"]
229 and any(x for x in data.get("DOMAINS", []) if x.startswith(reference))
230 ]
231 if TenantModel is not None and dynamic_ready and allow_dynamic:
232 local += (
233 TenantModel.objects.annotate(
234 route=Case(
235 When(
236 domains__folder="",
237 then="domains__domain",
238 ),
239 default=Concat(
240 "domains__domain",
241 Value("/"),
242 "domains__folder",
243 output_field=CharField(),
244 ),
245 output_field=CharField(),
246 )
247 if has_domains
248 else Value("")
249 )
250 .filter(Q(schema_name=reference) | Q(route__startswith=reference))
251 .distinct()
252 .values_list("schema_name", flat=True)
253 )
254 if not local:
255 message = (
256 "No schema found for '%s' (excluded)"
257 if as_excluded
258 else "No schema found for '%s'"
259 )
260 raise CommandError(message % reference)
261 if len(local) > 1:
262 message = (
263 "More than one tenant found for schema '%s' by domain (excluded), "
264 "please, narrow down the filter"
265 if as_excluded
266 else "More than one tenant found for schema '%s' by domain, please, narrow down the filter"
267 )
268 raise CommandError(message % reference)
269 return local[0]
270
271 for schema in schemas:
272 included = find_schema_by_reference(schema, as_excluded=False)
273 schemas_to_return.add(included)
274
275 for schema in excluded_schemas:
276 excluded = find_schema_by_reference(schema, as_excluded=True)
277 schemas_to_return -= {excluded}
278
279 return (
280 list(schemas_to_return)
281 if "public" not in schemas_to_return
282 else ["public"] + list(schemas_to_return - {"public"})
283 )
284
285
286 class SchemaCommand(WrappedSchemaOption, BaseCommand):
287 def handle(self, *args: Any, **options: Any) -> None:
288 schemas = self.get_schemas_from_options(**options)
289 executor = self.get_executor_from_options(**options)
290 executor(schemas, self, "_raw_handle_schema", args, options, pass_schema_in_kwargs=True)
291
292 def _raw_handle_schema(self, *args: Any, **kwargs: Any) -> None:
293 kwargs.pop("schema_name")
294 self.handle_schema(get_current_schema(), *args, **kwargs)
295
296 def handle_schema(self, schema: Schema, *args: Any, **options: Any) -> None:
297 raise NotImplementedError
298
299
300 class StaticSchemaCommand(SchemaCommand):
301 scope = CommandScope.STATIC
302
303
304 class DynamicSchemaCommand(SchemaCommand):
305 scope = CommandScope.DYNAMIC