Skip to content

Commit

Permalink
db: Add migration for calculated field
Browse files Browse the repository at this point in the history
Add calculated field to task table to do the end - init calculation at db level. This will be used by work summary calculations.
  • Loading branch information
dmtrek14 committed Jan 22, 2024
1 parent 73c090d commit 74f87a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 ###
2 changes: 2 additions & 0 deletions api/models/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ForeignKey,
CheckConstraint,
UniqueConstraint,
FetchedValue,
)
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import relationship, Mapped
Expand All @@ -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)
Expand Down

0 comments on commit 74f87a7

Please sign in to comment.