From 7d0c12aa3acc90e5ab33439cf88efdb086d0c14b Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Fri, 22 Mar 2024 14:01:55 +0000 Subject: [PATCH] semgrep: Enforce no bare select_related() calls. Previously, #26419 addressed the majority of these calls, but did not prevent more from creeping in. Remove the one remaining callsite (after the cleanup from the previous commits), and ban any future use of the pattern. --- tools/semgrep-py.yml | 9 +++++++++ zerver/migrations/0273_migrate_old_bot_messages.py | 4 +--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tools/semgrep-py.yml b/tools/semgrep-py.yml index 8f48e394fc..371d5828c0 100644 --- a/tools/semgrep-py.yml +++ b/tools/semgrep-py.yml @@ -43,6 +43,15 @@ rules: exclude: - "**/migrations/" + - id: dont-use-empty-select_related + pattern-either: + - pattern: $X.objects. ... .select_related() + - pattern: $X.objects. ... .prefetch_related() + message: | + Do not use a bare '.select_related()' or '.prefetch_related()', which can join many more tables than expected. Specify the relations to follow explicitly. + languages: [python] + severity: ERROR + - id: dont-import-models-in-migrations patterns: - pattern-not: from zerver.lib.redis_utils import get_redis_client diff --git a/zerver/migrations/0273_migrate_old_bot_messages.py b/zerver/migrations/0273_migrate_old_bot_messages.py index e9067eb67c..233d5f1e79 100644 --- a/zerver/migrations/0273_migrate_old_bot_messages.py +++ b/zerver/migrations/0273_migrate_old_bot_messages.py @@ -38,9 +38,7 @@ def fix_messages(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> No return def get_bot_by_delivery_email(email: str) -> Any: - return UserProfile.objects.select_related().get( - delivery_email__iexact=email.strip(), realm=internal_realm - ) + return UserProfile.objects.get(delivery_email__iexact=email.strip(), realm=internal_realm) notification_bot = get_bot_by_delivery_email(settings.NOTIFICATION_BOT)