11from contextlib import contextmanager
2- from datetime import datetime
2+ import datetime
33import os
44from pathlib import Path
55from typing import Any , Dict , List , Optional , Union
@@ -51,6 +51,10 @@ def get_version(path: Union[str, Path]) -> Optional[str]:
5151 return version_file .read_text ().strip ()
5252
5353
54+ def datetime_utcnow ():
55+ return lambda : datetime .datetime .now (datetime .timezone .utc )
56+
57+
5458@contextmanager
5559def session_context (engine : Engine ):
5660 """Open a connection to the database."""
@@ -128,7 +132,7 @@ class NbProjectRecord(OrmBase):
128132 """A list of file assets required for the notebook to run."""
129133 exec_data = Column (JSON (), nullable = True )
130134 """Data on how to execute the notebook."""
131- created = Column (DateTime , nullable = False , default = datetime . utcnow )
135+ created = Column (DateTime , nullable = False , default = datetime_utcnow () )
132136 traceback = Column (Text (), nullable = True , default = "" )
133137 """A traceback is added if a notebook fails to execute fully."""
134138
@@ -288,9 +292,9 @@ class NbCacheRecord(OrmBase):
288292 description = Column (String (255 ), nullable = False , default = "" )
289293 data = Column (JSON ())
290294 """Extra data, such as the execution time."""
291- created = Column (DateTime , nullable = False , default = datetime . utcnow )
295+ created = Column (DateTime , nullable = False , default = datetime_utcnow () )
292296 accessed = Column (
293- DateTime , nullable = False , default = datetime . utcnow , onupdate = datetime . utcnow
297+ DateTime , nullable = False , default = datetime_utcnow () , onupdate = datetime_utcnow ()
294298 )
295299
296300 def __repr__ (self ):
@@ -368,7 +372,7 @@ def touch(pk, db: Engine):
368372 record = session .query (NbCacheRecord ).filter_by (pk = pk ).one_or_none ()
369373 if record is None :
370374 raise KeyError (f"Cache record not found for NB with PK: { pk } " )
371- record .accessed = datetime . utcnow ()
375+ record .accessed = datetime_utcnow () ()
372376 session .commit ()
373377
374378 def touch_hashkey (hashkey , db : Engine ):
@@ -379,7 +383,7 @@ def touch_hashkey(hashkey, db: Engine):
379383 )
380384 if record is None :
381385 raise KeyError (f"Cache record not found for NB with hashkey: { hashkey } " )
382- record .accessed = datetime . utcnow ()
386+ record .accessed = datetime_utcnow () ()
383387 session .commit ()
384388
385389 @staticmethod
0 commit comments