Skip to content

Commit

Permalink
[migration] add server side constraint for Task.difficulty + set defa…
Browse files Browse the repository at this point in the history
…ult value to 3 not null
  • Loading branch information
EvanBldy committed Oct 8, 2024
1 parent a71be46 commit 0e4ef5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 4 additions & 5 deletions zou/app/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ class Task(db.Model, BaseMixin, SerializerMixin):
description = db.Column(db.Text())

priority = db.Column(db.Integer, default=0)
difficulty = db.Column(
db.Integer,
db.CheckConstraint("difficulty > 0 AND difficulty < 6"),
default=3,
)
difficulty = db.Column(db.Integer, default=3, nullable=False)
duration = db.Column(db.Float, default=0)
estimation = db.Column(db.Float, default=0)
completion_rate = db.Column(db.Integer, default=0)
Expand Down Expand Up @@ -75,6 +71,9 @@ class Task(db.Model, BaseMixin, SerializerMixin):
db.UniqueConstraint(
"name", "project_id", "task_type_id", "entity_id", name="task_uc"
),
db.CheckConstraint(
"difficulty > 0 AND difficulty < 6", name="check_difficulty"
),
)

def assignees_as_string(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ def upgrade():

with op.batch_alter_table("task", schema=None) as batch_op:
batch_op.add_column(
sa.Column("difficulty", sa.Integer(), nullable=True)
sa.Column(
"difficulty", sa.Integer(), nullable=False, server_default="3"
)
)
batch_op.create_check_constraint(
"check_difficulty", "difficulty > 0 AND difficulty < 6"
)

# ### end Alembic commands ###
Expand All @@ -51,6 +56,7 @@ def upgrade():
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("task", schema=None) as batch_op:
batch_op.drop_constraint("check_difficulty", type_="check")
batch_op.drop_column("difficulty")

with op.batch_alter_table("project", schema=None) as batch_op:
Expand Down

0 comments on commit 0e4ef5d

Please sign in to comment.