Verify the compatibility of a Python project with MySQL version 8

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

  • Search for a keyword to find all Python file paths containing SQL text.
  • Iterate through each SQL text found above and ask ChatGPT if it is compatible with MySQL version 8.

 

# %% Finding all file paths which contain the keyword "from MySQLdb import Connection" in the project.

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))

 

# %% Extracts SQL texts from files.


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


# %% Checks the compatibility of the given SQL text with MySQL version 8.


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


# %% Checks the compatibility of SQL texts in the given file paths with MySQL version 8.


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)

 

Sample result

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

  • Run the SQL code using MySQL version 8 and check for any syntax or compatibility errors. This is the most direct way to test for issues.
  • Check for any deprecated or removed SQL features in MySQL 8 and verify your code does not use those. The MySQL documentation lists features that were deprecated or removed between versions.
  • Run the "mysql_upgrade" utility included with MySQL 8 on your existing database to check for incompatibilities. This utility performs checks and converts your metadata to be compatible.
  • Use MySQL Workbench or other tools to analyze your SQL code and check for compatibility issues. Some tools can detect deprecated features or syntax that may cause problems.
  • Check for any data type, storage engine, or function changes between your current MySQL version and version 8. Data types, engines or functions that changed behavior could affect your code.
  • If using stored procedures or functions, check the MySQL server upgrade guide section on stored routine incompatibilities. Syntax or parameters may have changed.
  • Test your application code thoroughly. Even if the SQL itself is compatible, application code interacting with the database may rely on undocumented behaviors that changed.
References

https://iopshub.medium.com/transitioning-from-mysql-7-x-to-mysql-8-0-whats-new-and-how-to-migrate-691dab8e1c9f