Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Django 5.1 compatibility to MigrationAutodetectorMixin #156

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +9 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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