Skip to content

Commit 38b43f3

Browse files
committed
Independent unused flag preflight check
1 parent 5b300e1 commit 38b43f3

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

plain-flags/plain/flags/models.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
from plain import models
44
from plain.exceptions import ValidationError
5-
from plain.models import OperationalError, ProgrammingError
6-
from plain.preflight import PreflightResult
7-
from plain.runtime import settings
8-
9-
from .bridge import get_flag_class
10-
from .exceptions import FlagImportError
115

126

137
def validate_flag_name(value: str) -> None:
@@ -61,37 +55,3 @@ class Flag(models.Model):
6155

6256
def __str__(self) -> str:
6357
return self.name
64-
65-
@classmethod
66-
def preflight(cls) -> list[PreflightResult]:
67-
"""
68-
Check for flags that are in the database, but no longer defined in code.
69-
70-
Only returns Info errors because it is valid to leave them if you're worried about
71-
putting the flag back, but they should probably be deleted eventually.
72-
"""
73-
errors = super().preflight()
74-
75-
flag_names = cls.query.all().values_list("name", flat=True)
76-
77-
try:
78-
flag_names = set(flag_names)
79-
except (ProgrammingError, OperationalError):
80-
# The table doesn't exist yet
81-
# (migrations probably haven't run yet),
82-
# so we can't check it.
83-
return errors
84-
85-
for flag_name in flag_names:
86-
try:
87-
get_flag_class(flag_name)
88-
except FlagImportError:
89-
errors.append(
90-
PreflightResult(
91-
fix=f"Flag {flag_name} is not used. Remove the flag from the database or define it in the {settings.FLAGS_MODULE} module.",
92-
warning=True,
93-
id="flags.unused_flags",
94-
)
95-
)
96-
97-
return errors
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from plain.models import OperationalError, ProgrammingError
2+
from plain.preflight import PreflightCheck, PreflightResult, register_check
3+
from plain.runtime import settings
4+
5+
from .bridge import get_flag_class
6+
from .exceptions import FlagImportError
7+
8+
9+
@register_check(name="flags.unused_flags")
10+
class CheckUnusedFlags(PreflightCheck):
11+
"""
12+
Check for flags that are in the database, but no longer defined in code.
13+
14+
Only returns Info errors because it is valid to leave them if you're worried about
15+
putting the flag back, but they should probably be deleted eventually.
16+
"""
17+
18+
def run(self) -> list[PreflightResult]:
19+
# Import here to avoid circular import
20+
from .models import Flag
21+
22+
errors = []
23+
24+
flag_names = Flag.query.all().values_list("name", flat=True)
25+
26+
try:
27+
flag_names = set(flag_names)
28+
except (ProgrammingError, OperationalError):
29+
# The table doesn't exist yet
30+
# (migrations probably haven't run yet),
31+
# so we can't check it.
32+
return errors
33+
34+
for flag_name in flag_names:
35+
try:
36+
get_flag_class(flag_name)
37+
except FlagImportError:
38+
errors.append(
39+
PreflightResult(
40+
fix=f"Flag {flag_name} is not used. Remove the flag from the database or define it in the {settings.FLAGS_MODULE} module.",
41+
warning=True,
42+
id="flags.unused_flags",
43+
)
44+
)
45+
46+
return errors

0 commit comments

Comments
 (0)