Skip to content

Commit ea8416b

Browse files
committed
Added raw mode and other enhancements
1 parent ad2b939 commit ea8416b

File tree

5 files changed

+85
-28
lines changed

5 files changed

+85
-28
lines changed

README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ A powerful CLI tool to extract, deduplicate, and analyze SQL logs for **Cockroac
2626

2727
## 🖼 Sample Output
2828

29-
| Report Type | Preview |
30-
|-------------|---------------------------------------------------|
31-
| HTML | ![HTML Report Screenshot](docs/sample_report.png) |
32-
| Chart | ![Bar Chart](docs/sample_chart.png) |
33-
| CSV | ![CSV Snippet](docs/sample_csv.png) |
34-
| SQL | ![SQL Snippet](docs/sample_sql.png) |
35-
| Markdown | ![Markdown Snippet](docs/sample_md.png) |
29+
| Report Type | Preview |
30+
|-------------|-----------------------------------------------------------------------------------------------------------------------|
31+
| HTML | ![HTML Report Screenshot](https://raw.githubusercontent.com/viragtripathi/crdb-sql-audit/main/docs/sample_report.png) |
32+
| Chart | ![Bar Chart](https://raw.githubusercontent.com/viragtripathi/crdb-sql-audit/main/docs/sample_chart.png) |
33+
| CSV | ![CSV Snippet](https://raw.githubusercontent.com/viragtripathi/crdb-sql-audit/main/docs/sample_csv.png) |
34+
| SQL | ![SQL Snippet](https://raw.githubusercontent.com/viragtripathi/crdb-sql-audit/main/docs/sample_sql.png) |
35+
| Markdown | ![Markdown Snippet](https://raw.githubusercontent.com/viragtripathi/crdb-sql-audit/main/docs/sample_md.png) |
3636

3737

3838
## 📦 Installation
@@ -53,21 +53,49 @@ pip install dist/crdb_sql_audit-0.2.0-py3-none-any.whl
5353
```
5454

5555
## 🧪 Usage
56+
5657
```bash
5758
crdb-sql-audit \
5859
--dir /path/to/logs \
5960
--terms execute,pg_ \
6061
--out output/report
6162
```
6263

63-
### Additional Options
64+
You can also analyze a single file:
65+
66+
```bash
67+
crdb-sql-audit \
68+
--file /path/to/logfile.log \
69+
--terms SELECT,INSERT \
70+
--raw \
71+
--out output/single_file_report
72+
```
73+
74+
> ⚠️ You must provide either `--dir` or `--file`, but not both.
75+
76+
### 🔧 Additional Options
77+
78+
```bash
79+
--dir Directory containing SQL log files (mutually exclusive with --file)
80+
--file Single SQL log file (mutually exclusive with --dir)
81+
--terms Comma-separated search keywords to extract SQL (default: 'execute,pg_')
82+
--raw Treat each matching line as a raw SQL statement (default: False)
83+
--rules Path to YAML rules file (optional, default: built-in PostgreSQL rules)
84+
--out Output file prefix (default: crdb_audit_output/report)
85+
--help Show usage help
86+
--version Show current version
87+
```
88+
89+
### 📘 CLI Help Example
90+
6491
```bash
65-
--rules Path to YAML rules file (optional, default: built-in PostgreSQL rules)
66-
--help Show usage help
67-
--version Show current version
92+
crdb-sql-audit --help
6893
```
6994

95+
![CLI help screenshot](https://raw.githubusercontent.com/viragtripathi/crdb-sql-audit/main/docs/cli_help.png)
96+
7097
### Custom Rules Example
98+
7199
```bash
72100
crdb-sql-audit \
73101
--dir ./logs \

crdb_sql_audit/audit.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,34 @@
88

99
from .rules_engine import load_rules, apply_rules
1010

11-
def extract_sql(logs_dir, search_terms):
11+
def extract_sql(logs_path, search_terms, raw_mode=False):
1212
seen_sql = set()
13-
for filename in os.listdir(logs_dir):
14-
path = os.path.join(logs_dir, filename)
13+
14+
if os.path.isfile(logs_path):
15+
paths = [logs_path]
16+
elif os.path.isdir(logs_path):
17+
paths = [os.path.join(logs_path, f) for f in os.listdir(logs_path)]
18+
else:
19+
raise ValueError(f"The path '{logs_path}' is not a valid file or directory.")
20+
21+
for path in paths:
1522
if os.path.isfile(path):
1623
with open(path, "r", encoding="utf-8", errors="ignore") as f:
1724
for line in f:
1825
if any(term in line for term in search_terms):
19-
match = re.search(r'execute [^:]+: (.+)', line)
20-
if match:
21-
sql = match.group(1).strip()
22-
seen_sql.add(sql)
26+
if raw_mode:
27+
sql = line.strip()
28+
if len(sql) > 5:
29+
seen_sql.add(sql)
2330
else:
24-
func_match = re.findall(r'\b(pg_\w+\s*\(.*?\))', line)
25-
for func in func_match:
26-
seen_sql.add(func.strip())
31+
match = re.search(r'execute [^:]+: (.+)', line)
32+
if match:
33+
sql = match.group(1).strip()
34+
seen_sql.add(sql)
35+
else:
36+
func_match = re.findall(r'\b(pg_\w+\s*\(.*?\))', line)
37+
for func in func_match:
38+
seen_sql.add(func.strip())
2739
return seen_sql
2840

2941

@@ -38,7 +50,7 @@ def analyze_compatibility(seen_sql, rules_path=None):
3850
logging.info(f"⚠️ MATCHED: {sql}")
3951
for m in matches:
4052
logging.info(f" 🔸 Rule: {m['Rule_ID']}{m['Issue']}")
41-
all_issues.extend(matches) # ✅ move this inside the loop
53+
all_issues.extend(matches)
4254

4355
return all_issues
4456

crdb_sql_audit/main.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import argparse
22
import logging
3+
import os
4+
import sys
35
from crdb_sql_audit.audit import extract_sql, analyze_compatibility, generate_reports
46

5-
__version__ = "0.2.5"
7+
__version__ = "0.2.6"
68

79
logging.basicConfig(
810
level=logging.INFO,
@@ -13,32 +15,47 @@
1315
]
1416
)
1517

16-
1718
def main():
1819
parser = argparse.ArgumentParser(
1920
description="🔍 Analyze SQL logs for CockroachDB compatibility.",
2021
formatter_class=argparse.ArgumentDefaultsHelpFormatter
2122
)
2223

2324
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
24-
parser.add_argument("--dir", required=True, help="Directory with SQL log files")
25+
parser.add_argument("--dir", help="Directory containing SQL log files")
26+
parser.add_argument("--file", help="Single SQL log file")
2527
parser.add_argument("--terms", default="execute,pg_", help="Comma-separated search keywords to extract SQL (default: 'execute,pg_')")
28+
parser.add_argument("--raw", action="store_true", help="Treat each matching line as raw SQL (skip extraction)")
2629
parser.add_argument("--out", default="crdb_audit_output/report", help="Output prefix for reports")
2730
parser.add_argument("--rules", required=False, help="Path to YAML rules file (defaults to Postgres rules)")
2831
args = parser.parse_args()
2932

33+
if not args.dir and not args.file:
34+
parser.print_help()
35+
sys.exit(1)
36+
if args.dir and args.file:
37+
parser.error("Please provide only one of --dir or --file, not both")
38+
if args.dir and not os.path.isdir(args.dir):
39+
parser.error(f"--dir expects a directory. Got: {args.dir}")
40+
if args.file and not os.path.isfile(args.file):
41+
parser.error(f"--file expects a file. Got: {args.file}")
42+
3043
search_terms = [t.strip() for t in args.terms.split(",")]
31-
seen_sql = extract_sql(args.dir, search_terms)
44+
logs_path = args.dir if args.dir else args.file
45+
seen_sql = extract_sql(logs_path, search_terms, raw_mode=args.raw)
3246
issues = analyze_compatibility(seen_sql, rules_path=args.rules)
47+
3348
if args.rules:
3449
logging.info(f"🔍 Using rule file: {args.rules}")
3550
with open(args.rules, "r") as f:
3651
logging.info("🔍 Rule file contents:")
3752
logging.info(f.read())
3853
else:
3954
logging.warning("📦 No rule file provided. Falling back to built-in postgres_to_crdb.yaml.")
55+
4056
logging.info(f"🧪 Total SQL statements analyzed: {len(seen_sql)}")
4157
logging.info(f"🚨 Total issues found: {len(issues)}")
58+
4259
if not issues:
4360
logging.info("🕵️ Showing 5 sample SQL statements that didn't match:")
4461
for sql in list(seen_sql)[:5]:
@@ -47,4 +64,4 @@ def main():
4764
generate_reports(seen_sql, issues, args.out)
4865

4966
if __name__ == "__main__":
50-
main()
67+
main()

docs/cli_help.png

189 KB
Loading

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "crdb-sql-audit"
7-
version = "0.2.5"
7+
version = "0.2.6"
88
description = "Analyze PostgreSQL SQL logs for CockroachDB compatibility"
99
authors = [
1010
{ name = "Virag Tripathi", email = "virag.tripathi@gmail.com" }

0 commit comments

Comments
 (0)