Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Update API calls to datetime functionality #117

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions jupyter_cache/cache/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextlib import contextmanager
from datetime import datetime
import datetime
import os
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -51,6 +51,10 @@ def get_version(path: Union[str, Path]) -> Optional[str]:
return version_file.read_text().strip()


def datetime_utcnow():
return lambda: datetime.datetime.now(datetime.timezone.utc)


@contextmanager
def session_context(engine: Engine):
"""Open a connection to the database."""
Expand Down Expand Up @@ -128,7 +132,7 @@ class NbProjectRecord(OrmBase):
"""A list of file assets required for the notebook to run."""
exec_data = Column(JSON(), nullable=True)
"""Data on how to execute the notebook."""
created = Column(DateTime, nullable=False, default=datetime.utcnow)
created = Column(DateTime, nullable=False, default=datetime_utcnow())
traceback = Column(Text(), nullable=True, default="")
"""A traceback is added if a notebook fails to execute fully."""

Expand Down Expand Up @@ -288,9 +292,9 @@ class NbCacheRecord(OrmBase):
description = Column(String(255), nullable=False, default="")
data = Column(JSON())
"""Extra data, such as the execution time."""
created = Column(DateTime, nullable=False, default=datetime.utcnow)
created = Column(DateTime, nullable=False, default=datetime_utcnow())
accessed = Column(
DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow
DateTime, nullable=False, default=datetime_utcnow(), onupdate=datetime_utcnow()
)

def __repr__(self):
Expand Down Expand Up @@ -368,7 +372,7 @@ def touch(pk, db: Engine):
record = session.query(NbCacheRecord).filter_by(pk=pk).one_or_none()
if record is None:
raise KeyError(f"Cache record not found for NB with PK: {pk}")
record.accessed = datetime.utcnow()
record.accessed = datetime_utcnow()()
session.commit()

def touch_hashkey(hashkey, db: Engine):
Expand All @@ -379,7 +383,7 @@ def touch_hashkey(hashkey, db: Engine):
)
if record is None:
raise KeyError(f"Cache record not found for NB with hashkey: {hashkey}")
record.accessed = datetime.utcnow()
record.accessed = datetime_utcnow()()
session.commit()

@staticmethod
Expand Down
Loading