File: patched_autodetector.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (329 lines) | stat: -rw-r--r-- 11,888 bytes parent folder | download
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
from contextlib import contextmanager
from unittest import mock

import django

from django.db.migrations import (
    AddField,
    AlterField,
    CreateModel,
    DeleteModel,
    RemoveField,
    RenameField,
)
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.operations.fields import FieldOperation

from psqlextra.models import (
    PostgresMaterializedViewModel,
    PostgresPartitionedModel,
    PostgresViewModel,
)
from psqlextra.types import PostgresPartitioningMethod

from . import operations
from .state import (
    PostgresMaterializedViewModelState,
    PostgresPartitionedModelState,
    PostgresViewModelState,
)

# original `MigrationAutodetector.add_operation`
# function, saved here so the patched version can
# call the original
add_operation = MigrationAutodetector.add_operation


class AddOperationHandler:
    """Handler for when operations are being added to a new migration.

    This is where we intercept operations such as
    :see:CreateModel to replace it with our own.
    """

    def __init__(self, autodetector, app_label, args, kwargs):
        self.autodetector = autodetector
        self.app_label = app_label
        self.args = args
        self.kwargs = kwargs

    def add(self, operation):
        """Adds the specified operation to the list of operations to execute in
        the migration."""

        return add_operation(
            self.autodetector,
            self.app_label,
            operation,
            *self.args,
            **self.kwargs,
        )

    def add_field(self, operation: AddField):
        """Adds the specified :see:AddField operation to the list of operations
        to execute in the migration."""

        return self._transform_view_field_operations(operation)

    def remove_field(self, operation: RemoveField):
        """Adds the specified :see:RemoveField operation to the list of
        operations to execute in the migration."""

        return self._transform_view_field_operations(operation)

    def alter_field(self, operation: AlterField):
        """Adds the specified :see:AlterField operation to the list of
        operations to execute in the migration."""

        return self._transform_view_field_operations(operation)

    def rename_field(self, operation: RenameField):
        """Adds the specified :see:RenameField operation to the list of
        operations to execute in the migration."""

        return self._transform_view_field_operations(operation)

    def _transform_view_field_operations(self, operation: FieldOperation):
        """Transforms operations on fields on a (materialized) view into state
        only operations.

        One cannot add/remove/delete fields on a (materialized) view,
        however, we do want Django's migration system to keep track of
        these kind of changes to the model. The :see:ApplyState
        operation just tells Django the operation was applied without
        actually applying it.
        """

        if django.VERSION >= (4, 0):
            model_identifier = (self.app_label, operation.model_name.lower())
            model_state = (
                self.autodetector.to_state.models.get(model_identifier)
                or self.autodetector.from_state.models[model_identifier]
            )

            if isinstance(model_state, PostgresViewModelState):
                return self.add(
                    operations.ApplyState(state_operation=operation)
                )
        else:
            model = self.autodetector.new_apps.get_model(
                self.app_label, operation.model_name
            )

            if issubclass(model, PostgresViewModel):
                return self.add(
                    operations.ApplyState(state_operation=operation)
                )

        return self.add(operation)

    def add_create_model(self, operation: CreateModel):
        """Adds the specified :see:CreateModel operation to the list of
        operations to execute in the migration."""

        if django.VERSION >= (4, 0):
            model_state = self.autodetector.to_state.models[
                self.app_label, operation.name.lower()
            ]

            if isinstance(model_state, PostgresPartitionedModelState):
                return self.add_create_partitioned_model(operation)
            elif isinstance(model_state, PostgresMaterializedViewModelState):
                return self.add_create_materialized_view_model(operation)
            elif isinstance(model_state, PostgresViewModelState):
                return self.add_create_view_model(operation)
        else:
            model = self.autodetector.new_apps.get_model(
                self.app_label, operation.name
            )

            if issubclass(model, PostgresPartitionedModel):
                return self.add_create_partitioned_model(operation)
            elif issubclass(model, PostgresMaterializedViewModel):
                return self.add_create_materialized_view_model(operation)
            elif issubclass(model, PostgresViewModel):
                return self.add_create_view_model(operation)

        return self.add(operation)

    def add_delete_model(self, operation: DeleteModel):
        """Adds the specified :see:Deletemodel operation to the list of
        operations to execute in the migration."""

        if django.VERSION >= (4, 0):
            model_state = self.autodetector.from_state.models[
                self.app_label, operation.name.lower()
            ]

            if isinstance(model_state, PostgresPartitionedModelState):
                return self.add_delete_partitioned_model(operation)
            elif isinstance(model_state, PostgresMaterializedViewModelState):
                return self.add_delete_materialized_view_model(operation)
            elif isinstance(model_state, PostgresViewModelState):
                return self.add_delete_view_model(operation)
        else:
            model = self.autodetector.old_apps.get_model(
                self.app_label, operation.name
            )

            if issubclass(model, PostgresPartitionedModel):
                return self.add_delete_partitioned_model(operation)
            elif issubclass(model, PostgresMaterializedViewModel):
                return self.add_delete_materialized_view_model(operation)
            elif issubclass(model, PostgresViewModel):
                return self.add_delete_view_model(operation)

        return self.add(operation)

    def add_create_partitioned_model(self, operation: CreateModel):
        """Adds a :see:PostgresCreatePartitionedModel operation to the list of
        operations to execute in the migration."""

        if django.VERSION >= (4, 0):
            model_state = self.autodetector.to_state.models[
                self.app_label, operation.name.lower()
            ]
            partitioning_options = model_state.partitioning_options
        else:
            model = self.autodetector.new_apps.get_model(
                self.app_label, operation.name
            )
            partitioning_options = model._partitioning_meta.original_attrs

        _, args, kwargs = operation.deconstruct()

        if partitioning_options["method"] != PostgresPartitioningMethod.HASH:
            self.add(
                operations.PostgresAddDefaultPartition(
                    model_name=operation.name, name="default"
                )
            )

        partitioned_kwargs = {
            **kwargs,
            "partitioning_options": partitioning_options,
        }

        self.add(
            operations.PostgresCreatePartitionedModel(
                *args,
                **partitioned_kwargs,
            )
        )

    def add_delete_partitioned_model(self, operation: DeleteModel):
        """Adds a :see:PostgresDeletePartitionedModel operation to the list of
        operations to execute in the migration."""

        _, args, kwargs = operation.deconstruct()
        return self.add(
            operations.PostgresDeletePartitionedModel(*args, **kwargs)
        )

    def add_create_view_model(self, operation: CreateModel):
        """Adds a :see:PostgresCreateViewModel operation to the list of
        operations to execute in the migration."""

        if django.VERSION >= (4, 0):
            model_state = self.autodetector.to_state.models[
                self.app_label, operation.name.lower()
            ]
            view_options = model_state.view_options
        else:
            model = self.autodetector.new_apps.get_model(
                self.app_label, operation.name
            )
            view_options = model._view_meta.original_attrs

        _, args, kwargs = operation.deconstruct()

        view_kwargs = {**kwargs, "view_options": view_options}

        self.add(operations.PostgresCreateViewModel(*args, **view_kwargs))

    def add_delete_view_model(self, operation: DeleteModel):
        """Adds a :see:PostgresDeleteViewModel operation to the list of
        operations to execute in the migration."""

        _, args, kwargs = operation.deconstruct()
        return self.add(operations.PostgresDeleteViewModel(*args, **kwargs))

    def add_create_materialized_view_model(self, operation: CreateModel):
        """Adds a :see:PostgresCreateMaterializedViewModel operation to the
        list of operations to execute in the migration."""

        if django.VERSION >= (4, 0):
            model_state = self.autodetector.to_state.models[
                self.app_label, operation.name.lower()
            ]
            view_options = model_state.view_options
        else:
            model = self.autodetector.new_apps.get_model(
                self.app_label, operation.name
            )
            view_options = model._view_meta.original_attrs

        _, args, kwargs = operation.deconstruct()

        view_kwargs = {**kwargs, "view_options": view_options}

        self.add(
            operations.PostgresCreateMaterializedViewModel(
                *args,
                **view_kwargs,
            )
        )

    def add_delete_materialized_view_model(self, operation: DeleteModel):
        """Adds a :see:PostgresDeleteMaterializedViewModel operation to the
        list of operations to execute in the migration."""

        _, args, kwargs = operation.deconstruct()
        return self.add(
            operations.PostgresDeleteMaterializedViewModel(*args, **kwargs)
        )


@contextmanager
def patched_autodetector():
    """Patches the standard Django :seee:MigrationAutodetector for the duration
    of the context.

    The patch intercepts the `add_operation` function to
    customize how new operations are added.

    We have to do this because there is no way in Django
    to extend the auto detector otherwise.
    """

    autodetector_module_path = "django.db.migrations.autodetector"
    autodetector_class_path = (
        f"{autodetector_module_path}.MigrationAutodetector"
    )
    add_operation_path = f"{autodetector_class_path}.add_operation"

    def _patched(autodetector, app_label, operation, *args, **kwargs):
        handler = AddOperationHandler(autodetector, app_label, args, kwargs)

        if isinstance(operation, CreateModel):
            return handler.add_create_model(operation)

        if isinstance(operation, DeleteModel):
            return handler.add_delete_model(operation)

        if isinstance(operation, AddField):
            return handler.add_field(operation)

        if isinstance(operation, RemoveField):
            return handler.remove_field(operation)

        if isinstance(operation, AlterField):
            return handler.alter_field(operation)

        if isinstance(operation, RenameField):
            return handler.rename_field(operation)

        return handler.add(operation)

    with mock.patch(add_operation_path, new=_patched):
        yield