Skip to content

Commit

Permalink
Rename family in db (#2670)(patch)
Browse files Browse the repository at this point in the history
  • Loading branch information
seallard authored Nov 14, 2023
1 parent d65d230 commit eb9153c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 25 deletions.
67 changes: 67 additions & 0 deletions alembic/versions/2023_11_08_b105b426af99_rename_family_in_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Rename family in db
Revision ID: b105b426af99
Revises: 70e641d723b3
Create Date: 2023-11-08 15:13:25.197537
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "b105b426af99"
down_revision = "70e641d723b3"
branch_labels = None
depends_on = None


def upgrade():
# Drop foreign key constraints that reference the unique constraint to be dropped
op.drop_constraint("analysis_ibfk_1", "analysis", type_="foreignkey")
op.drop_constraint("family_sample_ibfk_1", "family_sample", type_="foreignkey")

# Rename tables
op.rename_table("family", "case")
op.rename_table("family_sample", "case_sample")

# Rename foreign keys
op.alter_column("analysis", "family_id", new_column_name="case_id", existing_type=sa.Integer())
op.alter_column(
"case_sample", "family_id", new_column_name="case_id", existing_type=sa.Integer()
)

# Drop the unique constraint
op.drop_constraint("_family_sample_uc", "case_sample", type_="unique")

# Create the new unique constraint
op.create_unique_constraint("_case_sample_uc", "case_sample", ["case_id", "sample_id"])

# Recreate foreign key constraints referencing the new unique constraint
op.create_foreign_key("analysis_ibfk_1", "analysis", "case", ["case_id"], ["id"])
op.create_foreign_key("case_sample_ibfk_1", "case_sample", "case", ["case_id"], ["id"])


def downgrade():
# Drop foreign key constraints before renaming them back
op.drop_constraint("analysis_ibfk_1", "analysis", type_="foreignkey")
op.drop_constraint("case_sample_ibfk_1", "case_sample", type_="foreignkey")

# Drop the unique constraint
op.drop_constraint("_case_sample_uc", "case_sample", type_="unique")

# Rename foreign keys back to original before renaming tables
op.alter_column("analysis", "case_id", new_column_name="family_id", existing_type=sa.Integer())
op.alter_column(
"case_sample", "case_id", new_column_name="family_id", existing_type=sa.Integer()
)

# Rename tables back to original
op.rename_table("case", "family")
op.rename_table("case_sample", "family_sample")

# Recreate the original unique constraint
op.create_unique_constraint("_family_sample_uc", "family_sample", ["family_id", "sample_id"])

# Recreate foreign key constraints referencing the original unique constraint
op.create_foreign_key("analysis_ibfk_1", "analysis", "family", ["family_id"], ["id"])
op.create_foreign_key("family_sample_ibfk_1", "family_sample", "family", ["family_id"], ["id"])
2 changes: 1 addition & 1 deletion cg/cli/workflow/fastq/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def store_fastq_analysis(context: click.Context, case_id: str, dry_run: bool = F
completed_at=dt.datetime.now(),
primary=True,
started_at=dt.datetime.now(),
family_id=case_obj.id,
case_id=case_obj.id,
)
if dry_run:
return
Expand Down
22 changes: 11 additions & 11 deletions cg/server/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def view_priority(unused1, unused2, model, unused3):
return Markup("%s" % model.priority.name) if model else ""


def view_family_sample_link(unused1, unused2, model, unused3):
"""column formatter to open the family-sample view"""
def view_case_sample_link(unused1, unused2, model, unused3):
"""column formatter to open the case-sample view"""

del unused1, unused2, unused3

Expand Down Expand Up @@ -258,7 +258,7 @@ class CaseView(BaseView):
"tickets",
]
column_formatters = {
"internal_id": view_family_sample_link,
"internal_id": view_case_sample_link,
"priority": view_priority,
}
column_searchable_list = [
Expand All @@ -279,7 +279,7 @@ class CaseView(BaseView):
}

@staticmethod
def view_family_link(unused1, unused2, model, unused3):
def view_case_link(unused1, unused2, model, unused3):
"""column formatter to open this view"""
del unused1, unused2, unused3
markup = ""
Expand Down Expand Up @@ -310,9 +310,9 @@ def action_set_empty(self, ids: list[str]):
def set_action_for_cases(self, action: Union[CaseActions, None], case_entry_ids: list[str]):
try:
for entry_id in case_entry_ids:
family = db.get_case_by_entry_id(entry_id=entry_id)
if family:
family.action = action
case = db.get_case_by_entry_id(entry_id=entry_id)
if case:
case.action = action

db.session.commit()

