mirror of
https://github.com/ansible-collections/community.mysql.git
synced 2025-07-26 14:41:24 -07:00
Fix python2 compatibility of execution_time_ms (#720)
Co-authored-by: Simon Pahl <simon.pahl@etops.com>
This commit is contained in:
parent
83ad0e81e2
commit
539e940ab3
2 changed files with 13 additions and 2 deletions
3
changelogs/fragments/fix_python2_compatibility.yml
Normal file
3
changelogs/fragments/fix_python2_compatibility.yml
Normal 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)."
|
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue