Skip to content

Commit 5e3c099

Browse files
authored
Merge pull request #5451 from rtfd/davidfischer/optimize-repo-queries
Optimize the repos API query
2 parents 96d4e31 + 3aee6a1 commit 5e3c099

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

readthedocs/oauth/models.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,23 @@ def clone_fuzzy_url(self):
179179
def matches(self, user):
180180
"""Projects that exist with repository URL already."""
181181
# Support Git scheme GitHub url format that may exist in database
182-
fuzzy_url = self.clone_url.replace('git://', '').replace('.git', '')
183-
projects = (Project
184-
.objects
185-
.public(user)
186-
.filter(Q(repo=self.clone_url) |
187-
Q(repo__iendswith=fuzzy_url) |
188-
Q(repo__iendswith=fuzzy_url + '.git'))) # yapf: disable
182+
truncated_url = self.clone_url.replace('.git', '')
183+
http_url = self.clone_url.replace('git://', 'https://').replace('.git', '')
184+
185+
projects = Project.objects.public(user).filter(
186+
Q(repo=self.clone_url) |
187+
Q(repo=truncated_url) |
188+
Q(repo=truncated_url + '.git') |
189+
Q(repo=http_url) |
190+
Q(repo=http_url + '.git')
191+
).values('slug')
192+
189193
return [{
190-
'id': project.slug,
194+
'id': project['slug'],
191195
'url': reverse(
192196
'projects_detail',
193197
kwargs={
194-
'project_slug': project.slug,
198+
'project_slug': project['slug'],
195199
},
196200
),
197201
} for project in projects]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.20 on 2019-03-13 17:03
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import readthedocs.projects.validators
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('projects', '0040_increase_path_max_length'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='project',
18+
name='repo',
19+
field=models.CharField(db_index=True, help_text='Hosted documentation repository URL', max_length=255, validators=[readthedocs.projects.validators.RepositoryURLValidator()], verbose_name='Repository URL'),
20+
),
21+
]

readthedocs/projects/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Project(models.Model):
116116
max_length=255,
117117
validators=[validate_repository_url],
118118
help_text=_('Hosted documentation repository URL'),
119+
db_index=True,
119120
)
120121
repo_type = models.CharField(
121122
_('Repository type'),

readthedocs/restapi/serializers.py

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class RemoteRepositorySerializer(serializers.ModelSerializer):
173173
"""Remote service repository serializer."""
174174

175175
organization = RemoteOrganizationSerializer()
176+
177+
# This field does create an additional query per object returned
176178
matches = serializers.SerializerMethodField()
177179

178180
class Meta:

readthedocs/restapi/views/model_views.py

+4
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ def get_queryset(self):
337337
service.adapter.provider_id for service in registry
338338
],
339339
)
340+
341+
# optimizes for the RemoteOrganizationSerializer
342+
query = query.select_related('organization')
343+
340344
return query
341345

342346

0 commit comments

Comments
 (0)