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

Project history #139

Merged
merged 13 commits into from
Jul 8, 2024
2 changes: 2 additions & 0 deletions pepdbagent/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
LAST_UPDATE_DATE_KEY = "last_update_date"

PEPHUB_SAMPLE_ID_KEY = "ph_id"

MAX_HISTORY_SAMPLES_NUMBER = 2000
53 changes: 52 additions & 1 deletion pepdbagent/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import datetime
import enum
import logging
from typing import List, Optional

from sqlalchemy import (
TIMESTAMP,
BigInteger,
Enum,
FetchedValue,
ForeignKey,
Result,
Expand Down Expand Up @@ -119,6 +121,10 @@ class Projects(Base):

namespace_mapping: Mapped["User"] = relationship("User", back_populates="projects_mapping")

history_mapping: Mapped[List["HistoryProjects"]] = relationship(
back_populates="project_mapping", cascade="all, delete-orphan"
) # TODO: check if cascade is correct
khoroshevskyi marked this conversation as resolved.
Show resolved Hide resolved

__table_args__ = (UniqueConstraint("namespace", "name", "tag"),)


Expand All @@ -131,7 +137,6 @@ class Samples(Base):

id: Mapped[int] = mapped_column(primary_key=True)
sample: Mapped[dict] = mapped_column(JSON, server_default=FetchedValue())
row_number: Mapped[int] # TODO: should be removed
project_id = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
project_mapping: Mapped["Projects"] = relationship(back_populates="samples_mapping")
sample_name: Mapped[Optional[str]] = mapped_column()
Expand Down Expand Up @@ -245,6 +250,52 @@ class ViewSampleAssociation(Base):
view: Mapped["Views"] = relationship(back_populates="samples")


class HistoryProjects(Base):

__tablename__ = "project_history"

id: Mapped[int] = mapped_column(primary_key=True)
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"))
user: Mapped[str] = mapped_column(ForeignKey("users.namespace", ondelete="SET NULL"))
update_time: Mapped[datetime.datetime] = mapped_column(
TIMESTAMP(timezone=True), default=deliver_update_date
)
project_yaml: Mapped[dict] = mapped_column(JSON, server_default=FetchedValue())

project_mapping: Mapped["Projects"] = relationship(
"Projects", back_populates="history_mapping"
)
sample_changes_mapping: Mapped[List["HistorySamples"]] = relationship(
back_populates="history_project_mapping", cascade="all, delete-orphan"
)


class UpdateTypes(enum.Enum):
"""
Enum for the type of update
"""

UPDATE = "update"
INSERT = "insert"
DELETE = "delete"


class HistorySamples(Base):

__tablename__ = "sample_history"

id: Mapped[int] = mapped_column(primary_key=True)
history_id: Mapped[int] = mapped_column(ForeignKey("project_history.id", ondelete="CASCADE"))
guid: Mapped[str] = mapped_column(nullable=False)
parent_guid: Mapped[Optional[str]] = mapped_column(nullable=True)
sample_json: Mapped[dict] = mapped_column(JSON, server_default=FetchedValue())
change_type: Mapped[UpdateTypes] = mapped_column(Enum(UpdateTypes), nullable=False)

history_project_mapping: Mapped["HistoryProjects"] = relationship(
"HistoryProjects", back_populates="sample_changes_mapping"
)


class BaseEngine:
"""
A class with base methods, that are used in several classes. e.g. fetch_one or fetch_all
Expand Down
10 changes: 10 additions & 0 deletions pepdbagent/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ def __init__(self, msg=""):
class NamespaceNotFoundError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""Project does not exist. {msg}""")


class HistoryNotFoundError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""History does not exist. {msg}""")


class UserNotFoundError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""User does not exist. {msg}""")
22 changes: 22 additions & 0 deletions pepdbagent/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# file with pydantic models
import datetime
from typing import Dict, List, Optional, Union

from peppy.const import CONFIG_KEY, SAMPLE_RAW_DICT_KEY, SUBSAMPLE_RAW_LIST_KEY
Expand Down Expand Up @@ -224,3 +225,24 @@ class NamespaceStats(BaseModel):
namespace: Union[str, None] = None
projects_updated: Dict[str, int] = None
projects_created: Dict[str, int] = None


class HistoryChangeModel(BaseModel):
"""
Model for history change
"""

change_id: int
change_date: datetime.datetime
user: str


class HistoryAnnotationModel(BaseModel):
"""
History annotation model
"""

namespace: str
name: str
tag: str = DEFAULT_TAG
history: List[HistoryChangeModel]
Loading
Loading