Skip to content

Commit a92d5a5

Browse files
committed
Changes
1 parent e4b7a76 commit a92d5a5

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

.github/workflows/validate-queries.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
name: SQL Query Validation
22
on:
3-
push:
4-
paths:
5-
- 'docs/**/*.md'
6-
- 'blog-*/**/*.md'
73
pull_request:
84
paths:
95
- 'docs/**/*.md'

docs/metrics/metrics-operators/where.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where [VALUE BOOLEAN EXPRESSION | REDUCER BOOLEAN EXPRESSION]
1414
```
1515
## Checking my PR:
1616
```sql
17-
_collector="ABC6" | where type="web"
17+
_collector="ABC7" | where type="web"
1818
```
1919
Where:
2020

scripts/validate_queries.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,47 @@ def get_changed_files(repo_root):
6868
return []
6969

7070
def extract_changed_sql_queries(file_path, base_commit, current_commit):
71-
"""Extract SQL code blocks that were added or modified in the git diff"""
72-
# For now, simplify by validating all SQL in changed files
73-
# This is more reliable than complex diff parsing
74-
return extract_sql_queries(file_path)
71+
"""Extract only the SQL queries that were actually changed in this commit"""
72+
try:
73+
# Get the git diff for this specific file
74+
diff_cmd = ["git", "diff", f"{base_commit}...{current_commit}", "--", file_path]
75+
result = subprocess.run(diff_cmd, capture_output=True, text=True, cwd=get_repo_root())
76+
77+
if result.returncode != 0:
78+
print(f"::warning::Could not get git diff for {file_path}, validating all SQL queries")
79+
return extract_sql_queries(file_path)
80+
81+
diff_content = result.stdout
82+
if not diff_content.strip():
83+
print(f"::info::No changes found in {file_path}")
84+
return []
85+
86+
# Extract only the SQL content that was added/modified
87+
changed_queries = []
88+
lines = diff_content.split('\n')
89+
90+
for line in lines:
91+
# Look for added lines that contain SQL-like content
92+
if line.startswith('+') and not line.startswith('+++'):
93+
content = line[1:].strip() # Remove the '+' prefix
94+
95+
# Check if this line looks like a SQL query
96+
if content and any(keyword in content.lower() for keyword in [
97+
'_collector=', 'metric=', '| where', '| parse', '| count',
98+
'| sum', '| avg', '| json', '| timeslice'
99+
]):
100+
changed_queries.append(content)
101+
102+
if changed_queries:
103+
print(f"📊 Found {len(changed_queries)} changed SQL queries in diff")
104+
return changed_queries
105+
else:
106+
print(f"ℹ️ No SQL query changes detected in {file_path}")
107+
return []
108+
109+
except Exception as e:
110+
print(f"::error::Error parsing git diff for {file_path}: {e}")
111+
return extract_sql_queries(file_path) # Fallback
75112

76113
def extract_sql_queries(file_path):
77114
"""Extract SQL code blocks from markdown files (fallback method)"""

0 commit comments

Comments
 (0)