From 0e4ef5ddd64022fdec159dbeabdbf1018c44f98b Mon Sep 17 00:00:00 2001 From: Evan Blaudy Date: Tue, 8 Oct 2024 17:47:12 +0200 Subject: [PATCH] [migration] add server side constraint for Task.difficulty + set default value to 3 not null --- zou/app/models/task.py | 9 ++++----- .../versions/8e67c183bed7_add_preference_fields.py | 8 +++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/zou/app/models/task.py b/zou/app/models/task.py index 2c85116ec0..a493ed2a8c 100644 --- a/zou/app/models/task.py +++ b/zou/app/models/task.py @@ -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) @@ -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): diff --git a/zou/migrations/versions/8e67c183bed7_add_preference_fields.py b/zou/migrations/versions/8e67c183bed7_add_preference_fields.py index a7951506ab..a0874562d8 100644 --- a/zou/migrations/versions/8e67c183bed7_add_preference_fields.py +++ b/zou/migrations/versions/8e67c183bed7_add_preference_fields.py @@ -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 ### @@ -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: