Skip to content

Commit

Permalink
#594 Silk fails on constraint check queries
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCorbin committed Oct 28, 2022
1 parent 4934ffe commit 9dddfbd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.16 on 2022-10-28 08:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('example_app', '0002_alter_blind_photo'),
]

operations = [
migrations.AddConstraint(
model_name='blind',
constraint=models.UniqueConstraint(condition=models.Q(('name', ''), _negated=True), fields=('name',), name='unique_name_if_provided'),
),
]
9 changes: 9 additions & 0 deletions project/example_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ class Blind(Product):

def __str__(self):
return self.name

class Meta:
constraints = [
models.UniqueConstraint(
fields=["name"],
condition=~models.Q(name=""),
name="unique_name_if_provided",
),
]
11 changes: 11 additions & 0 deletions project/example_app/templates/example_app/blind_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>
<h1>Example App</h1>
<p>Use this app for testing and playing around with Silk. Displays a Blind creation form.</p>
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
</body>
</html>
1 change: 1 addition & 0 deletions project/example_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
app_name = 'example_app'
urlpatterns = [
path(route='', view=views.index, name='index'),
path(route='create', view=views.ExampleCreateView.as_view(), name='create'),
]
8 changes: 8 additions & 0 deletions project/example_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Create your views here.
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from example_app import models

from silk.profiling.profiler import silk_profile
Expand All @@ -15,3 +17,9 @@ def do_something_long():
with silk_profile(name='Why do this take so long?'):
do_something_long()
return render(request, 'example_app/index.html', {'blinds': models.Blind.objects.all()})


class ExampleCreateView(CreateView):
model = models.Blind
fields = ['name']
success_url = reverse_lazy('example_app:index')
9 changes: 7 additions & 2 deletions project/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ def setUpClass(cls):
SilkyConfig().SILKY_META = False

def test_profile_request_to_db(self):
client = Client()
DataCollector().configure(Request(reverse('example_app:index')))

with silk_profile(name='test_profile'):
resp = client.get(reverse('example_app:index'))
resp = self.client.get(reverse('example_app:index'))

DataCollector().profiles.values()
assert len(resp.context['blinds']) == 5

def test_profile_request_to_db_with_constraints(self):
DataCollector().configure(Request(reverse('example_app:create')))

resp = self.client.post(reverse('example_app:create'), {'name': 'Foo'})
self.assertEqual(resp.status_code, 302)


class TestAnalyzeQueries(TestCase):

Expand Down
2 changes: 1 addition & 1 deletion silk/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def execute_sql(self, *args, **kwargs):
request = DataCollector().request
if request:
query_dict['request'] = request
if self.query.model.__module__ != 'silk.models':
if getattr(self.query.model, '__module__', '') != 'silk.models':
query_dict['analysis'] = _explain_query(self.connection, q, params)
DataCollector().register_query(query_dict)
else:
Expand Down

0 comments on commit 9dddfbd

Please sign in to comment.