Fix python2 compatibility of execution_time_ms (#720)

Co-authored-by: Simon Pahl <simon.pahl@etops.com>
This commit is contained in:
Simon Pahl 2025-07-24 13:33:02 +02:00 committed by GitHub
commit 539e940ab3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View file

@ -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)."

View file

@ -148,15 +148,23 @@ DDL_QUERY_KEYWORDS = ('CREATE', 'DROP', 'ALTER', 'RENAME', 'TRUNCATE')
# Module execution. # 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): def execute_and_return_time(cursor, query, args):
# Measure query execution time in milliseconds # Measure query execution time in milliseconds
start_time = time.perf_counter() start_time = get_time()
cursor.execute(query, args) cursor.execute(query, args)
# Calculate the execution time rounding it to 4 decimal places # 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 return cursor, exec_time_ms