Skip to content

Commit 945c15c

Browse files
authored
Fix: Migration command compatibility with packages overriding makemigrations/migrate
Adds dynamic base class lookup to makemigrations and migrate commands. Both commands now scan INSTALLED_APPS and inherit from any custom commands found, ensuring PGViewsAutodetector is applied while preserving customizations from other packages (e.g., django-linear-migrations). - makemigrations/migrate commands use import_module to find custom commands - Continues through INSTALLED_APPS after django_pgviews to discover overrides - Falls back to Django's default commands if none found - Bumps version to 1.1.0
1 parent 4eabb40 commit 945c15c

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1+
from importlib import import_module
2+
3+
from django.apps import apps
14
from django.core.management.commands.makemigrations import Command as MakeMigrationsCommand
25

36
from django_pgviews.db.migrations.autodetector import PGViewsAutodetector
47

58

6-
class Command(MakeMigrationsCommand):
9+
def get_base_makemigrations_command():
10+
"""
11+
Find the makemigrations command from apps loaded before django_pgviews.
12+
13+
This ensures compatibility with other packages that override makemigrations
14+
(e.g., django-linear-migrations) by inheriting from their command instead
15+
of Django's base command directly.
16+
"""
17+
for app_config in apps.get_app_configs():
18+
# Stop when we reach django_pgviews to avoid circular dependency
19+
if app_config.name == "django_pgviews":
20+
continue
21+
try:
22+
# Try to load makemigrations command from this app
23+
module = import_module("{}.management.commands.{}".format(app_config.name, "makemigrations"))
24+
# Found a custom makemigrations Command class, use it as base
25+
return module.Command
26+
except (ImportError, AttributeError):
27+
# This app doesn't have a custom makemigrations command
28+
continue
29+
# No custom command found, use Django's default
30+
return MakeMigrationsCommand
31+
32+
33+
class Command(get_base_makemigrations_command()):
34+
"""Django 6.0 compatible makemigrations with PGViewsAutodetector."""
35+
736
autodetector = PGViewsAutodetector
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
from importlib import import_module
2+
3+
from django.apps import apps
14
from django.core.management.commands.migrate import Command as MigrateCommand
25

36
from django_pgviews.db.migrations.autodetector import PGViewsAutodetector
47

58

6-
class Command(MigrateCommand):
9+
def get_base_migrate_command():
10+
"""
11+
Find the migrate command from apps loaded before django_pgviews.
12+
13+
This ensures compatibility with other packages that override migrate
14+
by inheriting from their command instead of Django's base command directly.
15+
"""
16+
for app_config in apps.get_app_configs():
17+
# Stop when we reach django_pgviews to avoid circular dependency
18+
if app_config.name == "django_pgviews":
19+
continue
20+
try:
21+
# Try to load migrate command from this app
22+
module = import_module("{}.management.commands.{}".format(app_config.name, "migrate"))
23+
# Found a custom migrate Command class, use it as base
24+
return module.Command
25+
except (ImportError, AttributeError):
26+
# This app doesn't have a custom migrate command
27+
continue
28+
# No custom command found, use Django's default
29+
return MigrateCommand
30+
31+
32+
class Command(get_base_migrate_command()):
33+
"""Django 6.0 compatible migrate with PGViewsAutodetector."""
34+
735
autodetector = PGViewsAutodetector

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-pgviews-redux"
3-
version = "1.0.1"
3+
version = "1.1.0"
44
description = "Create and manage Postgres SQL Views in Django"
55
authors = ["Mikuláš Poul <mikulas.poul@xelix.com>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)