Commands
| Command | Purpose | Example |
|---|---|---|
list | List embedded SQL blocks | py-sql-cleaner list jobs/load_users.py |
format | Format embedded SQL in place | py-sql-cleaner format jobs/load_users.py |
check | Check whether embedded SQL is formatted | py-sql-cleaner check jobs/load_users.py |
extract | Extract embedded SQL into .sql files | py-sql-cleaner extract jobs/load_users.py --out-dir sql |
dialects | List accepted dialect values | py-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.