Expand All @@ -328,7 +328,7 @@ def set_action_for_cases(self, action: Union[CaseActions, None], case_entry_ids:
if not self.handle_view_exception(ex):
raise

flash(gettext(f"Failed to set family action. {str(ex)}"))
flash(gettext(f"Failed to set case action. {str(ex)}"))


class FlowcellView(BaseView):
Expand Down Expand Up @@ -392,7 +392,7 @@ class AnalysisView(BaseView):
column_default_sort = ("created_at", True)
column_editable_list = ["is_primary"]
column_filters = ["pipeline", "pipeline_version", "is_primary"]
column_formatters = {"case": CaseView.view_family_link}
column_formatters = {"case": CaseView.view_case_link}
column_searchable_list = [
"case.internal_id",
"case.name",
Expand Down Expand Up @@ -448,7 +448,7 @@ class SampleView(BaseView):
column_filters = ["customer.internal_id", "priority", "sex", "application_version.application"]
column_formatters = {
"is_external": is_external_application,
"internal_id": view_family_sample_link,
"internal_id": view_case_sample_link,
"invoice": InvoiceView.view_invoice_link,
"priority": view_priority,
}
Expand Down Expand Up @@ -570,7 +570,7 @@ class CaseSampleView(BaseView):
column_editable_list = ["status"]
column_filters = ["status"]
column_formatters = {
"case": CaseView.view_family_link,
"case": CaseView.view_case_link,
"sample": SampleView.view_sample_link,
}
column_searchable_list = ["case.internal_id", "case.name", "sample.internal_id"]
Expand Down
4 changes: 2 additions & 2 deletions cg/store/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _get_subquery_with_latest_case_analysis_date(self) -> Query:
case_and_date: Query = (
self._get_join_analysis_case_query()
.group_by(Case.id)
.with_entities(Analysis.family_id, func.max(Analysis.started_at).label("started_at"))
.with_entities(Analysis.case_id, func.max(Analysis.started_at).label("started_at"))
.subquery()
)
return case_and_date
Expand All @@ -104,7 +104,7 @@ def _get_latest_analyses_for_cases_query(self) -> Query:
return analyses.join(
case_and_date_subquery,
and_(
Analysis.family_id == case_and_date_subquery.c.family_id,
Analysis.case_id == case_and_date_subquery.c.case_id,
Analysis.started_at == case_and_date_subquery.c.started_at,
),
)
Expand Down
2 changes: 1 addition & 1 deletion cg/store/api/find_business_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def has_active_cases_for_sample(self, internal_id: str) -> bool:
active_actions = ["analyze", "running"]

for family_sample in sample.links:
case: Case = self.get_case_by_entry_id(entry_id=family_sample.family_id)
case: Case = self.get_case_by_entry_id(entry_id=family_sample.case_id)
if case.action in active_actions:
return True

Expand Down
2 changes: 1 addition & 1 deletion cg/store/filters/status_analysis_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def order_analyses_by_uploaded_at_asc(analyses: Query, **kwargs) -> Query:

def filter_analyses_by_case_entry_id(analyses: Query, case_entry_id: int, **kwargs) -> Query:
"""Return a query of analysis filtered by case entry id."""
return analyses.filter(Analysis.family_id == case_entry_id)
return analyses.filter(Analysis.case_id == case_entry_id)


def filter_analyses_started_before(analyses: Query, started_at_date: datetime, **kwargs) -> Query:
Expand Down
10 changes: 5 additions & 5 deletions cg/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class Analysis(Model):
is_primary = Column(types.Boolean, default=False)

created_at = Column(types.DateTime, default=dt.datetime.now, nullable=False)
family_id = Column(ForeignKey("family.id", ondelete="CASCADE"), nullable=False)
case_id = Column(ForeignKey("case.id", ondelete="CASCADE"), nullable=False)
uploaded_to_vogue_at = Column(types.DateTime, nullable=True)

case = orm.relationship("Case", back_populates="analyses")
Expand Down Expand Up @@ -377,7 +377,7 @@ def to_dict(self):


class Case(Model, PriorityMixin):
__tablename__ = "family"
__tablename__ = "case"
__table_args__ = (UniqueConstraint("customer_id", "name", name="_customer_name_uc"),)

action = Column(types.Enum(*CASE_ACTIONS))
Expand Down Expand Up @@ -516,11 +516,11 @@ def to_dict(self, links: bool = False, analyses: bool = False) -> dict:


class CaseSample(Model):
__tablename__ = "family_sample"
__table_args__ = (UniqueConstraint("family_id", "sample_id", name="_family_sample_uc"),)
__tablename__ = "case_sample"
__table_args__ = (UniqueConstraint("case_id", "sample_id", name="_case_sample_uc"),)

id = Column(types.Integer, primary_key=True)
family_id = Column(ForeignKey("family.id", ondelete="CASCADE"), nullable=False)
case_id = Column(ForeignKey("case.id", ondelete="CASCADE"), nullable=False)
sample_id = Column(ForeignKey("sample.id", ondelete="CASCADE"), nullable=False)
status = Column(types.Enum(*STATUS_OPTIONS), default="unknown", nullable=False)

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/workflow/fastq/test_fastq_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_store_fastq_analysis(caplog, another_case_id: str, cli_runner, fastq_co
assert (
len(
fastq_context.status_db._get_query(table=Analysis)
.filter(Analysis.family_id == case_obj.id)
.filter(Analysis.case_id == case_obj.id)
.all()
)
> 0
Expand Down
4 changes: 1 addition & 3 deletions tests/store_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ def add_analysis(
if not case:
case = StoreHelpers.add_case(store, data_analysis=pipeline, data_delivery=data_delivery)

analysis = store.add_analysis(
pipeline=pipeline, version=pipeline_version, family_id=case.id
)
analysis = store.add_analysis(pipeline=pipeline, version=pipeline_version, case_id=case.id)

analysis.started_at = started_at or datetime.now()
if completed_at:
Expand Down

0 comments on commit eb9153c

Please sign in to comment.