Skip to content

Commit

Permalink
chore: use PurePosixPath type in sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski committed Aug 27, 2024
1 parent 3238e06 commit 8872560
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions components/renku_data_services/session/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from renku_data_services.crc.orm import ResourceClassORM
from renku_data_services.project.orm import ProjectORM
from renku_data_services.session import models
from renku_data_services.utils.sqlalchemy import ULIDType
from renku_data_services.utils.sqlalchemy import PurePosixPathType, ULIDType

metadata_obj = MetaData(schema="sessions") # Has to match alembic ini section name
JSONVariant = JSON().with_variant(JSONB(), "postgresql")
Expand Down Expand Up @@ -51,8 +51,8 @@ class EnvironmentORM(BaseORM):
"""Default URL path to open in a session."""

port: Mapped[int] = mapped_column("port")
working_directory: Mapped[str] = mapped_column("working_directory", String())
mount_directory: Mapped[str] = mapped_column("mount_directory", String())
working_directory: Mapped[PurePosixPath] = mapped_column("working_directory", PurePosixPathType)
mount_directory: Mapped[PurePosixPath] = mapped_column("mount_directory", PurePosixPathType)
uid: Mapped[int] = mapped_column("uid")
gid: Mapped[int] = mapped_column("gid")
environment_kind: Mapped[models.EnvironmentKind] = mapped_column("environment_kind")
Expand All @@ -72,8 +72,8 @@ def dump(self) -> models.Environment:
gid=self.gid,
uid=self.uid,
environment_kind=self.environment_kind,
mount_directory=PurePosixPath(self.mount_directory),
working_directory=PurePosixPath(self.working_directory),
mount_directory=self.mount_directory,
working_directory=self.working_directory,
port=self.port,
args=self.args,
command=self.command,
Expand Down
20 changes: 20 additions & 0 deletions components/renku_data_services/utils/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utilities for SQLAlchemy."""

from pathlib import PurePosixPath
from typing import cast

from sqlalchemy import Dialect, types
Expand All @@ -23,3 +24,22 @@ def process_result_value(self, value: str | None, dialect: Dialect) -> ULID | No
if value is None:
return None
return cast(ULID, ULID.from_str(value)) # cast because mypy doesn't understand ULID type annotations


class PurePosixPathType(types.TypeDecorator):
"""Wrapper type for Path <--> str conversion."""

impl = types.String
cache_ok = True

def process_bind_param(self, value: PurePosixPath | None, dialect: Dialect) -> str | None:
"""Transform value for storing in the database."""
if value is None:
return None
return value.as_posix()

def process_result_value(self, value: str | None, dialect: Dialect) -> PurePosixPath | None:
"""Transform string from database into PosixPath."""
if value is None:
return None
return PurePosixPath(value)

0 comments on commit 8872560

Please sign in to comment.