Skip to content

Commit

Permalink
Attempt to add "types" to job details
Browse files Browse the repository at this point in the history
I tried to do an implicit join, as explained in the Sqlalchemy quickstart, and then added a join as it did nothing visible, but the Job objects still miss the types[] list. This is my first approach with SQLAlchemy, so... :-)
  • Loading branch information
bzizou committed Dec 8, 2023
1 parent 59e209e commit 43c677b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion oar/lib/basequery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
GanttJobsPredictionsVisu,
GanttJobsResourcesVisu,
Job,
JobType,
MoldableJobDescription,
Resource,
)
Expand Down Expand Up @@ -131,7 +132,8 @@ def apply_commons_filters(q, *criterion):
return query.outerjoin(
MoldableJobDescription,
Job.assigned_moldable_job == MoldableJobDescription.id,
).join(unionquery, Job.id == unionquery.c.job_id)
).join(unionquery, Job.id == unionquery.c.job_id,
).join(JobType, Job.id == JobType.job_id)


class BaseQueryCollection(object):
Expand Down
16 changes: 13 additions & 3 deletions oar/lib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
Text,
inspect,
text,
ForeignKey,
)
from sqlalchemy.ext.declarative import DeferredReflection
from sqlalchemy.orm import DeclarativeMeta, declarative_base
from sqlalchemy.orm import DeclarativeMeta, declarative_base, Mapped, relationship, mapped_column
from sqlalchemy.orm.state import InstanceState
from typing import List

from oar.lib.database import Database

from .utils import reraise
from .utils import reraise, to_json

# from .globals import db

Expand Down Expand Up @@ -75,6 +77,12 @@ def to_dict(self, ignore_keys=()):

asdict = to_dict

def to_json(self, **kwargs):
"""Dump `self` to json string."""
kwargs.setdefault("ignore_keys", ())
obj = self.to_dict(kwargs.pop("ignore_keys"))
return to_json(obj, **kwargs)

def __iter__(self):
"""Return an iterable that supports .next()"""
for key, value in (self.asdict()).items():
Expand Down Expand Up @@ -335,9 +343,10 @@ class JobType(Model):
__tablename__ = "job_types"

id = Column("job_type_id", Integer, primary_key=True)
job_id = Column(Integer, index=True, server_default="0")
job_id: Mapped[int] = mapped_column(ForeignKey("jobs.job_id"))
type = Column(String(255), index=True, server_default="")
types_index = Column(String(7), index=True, server_default="CURRENT")
job: Mapped["Job"] = relationship(back_populates="types")


class Resource(DeferredReflectionModel):
Expand Down Expand Up @@ -402,6 +411,7 @@ class Job(DeferredReflectionModel):
stderr_file = Column(Text, nullable=True)
resubmit_job_id = Column(Integer, server_default="0")
suspended = Column(String(3), index=True, server_default="NO")
types: Mapped[List["JobType"]] = relationship( back_populates="job", cascade="all" )


class MoldableJobDescription(Model):
Expand Down

0 comments on commit 43c677b

Please sign in to comment.