Skip to main content

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

Project description

SQLAlchemy Query Tracker

Downloads 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/sqa_tracker.git
cd sqa_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, Path(__file__).name) 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.

Use print method on tracer to show current status without finish execution

from sqlalchemy.orm import Session
from query_tracker import sql_query_trace


# 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, Path(__file__).name) as tracer:
        q = session.query(User)
        # ... perform query modifications here ...
        q = q.filter(User.active == True)
        tracer.print()
        # other code ...

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.2.tar.gz (5.6 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.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqa_tracker-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 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.2.tar.gz
Algorithm Hash digest
SHA256 34c973258e20afcdcc5c0fd15a4d1f1e9dc132d6a4f37f35db696ced07a48614
MD5 fea01470730a025ebadd4ed448046407
BLAKE2b-256 5e01dcc5382eed58ced8b2aaff314f8693d302c07913f6099d02d2b231bc467f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqa_tracker-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4fead0846969019a38a448c3389dd7ad82d1272c925a962cd5323a66b7468573
MD5 f78031fed4b14351018905c42c383ff4
BLAKE2b-256 77d18187b290454e6c14d1aafa926c0dc65cea805d2022e6ef1d050e5b740678

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