Skip to main content

Commands

CommandPurposeExample
listList embedded SQL blockspy-sql-cleaner list jobs/load_users.py
formatFormat embedded SQL in placepy-sql-cleaner format jobs/load_users.py
checkCheck whether embedded SQL is formattedpy-sql-cleaner check jobs/load_users.py
extractExtract embedded SQL into .sql filespy-sql-cleaner extract jobs/load_users.py --out-dir sql
dialectsList accepted dialect valuespy-sql-cleaner dialects

list

py-sql-cleaner list path/to/file.py

format

py-sql-cleaner format path/to/file.py

Use a database-specific SQLGlot dialect:

py-sql-cleaner format path/to/file.py -d redshift

Currently enabled dialects are generic, mysql, postgres, and redshift. Dialect support is best-effort formatting through SQLGlot. Redshift command-style statements such as COPY and UNLOAD are preserved rather than reformatted.

Preview changes:

py-sql-cleaner format path/to/file.py --dry-run

check

py-sql-cleaner check path/to/file.py

Use the same dialect option as format:

py-sql-cleaner check path/to/file.py -d postgres

extract

py-sql-cleaner extract path/to/file.py --out-dir sql

Format extracted SQL with a database-specific dialect:

py-sql-cleaner extract path/to/file.py --out-dir sql -d postgres

Use read-text replacement mode:

py-sql-cleaner extract path/to/file.py --out-dir sql --replace-mode read-text

Result:

query = Path("sql/query.sql").read_text()
note

py-sql-cleaner does not automatically insert from pathlib import Path in the current MVP.

Specify an output name:

py-sql-cleaner extract path/to/file.py --out-dir sql --name load_users

dialects

py-sql-cleaner dialects

The output lists the dialect values accepted by --dialect / -d.