From 539e940ab3c8b0eb350b42f9ae8413e1f843bfc7 Mon Sep 17 00:00:00 2001 From: Simon Pahl Date: Thu, 24 Jul 2025 13:33:02 +0200 Subject: [PATCH] Fix python2 compatibility of execution_time_ms (#720) Co-authored-by: Simon Pahl --- changelogs/fragments/fix_python2_compatibility.yml | 3 +++ plugins/modules/mysql_query.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/fix_python2_compatibility.yml diff --git a/changelogs/fragments/fix_python2_compatibility.yml b/changelogs/fragments/fix_python2_compatibility.yml new file mode 100644 index 0000000..4341ec8 --- /dev/null +++ b/changelogs/fragments/fix_python2_compatibility.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - "mysql_query - fix a Python 2 compatibility issue caused by the addition of ``execution_time_ms`` in version 3.12 (see https://github.com/ansible-collections/community.mysql/issues/716)." diff --git a/plugins/modules/mysql_query.py b/plugins/modules/mysql_query.py index 35beeb3..f7c900a 100644 --- a/plugins/modules/mysql_query.py +++ b/plugins/modules/mysql_query.py @@ -148,15 +148,23 @@ DDL_QUERY_KEYWORDS = ('CREATE', 'DROP', 'ALTER', 'RENAME', 'TRUNCATE') # Module execution. # +def get_time(): + try: + time_taken = time.perf_counter() + except AttributeError: + # For Python 2 compatibility, fallback to time.time() + time_taken = time.time() + return time_taken + def execute_and_return_time(cursor, query, args): # Measure query execution time in milliseconds - start_time = time.perf_counter() + start_time = get_time() cursor.execute(query, args) # Calculate the execution time rounding it to 4 decimal places - exec_time_ms = round((time.perf_counter() - start_time) * 1000, 4) + exec_time_ms = round((get_time() - start_time) * 1000, 4) return cursor, exec_time_ms