Skip to content

Commit

Permalink
Use correct db in a multi db setup
Browse files Browse the repository at this point in the history
SQLCompiler has a .connection instance which is already aware of what
database it should use. As such, when running EXPLAIN query let's use
that connection instead of connection to a default database.

Fixes #522.
  • Loading branch information
glujan authored and Asif Saif Uddin committed Jan 10, 2022
1 parent 92120cc commit 8f8d159
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
34 changes: 22 additions & 12 deletions project/tests/test_execute_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@


def mock_sql():
mock_sql_query = Mock(spec_set=['_execute_sql', 'query', 'as_sql'])
mock_sql_query = Mock(spec_set=['_execute_sql', 'query', 'as_sql', 'connection'])
mock_sql_query._execute_sql = Mock()
mock_sql_query.query = NonCallableMock(spec_set=['model'])
mock_sql_query.query.model = Mock()
query_string = 'SELECT * from table_name'
mock_sql_query.as_sql = Mock(return_value=(query_string, ()))

mock_sql_query.connection = NonCallableMock(
spec_set=['cursor', 'features', 'ops'],
cursor=Mock(
spec_set=['__call__'],
return_value=NonCallableMagicMock(spec_set=['__enter__', '__exit__', 'execute'])
),
features=NonCallableMock(
spec_set=['supports_explaining_query_execution'],
supports_explaining_query_execution=True
),
ops=NonCallableMock(spec_set=['explain_query_prefix']),
)

return mock_sql_query, query_string


Expand All @@ -29,8 +43,7 @@ def call_execute_sql(cls, request):
}
cls.args = [1, 2]
cls.kwargs = kwargs
with patch('silk.sql.connection'):
execute_sql(cls.mock_sql, *cls.args, **cls.kwargs)
execute_sql(cls.mock_sql, *cls.args, **cls.kwargs)


class TestCallNoRequest(TestCase):
Expand Down Expand Up @@ -86,25 +99,22 @@ def _query(self):
def test_request(self):
DataCollector().configure(request=Request.objects.create(path='/path/to/somewhere'))
sql, _ = mock_sql()
with patch('silk.sql.connection'):
execute_sql(sql)
execute_sql(sql)
query = self._query()
self.assertEqual(query['request'], DataCollector().request)

def test_registration(self):
DataCollector().configure(request=Request.objects.create(path='/path/to/somewhere'))
sql, _ = mock_sql()
with patch('silk.sql.connection'):
execute_sql(sql)
execute_sql(sql)
query = self._query()
self.assertIn(query, DataCollector().queries.values())

def test_explain(self):
DataCollector().configure(request=Request.objects.create(path='/path/to/somewhere'))
sql, qs = mock_sql()
prefix = "EXPLAIN"
with patch('silk.sql.connection') as m:
mock_cursor = m.cursor.return_value.__enter__.return_value
m.ops.explain_query_prefix.return_value = prefix
execute_sql(sql)
mock_cursor.execute.assert_called_once_with(f"{prefix} {qs}", ())
mock_cursor = sql.connection.cursor.return_value.__enter__.return_value
sql.connection.ops.explain_query_prefix.return_value = prefix
execute_sql(sql)
mock_cursor.execute.assert_called_once_with(f"{prefix} {qs}", ())
5 changes: 2 additions & 3 deletions silk/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import traceback

from django.core.exceptions import EmptyResultSet
from django.db import connection
from django.utils import timezone

from silk.collector import DataCollector
Expand All @@ -29,7 +28,7 @@ def _unpack_explanation(result):
yield row


def _explain_query(q, params):
def _explain_query(connection, q, params):
if connection.features.supports_explaining_query_execution:
if SilkyConfig().SILKY_ANALYZE_QUERIES:
# Work around some DB engines not supporting analyze option
Expand Down Expand Up @@ -91,7 +90,7 @@ def execute_sql(self, *args, **kwargs):
if request:
query_dict['request'] = request
if self.query.model.__module__ != 'silk.models':
query_dict['analysis'] = _explain_query(q, params)
query_dict['analysis'] = _explain_query(self.connection, q, params)
DataCollector().register_query(query_dict)
else:
DataCollector().register_silk_query(query_dict)
Expand Down

0 comments on commit 8f8d159

Please sign in to comment.