Skip to content

Commit

Permalink
Improve database check performance: replace introspection.table_names…
Browse files Browse the repository at this point in the history
…() by a simple cursor query (mwarkentin#171)

* Improve database check performance: replace introspection.table_names() by just a SELECT 1

* add test

* fix tests

* change patch path

* fix local tests

* fix pre-commit

* undo changes

* update history

Co-authored-by: Michael Warkentin <mwarkentin@hey.com>
  • Loading branch information
cristianemoyano and mwarkentin authored Jan 10, 2022
1 parent b06a20f commit b4c3c30
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased
----------

* [`#181 <https://github.com/mwarkentin/django-watchman/pull/181>`_] Update sample project to Django 4.x
* [`#171 <https://github.com/mwarkentin/django-watchman/pull/171>`_] Improve database check performance: replace introspection.table_names() by a simple cursor query (@cristianemoyano)

1.2.0 (2020-09-20)
------------------
Expand Down
16 changes: 16 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
import unittest
from unittest import mock
from unittest.mock import MagicMock, patch

from django.test.client import Client
from django.urls import reverse
Expand All @@ -25,6 +26,21 @@ def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)


class TestDBConnection(unittest.TestCase):
def setUp(self):
self.client = Client()

@patch("watchman.checks.connections")
def test_cursor_is_called(self, mock_connections):
cursor_mock = MagicMock()
mock_connections["default"].cursor().__enter__.return_value = cursor_mock
data = {
"watchman-token": "t1",
}
self.client.get(reverse("status"), data)
cursor_mock.execute.assert_called_once_with("SELECT 1")


class TestWatchmanMultiTokens(unittest.TestCase):
def setUp(self):
self.client = Client()
Expand Down
6 changes: 4 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ def test_check_database_handles_exception(self):
)

def test_check_cache_handles_exception(self):

response = checks._check_cache("foo")
self.assertFalse(response["foo"]["ok"])
self.assertIn(response["foo"]["error"], "The connection 'foo' doesn't exist.")
self.assertIn(
response["foo"]["error"],
"The connection 'foo' doesn't exist.",
)

def test_response_skipped_checks(self):
expected_checks = [
Expand Down
4 changes: 3 additions & 1 deletion watchman/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def _check_databases(databases):

@check
def _check_database(database):
connections[database].introspection.table_names()
connection = connections[database]
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
return {database: {"ok": True}}


Expand Down

0 comments on commit b4c3c30

Please sign in to comment.