-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathschema.py
548 lines (421 loc) · 17.8 KB
/
schema.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
from typing import Any, List, Optional
from unittest import mock
from django.core.exceptions import (
FieldDoesNotExist,
ImproperlyConfigured,
SuspiciousOperation,
)
from django.db import transaction
from django.db.models import Field, Model
from psqlextra.type_assertions import is_sql_with_params
from psqlextra.types import PostgresPartitioningMethod
from . import base_impl
from .introspection import PostgresIntrospection
from .side_effects import (
HStoreRequiredSchemaEditorSideEffect,
HStoreUniqueSchemaEditorSideEffect,
)
class PostgresSchemaEditor(base_impl.schema_editor()):
"""Schema editor that adds extra methods for PostgreSQL specific features
and hooks into existing implementations to add side effects specific to
PostgreSQL."""
sql_create_view = "CREATE VIEW %s AS (%s)"
sql_replace_view = "CREATE OR REPLACE VIEW %s AS (%s)"
sql_drop_view = "DROP VIEW IF EXISTS %s"
sql_create_materialized_view = (
"CREATE MATERIALIZED VIEW %s AS (%s) WITH DATA"
)
sql_drop_materialized_view = "DROP MATERIALIZED VIEW %s"
sql_refresh_materialized_view = "REFRESH MATERIALIZED VIEW %s"
sql_refresh_materialized_view_concurrently = (
"REFRESH MATERIALIZED VIEW CONCURRENTLY %s"
)
sql_partition_by = " PARTITION BY %s (%s)"
sql_add_default_partition = "CREATE TABLE %s PARTITION OF %s DEFAULT"
sql_add_hash_partition = "CREATE TABLE %s PARTITION OF %s FOR VALUES WITH (MODULUS %s, REMAINDER %s)"
sql_add_range_partition = (
"CREATE TABLE %s PARTITION OF %s FOR VALUES FROM (%s) TO (%s)"
)
sql_add_list_partition = (
"CREATE TABLE %s PARTITION OF %s FOR VALUES IN (%s)"
)
sql_delete_partition = "DROP TABLE %s"
sql_table_comment = "COMMENT ON TABLE %s IS %s"
side_effects = [
HStoreUniqueSchemaEditorSideEffect(),
HStoreRequiredSchemaEditorSideEffect(),
]
def __init__(self, connection, collect_sql=False, atomic=True):
super().__init__(connection, collect_sql, atomic)
for side_effect in self.side_effects:
side_effect.execute = self.execute
side_effect.quote_name = self.quote_name
self.deferred_sql = []
self.introspection = PostgresIntrospection(self.connection)
def create_model(self, model: Model) -> None:
"""Creates a new model."""
super().create_model(model)
for side_effect in self.side_effects:
side_effect.create_model(model)
def delete_model(self, model: Model) -> None:
"""Drops/deletes an existing model."""
for side_effect in self.side_effects:
side_effect.delete_model(model)
super().delete_model(model)
def refresh_materialized_view_model(
self, model: Model, concurrently: bool = False
) -> None:
"""Refreshes a materialized view."""
sql_template = (
self.sql_refresh_materialized_view_concurrently
if concurrently
else self.sql_refresh_materialized_view
)
sql = sql_template % self.quote_name(model._meta.db_table)
self.execute(sql)
def create_view_model(self, model: Model) -> None:
"""Creates a new view model."""
self._create_view_model(self.sql_create_view, model)
def replace_view_model(self, model: Model) -> None:
"""Replaces a view model with a newer version.
This is used to alter the backing query of a view.
"""
self._create_view_model(self.sql_replace_view, model)
def delete_view_model(self, model: Model) -> None:
"""Deletes a view model."""
sql = self.sql_drop_view % self.quote_name(model._meta.db_table)
self.execute(sql)
def create_materialized_view_model(self, model: Model) -> None:
"""Creates a new materialized view model."""
self._create_view_model(self.sql_create_materialized_view, model)
def replace_materialized_view_model(self, model: Model) -> None:
"""Replaces a materialized view with a newer version.
This is used to alter the backing query of a materialized view.
Replacing a materialized view is a lot trickier than a normal view.
For normal views we can use `CREATE OR REPLACE VIEW`, but for
materialized views, we have to create the new view, copy all
indexes and constraints and drop the old one.
This operation is atomic as it runs in a transaction.
"""
with self.connection.cursor() as cursor:
constraints = self.introspection.get_constraints(
cursor, model._meta.db_table
)
with transaction.atomic():
self.delete_materialized_view_model(model)
self.create_materialized_view_model(model)
for constraint_name, constraint_options in constraints.items():
if not constraint_options["definition"]:
raise SuspiciousOperation(
"Table %s has a constraint '%s' that no definition could be generated for",
(model._meta.db_tabel, constraint_name),
)
self.execute(constraint_options["definition"])
def delete_materialized_view_model(self, model: Model) -> None:
"""Deletes a materialized view model."""
sql = self.sql_drop_materialized_view % self.quote_name(
model._meta.db_table
)
self.execute(sql)
def create_partitioned_model(self, model: Model) -> None:
"""Creates a new partitioned model."""
meta = self._partitioning_properties_for_model(model)
# get the sql statement that django creates for normal
# table creations..
sql, params = self._extract_sql(self.create_model, model)
partitioning_key_sql = ", ".join(
self.quote_name(field_name) for field_name in meta.key
)
# create a composite key that includes the partitioning key
sql = sql.replace(" PRIMARY KEY", "")
if model._meta.pk.name not in meta.key:
sql = sql[:-1] + ", PRIMARY KEY (%s, %s))" % (
self.quote_name(model._meta.pk.name),
partitioning_key_sql,
)
else:
sql = sql[:-1] + ", PRIMARY KEY (%s))" % (partitioning_key_sql,)
# extend the standard CREATE TABLE statement with
# 'PARTITION BY ...'
sql += self.sql_partition_by % (
meta.method.upper(),
partitioning_key_sql,
)
self.execute(sql, params)
def delete_partitioned_model(self, model: Model) -> None:
"""Drops the specified partitioned model."""
return self.delete_model(model)
def add_range_partition(
self,
model: Model,
name: str,
from_values: Any,
to_values: Any,
comment: Optional[str] = None,
) -> None:
"""Creates a new range partition for the specified partitioned model.
Arguments:
model:
Partitioned model to create a partition for.
name:
Name to give to the new partition.
Final name will be "{table_name}_{partition_name}"
from_values:
Start of the partitioning key range of
values that need to be stored in this
partition.
to_values:
End of the partitioning key range of
values that need to be stored in this
partition.
comment:
Optionally, a comment to add on this
partition table.
"""
# asserts the model is a model set up for partitioning
self._partitioning_properties_for_model(model)
table_name = self.create_partition_table_name(model, name)
sql = self.sql_add_range_partition % (
self.quote_name(table_name),
self.quote_name(model._meta.db_table),
"%s",
"%s",
)
with transaction.atomic():
self.execute(sql, (from_values, to_values))
if comment:
self.set_comment_on_table(table_name, comment)
def add_list_partition(
self,
model: Model,
name: str,
values: List[Any],
comment: Optional[str] = None,
) -> None:
"""Creates a new list partition for the specified partitioned model.
Arguments:
model:
Partitioned model to create a partition for.
name:
Name to give to the new partition.
Final name will be "{table_name}_{partition_name}"
values:
Partition key values that should be
stored in this partition.
comment:
Optionally, a comment to add on this
partition table.
"""
# asserts the model is a model set up for partitioning
self._partitioning_properties_for_model(model)
table_name = self.create_partition_table_name(model, name)
sql = self.sql_add_list_partition % (
self.quote_name(table_name),
self.quote_name(model._meta.db_table),
",".join(["%s" for _ in range(len(values))]),
)
with transaction.atomic():
self.execute(sql, values)
if comment:
self.set_comment_on_table(table_name, comment)
def add_hash_partition(
self,
model: Model,
name: str,
modulus: int,
remainder: int,
comment: Optional[str] = None,
) -> None:
"""Creates a new hash partition for the specified partitioned model.
Arguments:
model:
Partitioned model to create a partition for.
name:
Name to give to the new partition.
Final name will be "{table_name}_{partition_name}"
modulus:
Integer value by which the key is divided.
remainder:
The remainder of the hash value when divided by modulus.
comment:
Optionally, a comment to add on this partition table.
"""
# asserts the model is a model set up for partitioning
self._partitioning_properties_for_model(model)
table_name = self.create_partition_table_name(model, name)
sql = self.sql_add_hash_partition % (
self.quote_name(table_name),
self.quote_name(model._meta.db_table),
"%s",
"%s",
)
with transaction.atomic():
self.execute(sql, (modulus, remainder))
if comment:
self.set_comment_on_table(table_name, comment)
def add_default_partition(
self, model: Model, name: str, comment: Optional[str] = None
) -> None:
"""Creates a new default partition for the specified partitioned model.
A default partition is a partition where rows are routed to when
no more specific partition is a match.
Arguments:
model:
Partitioned model to create a partition for.
name:
Name to give to the new partition.
Final name will be "{table_name}_{partition_name}"
comment:
Optionally, a comment to add on this
partition table.
"""
# asserts the model is a model set up for partitioning
self._partitioning_properties_for_model(model)
table_name = self.create_partition_table_name(model, name)
sql = self.sql_add_default_partition % (
self.quote_name(table_name),
self.quote_name(model._meta.db_table),
)
with transaction.atomic():
self.execute(sql)
if comment:
self.set_comment_on_table(table_name, comment)
def delete_partition(self, model: Model, name: str) -> None:
"""Deletes the partition with the specified name."""
sql = self.sql_delete_partition % self.quote_name(
self.create_partition_table_name(model, name)
)
self.execute(sql)
def alter_db_table(
self, model: Model, old_db_table: str, new_db_table: str
) -> None:
"""Alters a table/model."""
super().alter_db_table(model, old_db_table, new_db_table)
for side_effect in self.side_effects:
side_effect.alter_db_table(model, old_db_table, new_db_table)
def add_field(self, model: Model, field: Field) -> None:
"""Adds a new field to an exisiting model."""
super().add_field(model, field)
for side_effect in self.side_effects:
side_effect.add_field(model, field)
def remove_field(self, model: Model, field: Field) -> None:
"""Removes a field from an existing model."""
for side_effect in self.side_effects:
side_effect.remove_field(model, field)
super().remove_field(model, field)
def alter_field(
self,
model: Model,
old_field: Field,
new_field: Field,
strict: bool = False,
) -> None:
"""Alters an existing field on an existing model."""
super().alter_field(model, old_field, new_field, strict)
for side_effect in self.side_effects:
side_effect.alter_field(model, old_field, new_field, strict)
def set_comment_on_table(self, table_name: str, comment: str) -> None:
"""Sets the comment on the specified table."""
sql = self.sql_table_comment % (self.quote_name(table_name), "%s")
self.execute(sql, (comment,))
def _create_view_model(self, sql: str, model: Model) -> None:
"""Creates a new view model using the specified SQL query."""
meta = self._view_properties_for_model(model)
with self.connection.cursor() as cursor:
view_sql = cursor.mogrify(*meta.query).decode("utf-8")
self.execute(sql % (self.quote_name(model._meta.db_table), view_sql))
def _extract_sql(self, method, *args):
"""Calls the specified method with the specified arguments and
intercepts the SQL statement it WOULD execute.
We use this to figure out the exact SQL statement Django would
execute. We can then make a small modification and execute it
ourselves.
"""
with mock.patch.object(self, "execute") as execute:
method(*args)
return tuple(execute.mock_calls[0])[1]
@staticmethod
def _view_properties_for_model(model: Model):
"""Gets the view options for the specified model.
Raises:
ImproperlyConfigured:
When the specified model is not set up
as a view.
"""
meta = getattr(model, "_view_meta", None)
if not meta:
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be a view."
" Create the `ViewMeta` class as a child of '%s'."
)
% (model.__name__, model.__name__)
)
if not is_sql_with_params(meta.query):
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be a view."
" Set the `query` and `key` attribute on the"
" `ViewMeta` class as a child of '%s'"
)
% (model.__name__, model.__name__)
)
return meta
@staticmethod
def _partitioning_properties_for_model(model: Model):
"""Gets the partitioning options for the specified model.
Raises:
ImproperlyConfigured:
When the specified model is not set up
for partitioning.
"""
meta = getattr(model, "_partitioning_meta", None)
if not meta:
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be partitioned."
" Create the `PartitioningMeta` class as a child of '%s'."
)
% (model.__name__, model.__name__)
)
if not meta.method or not meta.key:
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be partitioned."
" Set the `method` and `key` attributes on the"
" `PartitioningMeta` class as a child of '%s'"
)
% (model.__name__, model.__name__)
)
if meta.method not in PostgresPartitioningMethod:
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be partitioned."
" '%s' is not a member of the PostgresPartitioningMethod enum."
)
% (model.__name__, meta.method)
)
if not isinstance(meta.key, list):
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be partitioned."
" Partitioning key should be a list (of field names or values,"
" depending on the partitioning method)."
)
% model.__name__
)
try:
for field_name in meta.key:
model._meta.get_field(field_name)
except FieldDoesNotExist:
raise ImproperlyConfigured(
(
"Model '%s' is not properly configured to be partitioned."
" Field '%s' in partitioning key %s is not a valid field on"
" '%s'."
)
% (model.__name__, field_name, meta.key, model.__name__)
)
return meta
def create_partition_table_name(self, model: Model, name: str) -> str:
return "%s_%s" % (model._meta.db_table.lower(), name.lower())