From aa11d4e7542b9c8f9bc9a846edaffd8a6e57c9b2 Mon Sep 17 00:00:00 2001 From: gitofanindya Date: Mon, 7 Oct 2024 12:36:23 +0530 Subject: [PATCH] NXDRIVE-2967: Upgrade the deprecated datetime adapter --- nxdrive/dao/base.py | 19 ++++++++++++++++++- tests/conftest.py | 4 ---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/nxdrive/dao/base.py b/nxdrive/dao/base.py index 24bce57600..fb9c00278c 100644 --- a/nxdrive/dao/base.py +++ b/nxdrive/dao/base.py @@ -25,7 +25,24 @@ def execute(self, sql: str, parameters: Iterable[Any] = ()) -> Cursor: while True: count += 1 try: - return super().execute(sql, parameters) + if parameters: + import datetime + import sqlite3 + new_param = [] + for param in parameters: + if type(param) == datetime.datetime: + def adapt_datetime_iso(val): + return datetime.datetime.fromtimestamp(str(val), datetime.UTC) + mtime = sqlite3.register_adapter(param, adapt_datetime_iso) + new_param.append(mtime) + else: + new_param.append(param) + + new_param = tuple(new_param) + res = super().execute(sql, new_param) + else: + res = super().execute(sql, parameters) + return res except OperationalError as exc: log.info( f"Retry locked database #{count}, {sql=}, {parameters=}", diff --git a/tests/conftest.py b/tests/conftest.py index c08b9b0496..8a2de4fb85 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -95,10 +95,6 @@ def no_warnings(recwarn): continue elif "Cryptography will be significantly faster" in message: continue - elif "The default datetime adapter is deprecated as of Python 3.12" in message: - continue - elif "datetime.datetime" in message: - continue warn = f"{warning.filename}:{warning.lineno} {message}" print(warn, file=sys.stderr)