-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: command to delete invalid queries with invalid settings (#1807)
- Loading branch information
Showing
4 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
backend/maintenance/management/commands/remove_invalid_queries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Django command for removing queries with invalid query settings.""" | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
from jsonschema import Draft7Validator | ||
|
||
from variants.models.queries import SmallVariantQuery | ||
from variants.query_schemas import ( | ||
SCHEMA_QUERY, | ||
FormToQueryJsonConverter, | ||
QueryJsonToFormConverter, | ||
extend_with_default, | ||
) | ||
|
||
DefaultValidatingDraft7Validator = extend_with_default(Draft7Validator) | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Implementation of removing invalid query settigns.""" | ||
|
||
#: Help message displayed on the command line. | ||
help = "Remove queries with invalid query settings." | ||
|
||
def add_arguments(self, parser): | ||
"""Add command line arguments.""" | ||
parser.add_argument( | ||
"--no-dry-run", | ||
action="store_true", | ||
dest="no_dry_run", | ||
default=True, | ||
help="Perfom deleting invalid queries.", | ||
) | ||
|
||
@transaction.atomic | ||
def handle(self, *args, **options): | ||
"""Perform removing incompatible queries.""" | ||
no_dry_run = options.get("no_dry_run", False) | ||
|
||
for i in SmallVariantQuery.objects.all(): | ||
query_settings = FormToQueryJsonConverter().convert(i.query_settings) | ||
try: | ||
DefaultValidatingDraft7Validator(SCHEMA_QUERY).validate(query_settings) | ||
except Exception as e: | ||
self.stdout.write(self.style.ERROR(e)) | ||
self.stdout.write(self.style.ERROR(f"Query {i.id} is invalid. Deleting...")) | ||
if no_dry_run: | ||
i.delete() | ||
|
||
if no_dry_run: | ||
self.stdout.write(self.style.SUCCESS("Incompatible queries have been removed.")) | ||
else: | ||
self.stdout.write(self.style.SUCCESS("Dry run completed. No queries were deleted.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Git LFS file not shown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters