Skip to content

Commit e40b35b

Browse files
committed
Add a check for the use of PGViewsAutodetector
1 parent ee99893 commit e40b35b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

django_pgviews/apps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def ready(self) -> None:
4747
"""
4848
from django.conf import settings
4949

50+
from .checks import validate_has_pgviews_autodetector # noqa
51+
5052
sync_enabled = getattr(settings, "MATERIALIZED_VIEWS_DISABLE_SYNC_ON_MIGRATE", False) is False
5153

5254
if sync_enabled:

django_pgviews/checks.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Any
2+
3+
from django.core.checks import CheckMessage, Tags, register
4+
from django.core.checks import Warning as CheckWarning
5+
6+
from django_pgviews.db.migrations.autodetector import PGViewsAutodetector
7+
8+
9+
@register(Tags.database) # type: ignore[not-callable]
10+
def validate_has_pgviews_autodetector(**kwargs: Any) -> list[CheckMessage]:
11+
from django.core.management import get_commands, load_command_class
12+
13+
commands = get_commands()
14+
15+
make_migrations = load_command_class(commands["makemigrations"], "makemigrations")
16+
migrate = load_command_class(commands["migrate"], "migrate")
17+
18+
if not issubclass(
19+
migrate.autodetector, # type: ignore[missing-attribute]
20+
PGViewsAutodetector,
21+
) or not issubclass(
22+
make_migrations.autodetector, # type: ignore[missing-attribute]
23+
PGViewsAutodetector,
24+
):
25+
return [
26+
CheckWarning(
27+
(
28+
"If you don't use PGViewsAutodetector on your migrate and makemigrations commands, "
29+
"django_pgviews will not detect and delete that views have been removed. "
30+
"You are seeing this because you or some other dependency has overwritten the commands "
31+
"from django_pgviews. "
32+
),
33+
hint=(
34+
f"The makemigrations.Command.autodetector is {make_migrations.autodetector.__name__}, " # type: ignore[missing-attribute]
35+
f"the migrate.Command.autodetector is {migrate.autodetector.__name__}." # type: ignore[missing-attribute]
36+
),
37+
id="django_pgviews.W001",
38+
)
39+
]
40+
41+
return []

0 commit comments

Comments
 (0)