Skip to main content

A tool to track SQLAlchemy query modifications with a structured execution tree and diffs.

Project description

SQLAlchemy Query Tracker

SQLAlchemy Query Tracker is a Python package that lets you track modifications made to SQLAlchemy query objects in real time. Using Python’s sys.settrace along with the Rich library, it generates a detailed, nested execution tree showing function calls, conditional branches (if, elif, else, case, switch), and recognized query modifications—with unified diffs between query states.

Features

  • Real-time Tracking: Monitor SQLAlchemy query changes as they occur.
  • Rich Execution Tree: Visualize function calls, conditionals, and query modifications as a nested tree.
  • Unified Diff Output: Display differences between the SQL before and after each modification.
  • Easy Integration: Wrap your session code with a simple context manager—no modifications to your existing queries are needed.

Installation

Download or clone the repository:

git clone https://github.com/jiri-otoupal/sqlalchemy-query-tracker.git
cd sqlalchemy-query-tracker

Alternatively, download the ZIP archive from the repository page and extract it.

Usage

Wrap your SQLAlchemy query code using the context manager provided by the package. For example:

from sqlalchemy.orm import Session
from query_tracker import sql_query_trace
from query_printer import print_query_log

# Define your compile function (this example uses SQLAlchemy's compile)
def compile_sqlalchemy_query(query):
    return str(query.statement.compile(compile_kwargs={"literal_binds": True}))

# Within your session, wrap your code with sql_query_trace
with Session(engine) as session:
    with sql_query_trace(compile_sqlalchemy_query, user_file="test_query_tracking.py") as tracer:
        q = session.query(User)
        # ... perform query modifications here ...
        q = q.filter(User.active == True)
        q = q.order_by(User.name.asc())
    print_query_log(tracer.root)

A complete example is provided in the test_query_tracking.py file.

Requirements

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your enhancements or bug fixes.

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

sqa_tracker-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqa_tracker-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file sqa_tracker-0.1.0.tar.gz.

File metadata

  • Download URL: sqa_tracker-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.0 Windows/10

File hashes

Hashes for sqa_tracker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 324895a115f0cf622ffc863870bcf04b3f8c2fc731cd6b350a4370272f10c213
MD5 ef0520c7b623f3ca2a58f03a29bdb3f5
BLAKE2b-256 6a588686558eedfd5849074e44695a9a2bae22ce2e25fa4ad04459fb9e2f7fa8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqa_tracker-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.0 Windows/10

File hashes

Hashes for sqa_tracker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da0b7af3d4ad5677ea5cecbb0f04063972ad26a20aaf1cadcb902878d980d244
MD5 7958de855cf9c4d9bf25134d6df3f16a
BLAKE2b-256 1cd1f6b49c2fa7a35e8cf3bcacc45152cca0dbd6e093efa6b6dd83b1eca68752

See more details on using hashes here.

Supported by

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