Skip to main content

Generate compact, LLM-ready database context with schema profiles and inferred join links.

Project description

DB Snooper

DB Snooper generates compact, LLM-ready database context for SQL generation, query debugging, and schema exploration.

Specification: see spec/main.md for the design notes behind profiling, schema linking, and the text-to-SQL pipeline.

It inspects an existing database and produces two useful artifacts:

  • A SQL profile file with DDL, row counts, sampled small tables, and per-column summaries.
  • A Markdown schema-link report with declared PK/FK relationships and inferred join candidates.

This is useful when an AI agent, coding assistant, or text-to-SQL pipeline needs reliable database context without dumping the whole database. Instead of guessing table meanings or join paths, the agent can read the generated profile and link report before writing SQL.

Quick Start

Profile your local MySQL dababase

 uvx db-snooper profile --db-type mysql --user user --password password --database db
 uvx db-snooper links --db-type mysql --user user --password password --database db

This creates:

  • db_profile.sql
  • db_schema_links.md

What The Outputs Contain

The profile .sql file contains:

  • Metadata with db-snooper version, SQL dialect, database name, and a password-hidden URL.
  • CREATE TABLE DDL, indexes, and constraints.
  • Total row counts.
  • Deterministic sampled rows for small tables.
  • Latest and random sampled rows for larger tables.
  • Per-column null, non-null, distinct, numeric range, median, top-value, and shape summaries for larger tables.
  • Redacted values for sensitive column names containing password, passwd, pwd, hash, salt, secret, or token.

The schema links .md file contains:

  • Declared primary-key and foreign-key links from database constraints.
  • Inferred links from name, type, cardinality, and containment evidence.
  • Evidence labels for each inferred join candidate.

Treat inferred links as candidates, not guaranteed joins. Validate them against the user question and the generated profile before writing final SQL.

Database Examples

SQLite

db-snooper profile --db-type sqlite --database path/to/app.sqlite
db-snooper links --db-type sqlite --database path/to/app.sqlite

PostgreSQL

db-snooper profile --db-type postgres --database app_db --user readonly_user --host localhost --ask-password
db-snooper links --db-type postgres --database app_db --user readonly_user --host localhost --ask-password

MySQL

db-snooper profile --db-type mysql --database app_db --user readonly_user --host localhost --ask-password
db-snooper links --db-type mysql --database app_db --user readonly_user --host localhost --ask-password

MariaDB

db-snooper profile --db-type mariadb --database app_db --user readonly_user --host localhost --ask-password
db-snooper links --db-type mariadb --database app_db --user readonly_user --host localhost --ask-password

DuckDB

db-snooper profile --db-type duckdb --database warehouse.duckdb
db-snooper links --db-type duckdb --database warehouse.duckdb

Environment Variables

Connection values can come from environment variables instead of flags:

DB_SNOOPER_DB_TYPE=sqlite \
DB_SNOOPER_DATABASE=eval-dataset/student_club/student_club.sqlite \
db-snooper profile

Supported variables:

  • DB_SNOOPER_DB_TYPE
  • DB_SNOOPER_DATABASE
  • DB_SNOOPER_DB_HOST
  • DB_SNOOPER_DB_PORT
  • DB_SNOOPER_DB_USER
  • DB_SNOOPER_DB_PASSWORD

For server databases, --host defaults to localhost, --port defaults to the database default, and DB Snooper securely prompts for a password when DB_SNOOPER_DB_PASSWORD is not set.

Help

db-snooper -h
db-snooper profile -h
db-snooper links -h

Table filters:

db-snooper profile --db-type sqlite --database app.sqlite --include-tables users,orders,line_items
db-snooper links --db-type sqlite --database app.sqlite --exclude-tables audit_log,temp_imports

Profile options:

  • --small-table-threshold 50: tables with this many rows or fewer are sampled instead of column-profiled.
  • --sample-row-limit 50: maximum sampled rows for small tables.
  • --include-tables table_a,table_b: only profile selected tables.
  • --exclude-tables table_c: skip selected tables.

Schema-link options:

  • --include-tables table_a,table_b: only inspect selected tables.
  • --exclude-tables table_c: skip selected tables.
  • --containment-threshold 0.8: minimum exact containment for inferred links.
  • --max-distinct-values 10000: maximum distinct values loaded per candidate column.

Python API

Use the simple helpers when you have a SQLAlchemy URL:

from db_snooper import generate_profile, generate_schema_links

database_url = "sqlite:///eval-dataset/superhero/superhero.sqlite"

profile_sql = generate_profile(database_url)
schema_links_md = generate_schema_links(database_url)

Use the lower-level API when you already have a SQLAlchemy engine or need options:

from sqlalchemy import create_engine
from db_snooper import ProfileOptions, SchemaLinkOptions, link_schema, profile_database

engine = create_engine("sqlite:///eval-dataset/superhero/superhero.sqlite")

profile_sql = profile_database(
    engine,
    ProfileOptions(sample_row_limit=25, include_tables=frozenset({"superhero", "publisher"})),
)
schema_links_md = link_schema(
    engine,
    SchemaLinkOptions(containment_threshold=0.9),
)

License

The DB Snooper source code is licensed under the MIT License. See LICENCE.

Third-party Python dependencies remain under their own upstream licenses. See THIRD_PARTY_NOTICES.md for a dependency license summary.

The dataset files included under eval-dataset/ are derived from birdsql by The BIRD Team, and are used and redistributed under the Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA 4.0).

These files are not covered by the MIT source-code license. They retain their original CC BY-SA 4.0 terms. Any derivative works that include these files must also be distributed under CC BY-SA 4.0.

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

db_snooper-0.0.1.tar.gz (61.9 kB view details)

Uploaded Source

Built Distribution

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

db_snooper-0.0.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file db_snooper-0.0.1.tar.gz.

File metadata

  • Download URL: db_snooper-0.0.1.tar.gz
  • Upload date:
  • Size: 61.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for db_snooper-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fc540d5c2646cb483b1cd464c91b23cf6a068ac6b338c9d09509abae3c1a6fec
MD5 cc8dfd803c41863ed14e4fb5c7c6d077
BLAKE2b-256 985fe8666a6a3325020ecb73600cdf7dece38c54aa0af6ef3f6086e561c7c7b6

See more details on using hashes here.

File details

Details for the file db_snooper-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: db_snooper-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for db_snooper-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d16dd27d4a306772b1c0a1d0591e08f16d2f5d871d12deff116c9ffd5bf45706
MD5 6736805c267e36981c35d1784e1d6e91
BLAKE2b-256 5c065d94ba2b05d7b0f0a1daa45929b9a064a540ac27e2c75c165b3ce564f1a8

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