Skip to content

Commit

Permalink
Merge pull request #135 from pepkit/133_update_improvements
Browse files Browse the repository at this point in the history
133 update improvements
  • Loading branch information
khoroshevskyi authored Jun 28, 2024
2 parents 020b6e3 + a19bbbf commit 7129552
Show file tree
Hide file tree
Showing 25 changed files with 2,412 additions and 1,833 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Run pytests](https://github.com/pepkit/pepdbagent/workflows/Run%20pytests/badge.svg)
[![pypi-badge](https://img.shields.io/pypi/v/pepdbagent?color=%2334D058)](https://pypi.org/project/pepdbagent)
[![pypi-version](https://img.shields.io/pypi/pyversions/pepdbagent.svg?color=%2334D058)](https://pypi.org/project/pepdbagent)
[![Downloads](https://static.pepy.tech/badge/pepdbagent)](https://pepy.tech/project/pepdbagent)
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/pepkit/pepdbagent.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/pepkit/pepdbagent)
[![Github badge](https://img.shields.io/badge/source-github-354a75?logo=github)](https://github.com/pepkit/pepdbagent)


Expand Down Expand Up @@ -135,4 +135,4 @@ agent.namespace.get(query='search_pattern', admin=['databio', 'geo', 'ncbi'])
For more information, developers should use `pepdbagent pytest` as documentation due to its natural language syntax and the
ability to write tests that serve as executable examples.
This approach not only provides detailed explanations but also ensures that code examples are kept
up-to-date with the latest changes in the codebase.
up-to-date with the latest changes in the codebase.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format.

## [0.9.0] -- 2024-06-25
- Introduced new sample ordering with linked list [#133](https://github.com/pepkit/pepdbagent/issues/133)
- Efficiency improvements of project update function
- Test restructuring


## [0.8.0] -- 2024-02-26
- Fixed forking schema
- Improved forking efficiency [#129](https://github.com/pepkit/pepdbagent/issues/129)
Expand Down
2 changes: 1 addition & 1 deletion pepdbagent/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.8.0"
__version__ = "0.9.0"
2 changes: 2 additions & 0 deletions pepdbagent/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@

SUBMISSION_DATE_KEY = "submission_date"
LAST_UPDATE_DATE_KEY = "last_update_date"

PEPHUB_SAMPLE_ID_KEY = "ph_id"
48 changes: 33 additions & 15 deletions pepdbagent/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
import datetime
import logging
from typing import Optional, List
from typing import List, Optional

from sqlalchemy import (
TIMESTAMP,
BigInteger,
FetchedValue,
ForeignKey,
Result,
Select,
String,
UniqueConstraint,
event,
select,
TIMESTAMP,
ForeignKey,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.engine import URL, create_engine
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
Session,
mapped_column,
relationship,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, relationship

from pepdbagent.const import POSTGRES_DIALECT, PKG_NAME
from pepdbagent.const import PKG_NAME, POSTGRES_DIALECT
from pepdbagent.exceptions import SchemaError

_LOGGER = logging.getLogger(PKG_NAME)
Expand Down Expand Up @@ -93,7 +87,7 @@ class Projects(Base):
pep_schema: Mapped[Optional[str]]
pop: Mapped[Optional[bool]] = mapped_column(default=False)
samples_mapping: Mapped[List["Samples"]] = relationship(
back_populates="sample_mapping", cascade="all, delete-orphan"
back_populates="project_mapping", cascade="all, delete-orphan"
)
subsamples_mapping: Mapped[List["Subsamples"]] = relationship(
back_populates="subsample_mapping", cascade="all, delete-orphan"
Expand Down Expand Up @@ -133,10 +127,22 @@ class Samples(Base):

id: Mapped[int] = mapped_column(primary_key=True)
sample: Mapped[dict] = mapped_column(JSON, server_default=FetchedValue())
row_number: Mapped[int]
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()
sample_mapping: Mapped["Projects"] = relationship(back_populates="samples_mapping")
guid: Mapped[Optional[str]] = mapped_column(nullable=False, unique=True)

parent_guid: Mapped[Optional[str]] = mapped_column(
ForeignKey("samples.guid", ondelete="CASCADE"),
nullable=True,
doc="Parent sample id. Used to create a hierarchy of samples.",
)

parent_mapping: Mapped["Samples"] = relationship(
"Samples", remote_side=guid, back_populates="child_mapping"
)
child_mapping: Mapped["Samples"] = relationship("Samples", back_populates="parent_mapping")

views: Mapped[Optional[List["ViewSampleAssociation"]]] = relationship(
back_populates="sample", cascade="all, delete-orphan"
Expand Down Expand Up @@ -318,3 +324,15 @@ def check_db_connection(self):
self.session_execute(select(Projects).limit(1))
except ProgrammingError:
raise SchemaError()

def delete_schema(self, engine=None) -> None:
"""
Delete sql schema in the database.
:param engine: sqlalchemy engine [Default: None]
:return: None
"""
if not engine:
engine = self._engine
Base.metadata.drop_all(engine)
return None
10 changes: 10 additions & 0 deletions pepdbagent/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def __init__(self, msg=""):
super().__init__(f"""Sample does not exist. {msg}""")


class SampleTableUpdateError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""Sample table update error. {msg}""")


class ProjectDuplicatedSampleGUIDsError(SampleTableUpdateError):
def __init__(self, msg=""):
super().__init__(f"""Project has duplicated sample GUIDs. {msg}""")


class SampleAlreadyExistsError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""Sample already exists. {msg}""")
Expand Down
7 changes: 4 additions & 3 deletions pepdbagent/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# file with pydantic models
from typing import List, Optional, Union, Dict
from pydantic import BaseModel, Field, ConfigDict, field_validator
from peppy.const import CONFIG_KEY, SUBSAMPLE_RAW_LIST_KEY, SAMPLE_RAW_DICT_KEY
from typing import Dict, List, Optional, Union

from peppy.const import CONFIG_KEY, SAMPLE_RAW_DICT_KEY, SUBSAMPLE_RAW_LIST_KEY
from pydantic import BaseModel, ConfigDict, Field, field_validator

from pepdbagent.const import DEFAULT_TAG

Expand Down
4 changes: 2 additions & 2 deletions pepdbagent/modules/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from typing import List, Literal, Optional, Union

from sqlalchemy import and_, func, or_, select
from sqlalchemy.sql.selectable import Select
from sqlalchemy.orm import Session
from sqlalchemy.sql.selectable import Select

from pepdbagent.const import (
DEFAULT_LIMIT,
DEFAULT_OFFSET,
DEFAULT_TAG,
LAST_UPDATE_DATE_KEY,
PKG_NAME,
SUBMISSION_DATE_KEY,
LAST_UPDATE_DATE_KEY,
)
from pepdbagent.db_utils import BaseEngine, Projects
from pepdbagent.exceptions import FilterError, ProjectNotFoundError, RegistryPathError
Expand Down
12 changes: 6 additions & 6 deletions pepdbagent/modules/namespace.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import logging
from typing import List, Union, Tuple
from collections import Counter
from datetime import datetime, timedelta
from typing import List, Tuple, Union

from sqlalchemy import distinct, func, or_, select, text
from sqlalchemy.sql.selectable import Select
from sqlalchemy.orm import Session
from sqlalchemy.sql.selectable import Select

from pepdbagent.const import DEFAULT_LIMIT, DEFAULT_OFFSET, PKG_NAME, DEFAULT_LIMIT_INFO
from pepdbagent.const import DEFAULT_LIMIT, DEFAULT_LIMIT_INFO, DEFAULT_OFFSET, PKG_NAME
from pepdbagent.db_utils import BaseEngine, Projects
from pepdbagent.exceptions import NamespaceNotFoundError
from pepdbagent.db_utils import Projects, BaseEngine
from pepdbagent.models import (
ListOfNamespaceInfo,
Namespace,
NamespaceList,
NamespaceInfo,
ListOfNamespaceInfo,
NamespaceList,
NamespaceStats,
)
from pepdbagent.utils import tuple_converter
Expand Down
Loading

0 comments on commit 7129552

Please sign in to comment.