Skip to content

Commit

Permalink
Add Django 5.1 compatibility to MigrationAutodetectorMixin (AmbitionE…
Browse files Browse the repository at this point in the history
…ng#156)

Type: feature
  • Loading branch information
pfouque authored Aug 26, 2024
1 parent d1f027c commit 13dea27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
34 changes: 29 additions & 5 deletions pgtrigger/migrations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import contextlib
import re

from django import __version__ as DJANGO_VERSION
from django.apps import apps
from django.db import transaction
from django.db.migrations.operations.fields import AddField
from django.db.migrations.operations.models import CreateModel, IndexOperation

from pgtrigger import compiler, utils

if DJANGO_VERSION >= "5.1":
from django.db.migrations.autodetector import OperationDependency

OPERATION_DEPENDENCY_CREATE = OperationDependency.Type.CREATE
else:
OperationDependency = lambda *args: tuple((args)) # noqa: E731
OPERATION_DEPENDENCY_CREATE = True


def _add_trigger(schema_editor, model, trigger):
"""Add a trigger to a model."""
Expand Down Expand Up @@ -145,7 +154,11 @@ def _inject_m2m_dependency_in_proxy(proxy_op):
for field in creator._meta.many_to_many:
if field.remote_field.through == model:
app_label, model_name = creator._meta.label_lower.split(".")
proxy_op._auto_deps.append((app_label, model_name, field.name, True))
proxy_op._auto_deps.append(
OperationDependency(
app_label, model_name, field.name, OPERATION_DEPENDENCY_CREATE
)
)


class MigrationAutodetectorMixin:
Expand Down Expand Up @@ -230,10 +243,13 @@ def generate_created_models(self):
}

related_dependencies = [
(app_label, model_name, name, True) for name in sorted(related_fields)
OperationDependency(app_label, model_name, name, OPERATION_DEPENDENCY_CREATE)
for name in sorted(related_fields)
]
# Depend on the model being created
related_dependencies.append((app_label, model_name, None, True))
related_dependencies.append(
OperationDependency(app_label, model_name, None, OPERATION_DEPENDENCY_CREATE)
)

for trigger in model_state.options.pop("triggers", []):
self.add_operation(
Expand Down Expand Up @@ -265,7 +281,11 @@ def generate_created_proxies(self):
self.add_operation(
app_label,
self._get_add_trigger_op(model=model, trigger=trigger),
dependencies=[(app_label, model_name, None, True)],
dependencies=[
OperationDependency(
app_label, model_name, None, OPERATION_DEPENDENCY_CREATE
)
],
)

def generate_deleted_proxies(self):
Expand All @@ -278,7 +298,11 @@ def generate_deleted_proxies(self):
self.add_operation(
app_label,
RemoveTrigger(model_name=model_name, name=trigger.name),
dependencies=[(app_label, model_name, None, True)],
dependencies=[
OperationDependency(
app_label, model_name, None, OPERATION_DEPENDENCY_CREATE
)
],
)

super().generate_deleted_proxies()
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ envlist =
py312-django42-psycopg3
py{310,311,312}-django50-psycopg2
py312-django50-psycopg3
py{310,311,312}-django51-psycopg2
py312-django51-psycopg3
report

[testenv]
Expand All @@ -22,6 +24,7 @@ deps =
django32: Django>=3.2,<3.3
django42: Django>=4.2,<4.3
django50: Django>=5.0rc1,<5.1
django51: Django>=5.1rc1,<5.2
psycopg2: psycopg2-binary
psycopg3: psycopg[binary]
commands =
Expand Down

0 comments on commit 13dea27

Please sign in to comment.