From 69182937db1f1c3252d00ec48ccf8644e4661807 Mon Sep 17 00:00:00 2001 From: "Mr. Senko" Date: Sat, 30 Jun 2018 23:11:41 +0300 Subject: [PATCH] Remove some RawSQL. Refs #36 - remove RawSQL.num_case_bugs - not in use at all - replace RawSQL.num_cases with an annotated Count - replace RawSQL.num_runs with an annotated Count - replace RawSQL.num_plans with an annotated Count NOTE: num_children (aka RawSQL.num_plans) is removed from the query used in advanced search b/c I think the value is not used at all --- tcms/core/utils/raw_sql.py | 17 ----------------- tcms/profiles/views.py | 7 ++----- tcms/search/__init__.py | 9 +++------ tcms/testcases/views.py | 1 - tcms/testplans/views.py | 9 +++------ 5 files changed, 8 insertions(+), 35 deletions(-) diff --git a/tcms/core/utils/raw_sql.py b/tcms/core/utils/raw_sql.py index f7430d0ded..c4463e9146 100644 --- a/tcms/core/utils/raw_sql.py +++ b/tcms/core/utils/raw_sql.py @@ -3,23 +3,6 @@ class RawSQL: # pylint: disable=too-few-public-methods """ Record the Raw SQL for operate the database directly. """ - # Following SQL use for count case and run in plan - num_cases = 'SELECT COUNT(*) \ - FROM testcases_testcaseplan \ - WHERE testcases_testcaseplan.plan_id = testplans_testplan.plan_id' - - num_runs = 'SELECT COUNT(*) \ - FROM testruns_testrun \ - WHERE testruns_testrun.plan_id = testplans_testplan.plan_id' - - num_plans = 'SELECT COUNT(*) \ - FROM testplans_testplan AS ch_plans\ - WHERE ch_plans.parent_id = testplans_testplan.plan_id' - - num_case_bugs = 'SELECT COUNT(*) \ - FROM testcases_bug \ - WHERE testcases_bug.case_id = testcases_testcase.case_id' - num_case_run_bugs = 'SELECT COUNT(*) \ FROM testcases_bug \ WHERE testcases_bug.case_run_id = testruns_testcaserun.case_run_id' diff --git a/tcms/profiles/views.py b/tcms/profiles/views.py index 169397c98c..50c416dfb1 100755 --- a/tcms/profiles/views.py +++ b/tcms/profiles/views.py @@ -5,7 +5,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist -from django.db.models import Q +from django.db.models import Count, Q from django.shortcuts import render from django.views.decorators.csrf import csrf_protect from django.views.decorators.http import require_http_methods @@ -13,7 +13,6 @@ from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext_lazy as _ -from tcms.core.utils.raw_sql import RawSQL from tcms.testplans.models import TestPlan from tcms.testruns.models import TestRun from tcms.profiles.models import Bookmark @@ -112,9 +111,7 @@ def dashboard(request): tps = TestPlan.objects.filter(Q(author=request.user) | Q(owner=request.user)) tps = tps.order_by('-plan_id') tps = tps.select_related('product', 'type') - tps = tps.extra(select={ - 'num_runs': RawSQL.num_runs, - }) + tps = tps.annotate(num_runs=Count('run', distinct=True)) tps_active = tps.filter(is_active=True) trs = TestRun.list(runs_query) latest_fifteen_testruns = trs.order_by('-run_id')[:15] diff --git a/tcms/search/__init__.py b/tcms/search/__init__.py index 5d66fc9605..d1ed31ad8f 100644 --- a/tcms/search/__init__.py +++ b/tcms/search/__init__.py @@ -6,12 +6,12 @@ import time +from django.db.models import Count from django.db.models.query import QuerySet from django.http import HttpRequest from django.shortcuts import render from django.views.decorators.http import require_GET -from tcms.core.utils.raw_sql import RawSQL from tcms.management.models import Priority, Product from tcms.search.forms import CaseForm, RunForm, PlanForm from tcms.search.order import order_targets @@ -100,11 +100,8 @@ def _sum_orm_queries(plans, cases, runs, target): plans = plans.filter(case__in=cases).distinct() if runs: plans = plans.filter(run__in=runs).distinct() - plans = plans.extra(select={ - 'num_cases': RawSQL.num_cases, - 'num_runs': RawSQL.num_runs, - 'num_children': RawSQL.num_plans, - }) + plans = plans.annotate(num_cases=Count('case', distinct=True), + num_runs=Count('run', distinct=True)) return plans if target == 'case': if not plans and not runs: diff --git a/tcms/testcases/views.py b/tcms/testcases/views.py index 422eaaaa16..1a6f8b56a5 100644 --- a/tcms/testcases/views.py +++ b/tcms/testcases/views.py @@ -664,7 +664,6 @@ def ajax_search(request, template_name='case/common/json_cases.txt'): 'default_tester__id', 'default_tester__username' ) - tcs = tcs.extra(select={'num_bug': RawSQL.num_case_bugs, }) # columnIndexNameMap is required for correct sorting behavior, 5 should be # product, but we use run.build.product diff --git a/tcms/testplans/views.py b/tcms/testplans/views.py index d2c9383cb6..7ccb5f7512 100644 --- a/tcms/testplans/views.py +++ b/tcms/testplans/views.py @@ -25,7 +25,6 @@ from tcms.core.models import TCMSLog from tcms.core.utils import DataTableResult -from tcms.core.utils.raw_sql import RawSQL from tcms.management.models import EnvGroup from tcms.search import remove_from_request_path from tcms.search.order import order_plan_queryset @@ -149,11 +148,9 @@ def get_all(request, template_name='plan/all.html'): # The cleanest way I can find to get it into one query is to # use QuerySet.extra() # See http://docs.djangoproject.com/en/dev/ref/models/querysets - tps = tps.extra(select={ - 'num_cases': RawSQL.num_cases, - 'num_runs': RawSQL.num_runs, - 'num_children': RawSQL.num_plans, - }) + tps = tps.annotate(num_cases=Count('case', distinct=True), + num_runs=Count('run', distinct=True), + num_children=Count('child_set', distinct=True)) tps = order_plan_queryset(tps, order_by, asc) else: # Set search active plans only by default