Skip to content

Commit

Permalink
technical debt( rework order.workflow column) (#3929)(patch)
Browse files Browse the repository at this point in the history
# Description
Solves technical debt on database model
  • Loading branch information
ChrOertlin authored Nov 12, 2024
1 parent 09a5ccc commit 262bac9
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 29 deletions.
51 changes: 51 additions & 0 deletions alembic/versions/2024_11_11_05ffb5e13d7b_remove_order_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""remove_order_workflow
Revision ID: 05ffb5e13d7b
Revises: 3503b53a9bc3
Create Date: 2024-11-11 09:35:33.020098
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "05ffb5e13d7b"
down_revision = "3503b53a9bc3"
branch_labels = None
depends_on = None
workflows = [
"balsamic",
"balsamic-pon",
"balsamic-qc",
"balsamic-umi",
"demultiplex",
"raw-data",
"fluffy",
"microsalt",
"mip-dna",
"mip-rna",
"mutant",
"raredisease",
"rnafusion",
"rsync",
"spring",
"taxprofiler",
"tomte",
"jasen",
]


def upgrade():
op.drop_column(table_name="order", column_name="workflow")


def downgrade():
op.add_column(
"order",
sa.Column(
"workflow",
type_=sa.Enum(*workflows),
),
)
1 change: 0 additions & 1 deletion cg/cli/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ def add_case(
order = Order(
customer_id=customer.id,
ticket_id=int(ticket),
workflow=data_analysis,
)
new_case.orders.append(order)

Expand Down
3 changes: 2 additions & 1 deletion cg/services/orders/order_service/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from cg.constants import Workflow
from cg.server.dto.orders.orders_response import Order, OrdersResponse, OrderSummary
from cg.store.models import Order as DatabaseOrder

Expand All @@ -9,7 +10,7 @@ def create_order_response(order: DatabaseOrder, summary: OrderSummary | None = N
order_date=str(order.order_date.date()),
id=order.id,
is_open=order.is_open,
workflow=order.workflow,
workflow=order.workflow if Workflow.BALSAMIC not in order.workflow else Workflow.BALSAMIC,
summary=summary,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def store_items_in_status(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=Workflow(items[0]["data_analysis"]),
)
for case in items:
status_db_case: Case = self.status_db.get_case_by_internal_id(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def store_items_in_status(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=Workflow(submitted_case["data_analysis"]),
)
with self.status_db.session.no_autoflush:
for sample in items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def store_items_in_status(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=Workflow(case_dict["data_analysis"]),
)
with self.status_db.session.no_autoflush:
for sample in case_dict["samples"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def store_items_in_status(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=Workflow.RAW_DATA,
)
for sample in items:
case_name: str = f'{sample["name"]}-case'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def store_items_in_status(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=data_analysis,
)

with self.status.session.no_autoflush:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def _store_samples_in_statusdb(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=Workflow.RAW_DATA,
)
new_samples = []
with self.status_db.session.no_autoflush:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def store_items_in_status(
customer=customer,
order_date=datetime.now(),
ticket_id=int(ticket_id),
workflow=Workflow(items[0]["data_analysis"]),
)
new_pools: list[Pool] = []
new_samples: list[Sample] = []
Expand Down
5 changes: 5 additions & 0 deletions cg/store/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Application,
ApplicationLimitations,
ApplicationVersion,
Order,
)
from cg.store.models import Base as ModelBase
from cg.store.models import (
Expand Down Expand Up @@ -85,6 +86,10 @@ def _get_join_sample_case_order_query(self) -> Query:
self._get_query(table=Sample).join(Case.links).join(CaseSample.sample).join(Case.orders)
)

def _get_join_order_case_query(self) -> Query:
"""Return a query joining sample, cases_sample, case and order. Selects from sample."""
return self._get_query(table=Order).join(Order.cases)

def _get_join_application_ordertype_query(self) -> Query:
"""Return join application to order type query."""
return self._get_query(table=Application).join(Application.order_type_applications)
Expand Down
11 changes: 8 additions & 3 deletions cg/store/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,15 @@ def get_all_pools_to_deliver(self) -> list[Pool]:

def get_orders(self, orders_request: OrdersRequest) -> tuple[list[Order], int]:
"""Filter, sort and paginate orders based on the provided request."""
orders: Query = apply_order_filters(
orders=self._get_query(Order),
filters=[OrderFilter.BY_WORKFLOW, OrderFilter.BY_SEARCH, OrderFilter.BY_OPEN],
order_case: Query = self._get_join_order_case_query()
order_for_workflow: Query = apply_case_filter(
cases=order_case,
filter_functions=[CaseFilter.WITH_WORKFLOW],
workflow=orders_request.workflow,
)
orders: Query = apply_order_filters(
orders=order_for_workflow,
filters=[OrderFilter.BY_SEARCH, OrderFilter.BY_OPEN],
search=orders_request.search,
is_open=orders_request.is_open,
)
Expand Down
9 changes: 0 additions & 9 deletions cg/store/filters/status_order_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
from cg.store.models import Customer, Order


def filter_orders_by_workflow(orders: Query, workflow: str | None, **kwargs) -> Query:
if workflow == Workflow.BALSAMIC:
return orders.filter(Order.workflow.startswith(workflow))
return orders.filter(Order.workflow == workflow) if workflow else orders


def filter_orders_by_id(orders: Query, id: int | None, **kwargs) -> Query:
return orders.filter(Order.id == id) if id else orders

Expand Down Expand Up @@ -65,7 +59,6 @@ class OrderFilter(Enum):
BY_IDS: Callable = filter_orders_by_ids
BY_SEARCH: Callable = filter_orders_by_search
BY_TICKET_ID: Callable = filter_orders_by_ticket_id
BY_WORKFLOW: Callable = filter_orders_by_workflow
BY_OPEN: Callable = filter_orders_by_is_open
PAGINATE: Callable = apply_pagination
SORT: Callable = apply_sorting
Expand All @@ -77,7 +70,6 @@ def apply_order_filters(
id: int = None,
ids: list[int] = None,
ticket_id: int = None,
workflow: SortOrder = None,
page: int = None,
page_size: int = None,
sort_field: OrderSortField = None,
Expand All @@ -90,7 +82,6 @@ def apply_order_filters(
orders=orders,
id=id,
ids=ids,
workflow=workflow,
page=page,
page_size=page_size,
ticket_id=ticket_id,
Expand Down
5 changes: 4 additions & 1 deletion cg/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,12 @@ class Order(Base):
customer: Mapped[Customer] = orm.relationship(foreign_keys=[customer_id])
order_date: Mapped[datetime] = mapped_column(default=datetime.now)
ticket_id: Mapped[int] = mapped_column(unique=True, index=True)
workflow: Mapped[str] = mapped_column(types.Enum(*(workflow.value for workflow in Workflow)))
is_open: Mapped[bool] = mapped_column(default=True)

@property
def workflow(self) -> Workflow:
return self.cases[0].data_analysis

def to_dict(self):
return to_dict(model_instance=self)

Expand Down
1 change: 0 additions & 1 deletion tests/meta/archive/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ def archive_store(
order = Order(
customer_id=customer_ddn.id,
ticket_id=new_samples[0].original_ticket,
workflow=case.data_analysis,
)
base_store.session.add(order)
base_store.session.add(case)
Expand Down
8 changes: 6 additions & 2 deletions tests/server/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def order(
customer_id=customer.id,
ticket_id=1,
order_date=datetime.now(),
workflow=Workflow.MIP_DNA,
)
order.cases.append(server_case)
order.cases.append(server_case_in_same_order)
Expand All @@ -60,21 +59,26 @@ def order(

@pytest.fixture
def order_another(helpers: StoreHelpers, customer_another: Customer) -> Order:
case: Case = helpers.add_case(name="another_case", store=store, data_analysis=Workflow.MIP_DNA)
order: Order = helpers.add_order(
store=store, customer_id=customer_another.id, ticket_id=2, order_date=datetime.now()
)
order.cases.append(case)
return order


@pytest.fixture
def order_balsamic(helpers: StoreHelpers, customer_another: Customer) -> Order:
case: Case = helpers.add_case(
name="balsamic_case", store=store, data_analysis=Workflow.BALSAMIC
)
order: Order = helpers.add_order(
store=store,
customer_id=customer_another.id,
ticket_id=3,
order_date=datetime.now(),
workflow=Workflow.BALSAMIC,
)
order.cases.append(case)
return order


Expand Down
10 changes: 10 additions & 0 deletions tests/store/crud/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,27 +456,37 @@ def three_pool_names() -> list[str]:

@pytest.fixture
def order(helpers: StoreHelpers, store: Store) -> Order:
case: Case = helpers.add_case(data_analysis=Workflow.MIP_DNA, store=store, name="order_case")
order: Order = helpers.add_order(
store=store, customer_id=1, ticket_id=1, order_date=datetime.now()
)
order.cases.append(case)
return order


@pytest.fixture
def order_another(helpers: StoreHelpers, store: Store) -> Order:
case: Case = helpers.add_case(
data_analysis=Workflow.MIP_DNA, store=store, name="order_another_case"
)
order: Order = helpers.add_order(
store=store, customer_id=2, ticket_id=2, order_date=datetime.now()
)
order.cases.append(case)
return order


@pytest.fixture
def order_balsamic(helpers: StoreHelpers, store: Store) -> Order:
case: Case = helpers.add_case(
data_analysis=Workflow.BALSAMIC, store=store, name="order_balsamic_case"
)
order: Order = helpers.add_order(
store=store,
customer_id=2,
ticket_id=3,
order_date=datetime.now(),
workflow=Workflow.BALSAMIC,
)
order.cases.append(case)
return order
2 changes: 0 additions & 2 deletions tests/store/filters/test_status_order_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_filter_orders_by_ticket_no_matching_ticket(base_store: Store, non_exist
id=1,
customer_id=1,
ticket_id=1,
workflow=Workflow.MIP_DNA,
)
base_store.session.add(order)
base_store.session.commit()
Expand All @@ -33,7 +32,6 @@ def test_filter_orders_by_ticket_id_matching_ticket(base_store: Store, ticket_id
id=1,
customer_id=1,
ticket_id=int(ticket_id),
workflow=Workflow.MIP_DNA,
)
base_store.session.add(order)
base_store.session.commit()
Expand Down
5 changes: 3 additions & 2 deletions tests/store_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,9 @@ def add_order(
workflow: Workflow = Workflow.MIP_DNA,
) -> Order:
order = Order(
customer_id=customer_id, ticket_id=ticket_id, order_date=order_date, workflow=workflow
customer_id=customer_id,
ticket_id=ticket_id,
order_date=order_date,
)
store.session.add(order)
store.session.commit()
Expand Down Expand Up @@ -568,7 +570,6 @@ def ensure_case_from_dict(
order = store.get_order_by_ticket_id(ticket_id=int(case_info["tickets"])) or Order(
ticket_id=int(case_info["tickets"]),
customer_id=customer_obj.id,
workflow=case_info.get("data_analysis", Workflow.MIP_DNA),
)
case = Case(
name=case_info["name"],
Expand Down

0 comments on commit 262bac9

Please sign in to comment.