Skip to content

Commit

Permalink
DEV-1673: Add accessed datetime column mixin and include in cohort ta…
Browse files Browse the repository at this point in the history
…ble (#67)
  • Loading branch information
mpsolano authored Nov 27, 2023
1 parent 16b6304 commit 5672330
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
23 changes: 23 additions & 0 deletions gdc_ng_models/models/accessed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Mixin for adding an accessed timestamp column to track the last time a record was accessed."""
import datetime

from sqlalchemy import schema, sql
from sqlalchemy.sql import sqltypes


class AccessedColumnMixin:
"""A mixin to add accessed timestamp column to a data model.
Attributes:
accessed_datetime: Metadata describing when a row was last accessed.
"""

def __init__(self):
pass

accessed_datetime = schema.Column(
sqltypes.DateTime(timezone=True),
nullable=False,
server_default=sql.text("now()"),
onupdate=datetime.datetime.utcnow,
)
16 changes: 12 additions & 4 deletions gdc_ng_models/models/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sqlalchemy
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext import declarative
from gdc_ng_models.models import audit
from gdc_ng_models.models import accessed, audit

Base = declarative.declarative_base()

Expand Down Expand Up @@ -73,15 +73,16 @@ def to_json(self):
}


class Cohort(Base, audit.AuditColumnsMixin):
class Cohort(Base, audit.AuditColumnsMixin, accessed.AccessedColumnMixin):
"""A base definition for a cohort entity.
Attributes:
id: A unique identifier for the cohort.
context_id: The ID of the associated context.
name: A user defined name for the cohort.
created_datetime: The date and time when the record is created.
updated_datetime: The date and time when the record is updated.
updated_datetime: The date and time when the record is last updated.
accessed_datetime: The date and time when the record is last accessed.
"""

__tablename__ = "cohort"
Expand Down Expand Up @@ -121,7 +122,8 @@ def __repr__(self):
"name='{name}', "
"context_id={context_id}, "
"created_datetime={created_datetime}, "
"updated_datetime={updated_datetime})>".format(
"updated_datetime={updated_datetime}), "
"accessed_datetime={accessed_datetime})>".format(
id=self.id,
name=self.name,
context_id=self.context_id,
Expand All @@ -131,6 +133,9 @@ def __repr__(self):
updated_datetime=self.updated_datetime.isoformat()
if self.updated_datetime
else None,
accessed_datetime=self.accessed_datetime.isoformat()
if self.accessed_datetime
else None,
)
)

Expand All @@ -145,6 +150,9 @@ def to_json(self):
"updated_datetime": self.updated_datetime.isoformat()
if self.updated_datetime
else None,
"accessed_datetime": self.accessed_datetime.isoformat()
if self.accessed_datetime
else None,
}


Expand Down
1 change: 1 addition & 0 deletions tests/integration/models/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def test_cohort__to_json(create_cohort_db, db_session, fixture_context):
"context_id": str(test_cohort.context_id),
"created_datetime": test_cohort.created_datetime.isoformat(),
"updated_datetime": test_cohort.updated_datetime.isoformat(),
"accessed_datetime": test_cohort.accessed_datetime.isoformat(),
}
)
)
Expand Down

0 comments on commit 5672330

Please sign in to comment.