@@ -68,10 +68,47 @@ def get_changed_files(repo_root):
6868 return []
6969
7070def 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
76113def extract_sql_queries (file_path ):
77114 """Extract SQL code blocks from markdown files (fallback method)"""
0 commit comments