{"id":17005944,"url":"https://github.com/friskes/capture-db-queries","last_synced_at":"2025-07-22T12:33:40.208Z","repository":{"id":234962471,"uuid":"789853972","full_name":"Friskes/capture-db-queries","owner":"Friskes","description":"Class to simplify the search for slow and suboptimal sql queries to the database in django projects.","archived":false,"fork":false,"pushed_at":"2025-03-04T08:23:04.000Z","size":166,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T07:14:55.824Z","etag":null,"topics":["capture-queries","context-manager","database","decorator","django","drf","explain","iterator","mysql","oracle","postgresql","pytest","sqlite3","testing","unittest"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Friskes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-04-21T18:23:07.000Z","updated_at":"2025-03-04T08:19:42.000Z","dependencies_parsed_at":"2024-05-01T22:18:03.636Z","dependency_job_id":"157ca8b6-e1c1-4bac-b62f-5c5944d6bd01","html_url":"https://github.com/Friskes/capture-db-queries","commit_stats":{"total_commits":8,"total_committers":1,"mean_commits":8.0,"dds":0.0,"last_synced_commit":"3577bd07f58e01d046c8d934b3ae8d49fbabcc8f"},"previous_names":["friskes/capture-db-queries"],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/Friskes/capture-db-queries","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friskes%2Fcapture-db-queries","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friskes%2Fcapture-db-queries/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friskes%2Fcapture-db-queries/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friskes%2Fcapture-db-queries/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Friskes","download_url":"https://codeload.github.com/Friskes/capture-db-queries/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friskes%2Fcapture-db-queries/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266496045,"owners_count":23938657,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["capture-queries","context-manager","database","decorator","django","drf","explain","iterator","mysql","oracle","postgresql","pytest","sqlite3","testing","unittest"],"created_at":"2024-10-14T05:04:35.203Z","updated_at":"2025-07-22T12:33:40.172Z","avatar_url":"https://github.com/Friskes.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Class to simplify the search for slow and suboptimal sql queries to the database in django projects.\n\n\u003cdiv align=\"center\"\u003e\n\n| Project   |     | Status                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n|-----------|:----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| CI/CD     |     | [![Latest Release](https://github.com/Friskes/capture-db-queries/actions/workflows/publish-to-pypi.yml/badge.svg)](https://github.com/Friskes/capture-db-queries/actions/workflows/publish-to-pypi.yml)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |\n| Quality   |     | [![Coverage](https://codecov.io/github/Friskes/capture-db-queries/graph/badge.svg?token=vKez4Pycrc)](https://codecov.io/github/Friskes/capture-db-queries)                                                                                                                                                                                                                                                                                                                               |\n| Package   |     | [![PyPI - Version](https://img.shields.io/pypi/v/capture-db-queries?labelColor=202235\u0026color=edb641\u0026logo=python\u0026logoColor=edb641)](https://badge.fury.io/py/capture-db-queries) ![PyPI - Support Python Versions](https://img.shields.io/pypi/pyversions/capture-db-queries?labelColor=202235\u0026color=edb641\u0026logo=python\u0026logoColor=edb641) ![Project PyPI - Downloads](https://img.shields.io/pypi/dm/capture-db-queries?logo=python\u0026label=downloads\u0026labelColor=202235\u0026color=edb641\u0026logoColor=edb641)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |\n| Meta      |     | [![types - Mypy](https://img.shields.io/badge/types-Mypy-202235.svg?logo=python\u0026labelColor=202235\u0026color=edb641\u0026logoColor=edb641)](https://github.com/python/mypy) [![License - MIT](https://img.shields.io/badge/license-MIT-202235.svg?logo=python\u0026labelColor=202235\u0026color=edb641\u0026logoColor=edb641)](https://spdx.org/licenses/) [![code style - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/format.json\u0026labelColor=202235)](https://github.com/astral-sh/ruff) |\n\n\u003c/div\u003e\n\n## About class\n- Class allows you to track any sql queries that are executed inside the body of a loop, a decorated function, or a context manager, the body can be executed a specified number of times to get the average query execution time.\n\n- The class allows you to display formatted output data containing brief information about the current iteration of the measurement, display sql queries and explain information on them, as well as summary information containing data on all measurements.\n\n\u003e **Do not use the class inside the business logic of your application, this will greatly slow down the execution of the queries, the class is intended only for the test environment.**\n\n## Install\n1. Install package\n    ```bash\n    pip install capture-db-queries\n    ```\n\n## About class parameters\n\u003e *All parameters are purely optional.*\n\n- Optional parameters:\n    - `assert_q_count`: The expected number of database queries during all `number_runs`, otherwise an exception will be raised: **\"AssertionError: `N` not less than or equal to `N` queries\"**.\n    - `number_runs`: The number of runs of the decorated function or test for loop.\n    - `verbose`: Displaying the final results of test measurements within all `number_runs`.\n    - `advanced_verb`: Displaying the result of each test measurement.\n    - `auto_call_func`: Autorun of the decorated function. *(without passing arguments to the function, since the launch takes place inside the class)*.\n    - `queries`: Displaying colored and formatted SQL queries to the database.\n    - `explain`: Displaying explain information about each query. (has no effect on the original query).\n    - `explain_opts`: Parameters for explain. *(for more information about the parameters for explain, see the documentation for your DBMS).*\n    - `connection`: Connecting to your database, by default: django.db.connection\n\n\n### \u003e WARNING: If you use `pytest-xdist` and run the test with the `-n \u003cworkers\u003e` flag, the results will not be reflected in the terminal. Remove the `-n \u003cworkers\u003e` flag to display them or use `--capture=tee-sys -rP` parameters.\n\n\n## Usage examples\n\n```python\nfrom capture_db_queries import CaptureQueries\n```\n\n```python\nfor ctx in CaptureQueries(number_runs=2, advanced_verb=True):\n    response = self.client.get(url)\n\n\u003e\u003e\u003e Test №1 | Queries count: 10 | Execution time: 0.04s\n\u003e\u003e\u003e Test №2 | Queries count: 10 | Execution time: 0.04s\n\u003e\u003e\u003e Tests count: 2  |  Total queries count: 20  |  Total execution time: 0.08s  |  Median time one test is: 0.041s  |  Vendor: sqlite\n```\n\n#### OR\n\n```python\n@CaptureQueries(number_runs=2, advanced_verb=True)\ndef test_request():\n    response = self.client.get(url)\n\n\u003e\u003e\u003e Test №1 | Queries count: 10 | Execution time: 0.04s\n\u003e\u003e\u003e Test №2 | Queries count: 10 | Execution time: 0.04s\n\u003e\u003e\u003e Tests count: 2  |  Total queries count: 20  |  Total execution time: 0.08s  |  Median time one test is: 0.041s  |  Vendor: sqlite\n```\n\n#### OR\n\n```python\n# NOTE: The with context manager does not support multi-launch number_runs \u003e 1\n# NOTE: Also you can use `async with` if you capture queries in async context.\nwith CaptureQueries(number_runs=1, advanced_verb=True) as ctx:\n    response = self.client.get(url)\n\n\u003e\u003e\u003e Queries count: 10  |  Execution time: 0.04s  |  Vendor: sqlite\n```\n\n\u003e ### Example of output when using queries and explain:\n\n```python\nfor _ in CaptureQueries(advanced_verb=True, queries=True, explain=True):\n    list(Reporter.objects.filter(pk=1))\n    list(Article.objects.filter(pk=1))\n\n\u003e\u003e\u003e Test №1 | Queries count: 2 | Execution time: 0.22s\n\u003e\u003e\u003e\n\u003e\u003e\u003e\n\u003e\u003e\u003e №[1] time=[0.109] explain=['2 0 0 SEARCH TABLE tests_reporter USING INTEGER PRIMARY KEY (rowid=?)']\n\u003e\u003e\u003e SELECT \"tests_reporter\".\"id\",\n\u003e\u003e\u003e     \"tests_reporter\".\"full_name\"\n\u003e\u003e\u003e FROM \"tests_reporter\"\n\u003e\u003e\u003e WHERE \"tests_reporter\".\"id\" = 1\n\u003e\u003e\u003e\n\u003e\u003e\u003e\n\u003e\u003e\u003e №[2] time=[0.109] explain=['2 0 0 SEARCH TABLE tests_article USING INTEGER PRIMARY KEY (rowid=?)']\n\u003e\u003e\u003e SELECT \"tests_article\".\"id\",\n\u003e\u003e\u003e     \"tests_article\".\"pub_date\",\n\u003e\u003e\u003e     \"tests_article\".\"headline\",\n\u003e\u003e\u003e     \"tests_article\".\"content\",\n\u003e\u003e\u003e     \"tests_article\".\"reporter_id\"\n\u003e\u003e\u003e FROM \"tests_article\"\n\u003e\u003e\u003e WHERE \"tests_article\".\"id\" = 1\n\u003e\u003e\u003e\n\u003e\u003e\u003e\n\u003e\u003e\u003e Tests count: 1  |  Total queries count: 2  |  Total execution time: 0.22s  |  Median time one test is: 0.109s  |  Vendor: sqlite\n```\n\n### Customization of the display\n\u003e To customize the display of SQL queries, you can import a list with handlers and remove handlers from it or expand it with your own handlers.\n\n```python\nfrom capture_db_queries import settings, IHandler\n\n# NOTE: The handler must comply with the specified interface.\nclass SomeHandler(IHandler):\n    def handle(self, queries_log):\n        for query in queries_log:\n            query.sql = \"Hello World!\"\n        return queries_log\n\nsettings.PRINTER_HANDLERS.remove(\"capture_db_queries.handlers.ColorizeSqlHandler\")\nsettings.PRINTER_HANDLERS.append(\"path.to.your.handler.SomeHandler\")\n```\n\n## TODO:\n1. Add support for async loop and async func decorator, __call__, __aiter__, __anext__\n2. Add support for other ORM's, SQLAlchemy, etc.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriskes%2Fcapture-db-queries","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffriskes%2Fcapture-db-queries","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriskes%2Fcapture-db-queries/lists"}