Skip to content

Commit

Permalink
Remove some RawSQL. Refs #36
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
atodorov committed Jul 2, 2018
1 parent b79aa36 commit 6918293
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 35 deletions.
17 changes: 0 additions & 17 deletions tcms/core/utils/raw_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 2 additions & 5 deletions tcms/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
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
from django.views.decorators.http import require_GET
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
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 3 additions & 6 deletions tcms/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion tcms/testcases/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions tcms/testplans/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6918293

Please sign in to comment.