Skip to content

Commit

Permalink
Add Task Type entity
Browse files Browse the repository at this point in the history
This will be used in the task form and in the future we'll have a UI
so admins can manage this better.
  • Loading branch information
anarute committed Jul 17, 2023
1 parent db97ead commit b90a996
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
89 changes: 89 additions & 0 deletions api/migrations/versions/b7cbb5c2b970_add_task_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""Add task type
Revision ID: b7cbb5c2b970
Revises: ff0037261dd9
Create Date: 2023-07-13 18:06:28.663432
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "b7cbb5c2b970"
down_revision = "ff0037261dd9"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.alter_column("task", "ttype", nullable=True, new_column_name="ttype_tmp")

op.create_table(
"task_type",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("active", sa.Boolean(), nullable=True),
sa.Column("name", sa.String(), nullable=True),
sa.Column("slug", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("slug"),
)
op.add_column("task", sa.Column("ttype", sa.String(), nullable=True))
op.create_foreign_key("task_task_type_fkey", "task", "task_type", ["ttype"], ["slug"])

conn = op.get_bind()

# Clean up blank task types
query = sa.text("UPDATE task SET ttype_tmp = NULL WHERE ttype_tmp in ('', ' ', 'NULL')")
res = conn.execute(query)

# Add existing task types
query = sa.text(
"INSERT INTO task_type(slug, name, active) \
VALUES \
('administration', 'Administration', true), \
('analysis', 'Analysis', true), \
('community', 'Community', true), \
('coordination', 'Coordination', true), \
('demonstration', 'Demonstration', true), \
('deployment', 'Deployment', true), \
('design', 'Design', true), \
('documentation', 'Documentation', true), \
('environment', 'Environment', true), \
('implementation', 'Implementation', true), \
('maintenance', 'Maintenance', true), \
('publication', 'Publication', false), \
('requirements', 'Requirements', true), \
('sales', 'Sales', true), \
('sys_maintenance', 'Systems maintenance', true), \
('teaching', 'Teaching', true), \
('technology', 'Technology', true), \
('test', 'Test', true), \
('training', 'Training & Internal Events', true), \
('traveling', 'Travel', true), \
('deprecated_type', 'Deprecated Type', false)\
"
)
res = conn.execute(query)

# Migrate data from ttype to task_type, this might take a while
query = sa.text(
"UPDATE task SET ttype = ttype_tmp WHERE ttype_tmp is not null and ttype_tmp in (SELECT slug FROM task_type)"
)
res = conn.execute(query)

query = sa.text("ALTER TABLE task DROP COLUMN ttype_tmp CASCADE")
res = conn.execute(query)


def downgrade() -> None:
op.alter_column("task", "ttype", nullable=True, new_column_name="ttype_tmp")

op.add_column("task", sa.Column("ttype", sa.VARCHAR(length=40), autoincrement=False, nullable=True))
conn = op.get_bind()
query = sa.text("UPDATE task SET ttype = ttype_tmp")
res = conn.execute(query)

op.drop_constraint("task_task_type_fkey", "task", type_="foreignkey")
op.drop_column("task", "ttype_tmp")
op.drop_table("task_type")
11 changes: 10 additions & 1 deletion api/models/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Task(Base):
story = Column(String(length=80), nullable=True)
telework = Column(Boolean, nullable=True)
text = Column(String(length=8192), nullable=True)
ttype = Column(String(length=40), nullable=True)
task_type = Column("ttype", String, ForeignKey("task_type.slug"), nullable=True)
phase = Column(String(length=15), nullable=True)
onsite = Column(Boolean, default=False, nullable=False)
updated_at = Column(postgresql.TIMESTAMP(), nullable=True)
Expand All @@ -36,6 +36,15 @@ class Task(Base):
__table_args__ = (UniqueConstraint("usrid", "init", "_date", name="unique_task_usr_time"),)


class TaskType(Base):
__tablename__ = "task_type"

id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
active = Column(Boolean, nullable=True, default=True)
name = Column(String, nullable=True)
slug = Column(String, nullable=False, unique=True)


class ExtraHour(Base):
__tablename__ = "extra_hour"

Expand Down

0 comments on commit b90a996

Please sign in to comment.