From 782adcd3e313edbaef470c3e31bbcca38f397e9b Mon Sep 17 00:00:00 2001 From: Danielle Mayabb Date: Wed, 17 Jan 2024 13:05:14 -0800 Subject: [PATCH] db: Add migration for calculated field Add calculated field to task table to do the end - init calculation at db level. This will be used by work summary calculations. --- ...de50_add_calculated_field_to_task_table.py | 28 +++++++++++++++++++ api/models/timelog.py | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 api/migrations/versions/2a20fa1bde50_add_calculated_field_to_task_table.py diff --git a/api/migrations/versions/2a20fa1bde50_add_calculated_field_to_task_table.py b/api/migrations/versions/2a20fa1bde50_add_calculated_field_to_task_table.py new file mode 100644 index 000000000..1ac1969d9 --- /dev/null +++ b/api/migrations/versions/2a20fa1bde50_add_calculated_field_to_task_table.py @@ -0,0 +1,28 @@ +"""Add calculated field to task table + +Revision ID: 2a20fa1bde50 +Revises: 6a1ad39b566d +Create Date: 2024-01-09 11:15:22.286210 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '2a20fa1bde50' +down_revision = '6a1ad39b566d' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("task", sa.Column("task_total_minutes", sa.Integer(), sa.Computed("_end - init"))) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('task', 'task_total_minutes') + # ### end Alembic commands ### diff --git a/api/models/timelog.py b/api/models/timelog.py index 990157026..5dad56df6 100644 --- a/api/models/timelog.py +++ b/api/models/timelog.py @@ -8,6 +8,7 @@ ForeignKey, CheckConstraint, UniqueConstraint, + FetchedValue, ) from sqlalchemy.dialects import postgresql from sqlalchemy.orm import relationship, Mapped @@ -33,6 +34,7 @@ class Task(Base): phase = Column(String(length=15), nullable=True) onsite = Column(Boolean, default=False, nullable=False) updated_at = Column(postgresql.TIMESTAMP(), nullable=True) + task_total_minutes = Column(Integer, server_default=FetchedValue()) end_after_init_task = CheckConstraint("_end >= init AND init >= 0", name="end_after_init_task") user_id = Column("usrid", Integer, ForeignKey("usr.id"), nullable=False) project_id = Column("projectid", Integer, ForeignKey("project.id"), nullable=False)