To Nha Notes | Dec. 15, 2023, 6:37 p.m.
Follow these procedures to verify if the Python application code is compatible with MySQL version 8
import re
text = """
batch/activity_label.py:
13: from MySQLdb import Connection, IntegrityError
batch/basic_analytics_metrics_aggregation.py:
11: from MySQLdb import Connection, IntegrityError
batch/campaign_widget.py:
10: from MySQLdb import Connection, IntegrityError
... (remaining text)
batch/athena/dataloader/usergroupuser.py:
12: from MySQLdb import Connection
batch/dataload/enduser_activity_tag.py:
8: from MySQLdb import Connection
batch/dataload/enduser_cv_point_conversion.py:
7: from MySQLdb import Connection
batch/dataload/enduser_segment_tag.py:
8: from MySQLdb import Connection
batch/dataload/enduser_transition_point_tag.py:
8: from MySQLdb import Connection
utils/db.py:
8: from MySQLdb import Connection, OperationalError
"""
file_paths = re.findall(r"batch/.*\.py", text)
print("\n".join(file_paths))
import re
def extract_sql_texts(file_paths):
"""
Extracts SQL texts from files.
Args:
file_paths (list): List of file paths.
Returns:
list: List of extracted SQL texts.
"""
pattern = r"(sql|sql_select|sql_update|sql_insert|sql_delete): str\s*=\s*('''|\"\"\")([\s\S]*?)('''|\"\"\")"
sql_texts = []
for file_path in file_paths:
with open(file_path, "r") as file:
file_content = file.read()
results = re.findall(pattern, file_content)
if results:
for result in results:
sql_text = result[2]
sql_texts.append(sql_text)
else:
print(f"No SQL text found in file {file_path}.")
return sql_texts
import os
from openai import OpenAI
os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>"
client = OpenAI()
def check_sql_compatibility(sql_text):
"""
Checks the compatibility of the given SQL text with MySQL version 8.
Args:
sql_text (str): The SQL text to be checked.
Returns:
str: The response indicating the compatibility status.
"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": f"Please check if the sql text {sql_text} compatible with mysql version 8.",
}
],
)
return response.choices[0].message.content
from time import sleep
COLOR_OK = "\033[92m"
COLOR_NG = "\033[91m"
def check_sql_compatibility_for_file_paths(file_paths):
"""
Checks the compatibility of SQL texts in the given file paths with MySQL version 8.
Args:
file_paths (list): List of file paths.
Returns:
None
"""
sql_texts = extract_sql_texts(file_paths)
for index, sql_text in enumerate(sql_texts):
result = check_sql_compatibility(sql_text)
if "compatible" in result:
print(f"{COLOR_OK}No. {index + 1} Compatible. Text result: {result}")
else:
print(f"{COLOR_NG}No. {index + 1} Incompatible. Text result: {result}")
sleep(25) # API usage limit: 3 RPM, 200 RPD
check_sql_compatibility_for_file_paths(file_paths)

Here are a few ways to check if your SQL code is compatible with MySQL version 8