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
|
from django.db.migrations.operations.models import CreateModel
from psqlextra.backend.migrations.state import PostgresViewModelState
class PostgresCreateViewModel(CreateModel):
"""Creates the model as a native PostgreSQL 11.x view."""
serialization_expand_args = [
"fields",
"options",
"managers",
"view_options",
]
def __init__(
self,
name,
fields,
options=None,
view_options={},
bases=None,
managers=None,
):
super().__init__(name, fields, options, bases, managers)
self.view_options = view_options or {}
def state_forwards(self, app_label, state):
state.add_model(
PostgresViewModelState(
app_label=app_label,
name=self.name,
fields=list(self.fields),
options=dict(self.options),
bases=tuple(self.bases),
managers=list(self.managers),
view_options=dict(self.view_options),
)
)
def database_forwards(self, app_label, schema_editor, from_state, to_state):
"""Apply this migration operation forwards."""
model = to_state.apps.get_model(app_label, self.name)
if self.allow_migrate_model(schema_editor.connection.alias, model):
schema_editor.create_view_model(model)
def database_backwards(
self, app_label, schema_editor, from_state, to_state
):
"""Apply this migration operation backwards."""
model = from_state.apps.get_model(app_label, self.name)
if self.allow_migrate_model(schema_editor.connection.alias, model):
schema_editor.delete_view_model(model)
def deconstruct(self):
name, args, kwargs = super().deconstruct()
if self.view_options:
kwargs["view_options"] = self.view_options
return name, args, kwargs
def describe(self):
"""Gets a human readable text describing this migration."""
description = super().describe()
description = description.replace("model", "view model")
return description
|