Skip to content

Commit

Permalink
feat(db): add indexes to the study table to improve search engine
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-laporte-pro committed Jan 19, 2024
1 parent 0ba78ec commit b15733e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
24 changes: 12 additions & 12 deletions antarest/study/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class StudyAdditionalData(Base): # type:ignore
)
author = Column(String(255), default="Unknown")
horizon = Column(String)
patch = Column(String(), nullable=True)
patch = Column(String(), index=True, nullable=True)

def __eq__(self, other: t.Any) -> bool:
if not super().__eq__(other):
Expand All @@ -93,19 +93,19 @@ class Study(Base): # type: ignore
default=lambda: str(uuid.uuid4()),
unique=True,
)
name = Column(String(255))
type = Column(String(50))
version = Column(String(255))
name = Column(String(255), index=True)
type = Column(String(50), index=True)
version = Column(String(255), index=True)
author = Column(String(255))
created_at = Column(DateTime)
updated_at = Column(DateTime)
created_at = Column(DateTime, index=True)
updated_at = Column(DateTime, index=True)
last_access = Column(DateTime)
path = Column(String())
folder = Column(String, nullable=True)
parent_id = Column(String(36), ForeignKey("study.id", name="fk_study_study_id"))
folder = Column(String, nullable=True, index=True)
parent_id = Column(String(36), ForeignKey("study.id", name="fk_study_study_id"), index=True)
public_mode = Column(Enum(PublicMode), default=PublicMode.NONE)
owner_id = Column(Integer, ForeignKey(Identity.id), nullable=True)
archived = Column(Boolean(), default=False)
owner_id = Column(Integer, ForeignKey(Identity.id), nullable=True, index=True)
archived = Column(Boolean(), default=False, index=True)
owner = relationship(Identity, uselist=False)
groups = relationship(Group, secondary=lambda: groups_metadata, cascade="")
additional_data = relationship(
Expand Down Expand Up @@ -167,8 +167,8 @@ class RawStudy(Study):
primary_key=True,
)
content_status = Column(Enum(StudyContentStatus))
workspace = Column(String(255), default=DEFAULT_WORKSPACE_NAME)
missing = Column(DateTime, nullable=True)
workspace = Column(String(255), default=DEFAULT_WORKSPACE_NAME, nullable=False, index=True)
missing = Column(DateTime, nullable=True, index=True)

__mapper_args__ = {
"polymorphic_identity": "rawstudy",
Expand Down
66 changes: 66 additions & 0 deletions tests/study/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Test the database model.
"""
import uuid

from sqlalchemy import inspect # type: ignore
from sqlalchemy.engine import Engine # type: ignore
from sqlalchemy.orm import Session # type: ignore

from antarest.study.model import Study


# noinspection SpellCheckingInspection
class TestStudy:
"""
Test the study model.
"""

def test_study(self, db_session: Session) -> None:
"""
Basic test of the `study` table.
"""
study_id = uuid.uuid4()

with db_session:
db_session.add(Study(id=str(study_id), name="Study 1"))
db_session.commit()

with db_session:
study = db_session.query(Study).first()
assert study.id == str(study_id)
assert study.name == "Study 1"

def test_index_on_study(self, db_engine: Engine) -> None:
inspector = inspect(db_engine)
indexes = inspector.get_indexes("study")
index_names = {index["name"] for index in indexes}
assert index_names == {
"ix_study_archived",
"ix_study_created_at",
"ix_study_folder",
"ix_study_name",
"ix_study_owner_id",
"ix_study_parent_id",
"ix_study_type",
"ix_study_updated_at",
"ix_study_version",
}

def test_index_on_rawstudy(self, db_engine: Engine) -> None:
inspector = inspect(db_engine)
indexes = inspector.get_indexes("rawstudy")
index_names = {index["name"] for index in indexes}
assert index_names == {"ix_rawstudy_workspace", "ix_rawstudy_missing"}

def test_index_on_variantstudy(self, db_engine: Engine) -> None:
inspector = inspect(db_engine)
indexes = inspector.get_indexes("variantstudy")
index_names = {index["name"] for index in indexes}
assert not index_names

def test_index_on_study_additional_data(self, db_engine: Engine) -> None:
inspector = inspect(db_engine)
indexes = inspector.get_indexes("study_additional_data")
index_names = {index["name"] for index in indexes}
assert index_names == {"ix_study_additional_data_patch"}

0 comments on commit b15733e

Please sign in to comment.