Skip to main content

Adds a comment to SQL queries pointing to their place of origin

Project description

django-sql-tagger

Adds a comment to SQL queries pointing to their place of origin

Usage

Install:

pip install django-sql-tagger

Configure and add to INSTALLED_APPS in your settings.py file:

INSTALLED_APPS = [
    ...
    'django_sql_tagger',
    ...
]

SQL_TAGGER_CODE_ROOT = BASE_DIR
SQL_TAGGER_PATH_REPLACEMENTS = [
    (r'^myapp/', 'a/'),
]

Settings

SQL_TAGGER_CODE_ROOT - The root of your codebase. This is used to find out which stack frames belong to your application code.

SQL_TAGGER_PATH_REPLACEMENTS - A list of tuples of regular expressions and replacements. This is used to shorten paths in the comments. For example, if you have a file at /myapp/views.py and you want to replace /myapp/ with a/, you would add (r'^myapp/', 'a/') to the list.

Example

See the testsite/ directory for an example project using this package.

from django.core.management import BaseCommand
from django.db import connection

from django_sql_tagger import transaction
from testapp.models import Website


class Command(BaseCommand):
    def handle(self, *args, **options):
        Website.objects.first()

        with transaction.atomic():
            Website.objects.first()

        with transaction.atomic():
            with transaction.atomic():
                Website.objects.first()

        with transaction.atomic(tag='xxx'):
            Website.objects.first()

The above command executes the following SQL queries:

/* ta/m/c/example.py:10 */ SELECT "testapp_website"."id", "testapp_website"."name", "testapp_website"."url" FROM "testapp_website"; args=(); alias=default
BEGIN;
/* ta/m/c/example.py:12 |> ta/m/c/example.py:13 */ SELECT "testapp_website"."id", "testapp_website"."name", "testapp_website"."url" FROM "testapp_website";
COMMIT;
BEGIN;
SAVEPOINT "s140328319196032_x1";
/* ta/m/c/example.py:15 |> ta/m/c/example.py:16 |> ta/m/c/example.py:17 */ SELECT "testapp_website"."id", "testapp_website"."name", "testapp_website"."url" FROM "testapp_website";
RELEASE SAVEPOINT "s140328319196032_x1";
COMMIT;
BEGIN;
/* T=xxx ta/m/c/example.py:19 |> ta/m/c/example.py:20 */ SELECT "testapp_website"."id", "testapp_website"."name", "testapp_website"."url" FROM "testapp_website";
COMMIT;

The comments make it easier to identify where the SQL queries are coming from, for example when you see the query in the database log or a database monitoring tool.

License

GPLv3 (see LICENSE file)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-sql-tagger-0.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

django_sql_tagger-0.1.0-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file django-sql-tagger-0.1.0.tar.gz.

File metadata

  • Download URL: django-sql-tagger-0.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for django-sql-tagger-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3423d0bb3a5f68581b731d8503325326770961a53b53283d1ddd73d8dd7a1a3f
MD5 ac89b0a09a28341bebc5b5c47ba04b78
BLAKE2b-256 a6eea7ecb13da15e37dee70aa514cbd7376fbb2e5da05ffa0220c92a1ad05c1d

See more details on using hashes here.

Provenance

File details

Details for the file django_sql_tagger-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sql_tagger-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f13f7c9f913b02650206c3c5101f7f80362f955f992b8c0e2f601b7b69fdfc8
MD5 67f3dc915363a3050ddb1a8be27a1015
BLAKE2b-256 9d5a79ec1058732e1101e881870a1dfedb3f5bf94930df048c036ac42bd9b5ca

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page