From dc98b25639f44d22940555e2ae76cb543d806fef Mon Sep 17 00:00:00 2001 From: robbertuittenbroek Date: Tue, 13 Aug 2024 16:53:14 +0200 Subject: [PATCH] Make title field for tasks a string of 1024 characters --- ...cf279a7f62_change_title_length_for_task.py | 34 +++++++++++++++++++ amt/models/task.py | 4 +-- amt/repositories/tasks.py | 1 + amt/services/tasks.py | 4 ++- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 amt/migrations/versions/66cf279a7f62_change_title_length_for_task.py diff --git a/amt/migrations/versions/66cf279a7f62_change_title_length_for_task.py b/amt/migrations/versions/66cf279a7f62_change_title_length_for_task.py new file mode 100644 index 00000000..f887f952 --- /dev/null +++ b/amt/migrations/versions/66cf279a7f62_change_title_length_for_task.py @@ -0,0 +1,34 @@ +"""change title length for task + +Revision ID: 66cf279a7f62 +Revises: 9ce2341f2922 +Create Date: 2024-08-13 15:33:28.001811 + +""" + +from collections.abc import Sequence + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "66cf279a7f62" +down_revision: str | None = "9ce2341f2922" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + with op.batch_alter_table("task", schema=None) as batch_op: + batch_op.alter_column( + "title", existing_type=sa.VARCHAR(length=255), type_=sa.String(length=1024), existing_nullable=False + ) + + +def downgrade() -> None: + op.execute("UPDATE task SET title=SUBSTRING(title,1,255)") + + with op.batch_alter_table("task", schema=None) as batch_op: + batch_op.alter_column( + "title", existing_type=sa.String(length=1024), type_=sa.VARCHAR(length=255), existing_nullable=False + ) diff --git a/amt/models/task.py b/amt/models/task.py index 9fb58cf3..4fbd0f32 100644 --- a/amt/models/task.py +++ b/amt/models/task.py @@ -1,4 +1,4 @@ -from sqlalchemy import ForeignKey +from sqlalchemy import ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column from amt.models.base import Base @@ -8,7 +8,7 @@ class Task(Base): __tablename__ = "task" id: Mapped[int] = mapped_column(primary_key=True) - title: Mapped[str] + title: Mapped[str] = mapped_column(String(length=1024)) description: Mapped[str] sort_order: Mapped[float] status_id: Mapped[int | None] = mapped_column(default=None) diff --git a/amt/repositories/tasks.py b/amt/repositories/tasks.py index b86cbf8e..4f23724f 100644 --- a/amt/repositories/tasks.py +++ b/amt/repositories/tasks.py @@ -76,6 +76,7 @@ def save_all(self, tasks: Sequence[Task]) -> None: self.session.add_all(tasks) self.session.commit() except Exception as e: + logger.exception("Could not store tasks") self.session.rollback() raise RepositoryError from e diff --git a/amt/services/tasks.py b/amt/services/tasks.py index 2d47c76e..2b190584 100644 --- a/amt/services/tasks.py +++ b/amt/services/tasks.py @@ -43,7 +43,9 @@ def create_instrument_tasks(self, tasks: Sequence[InstrumentTask], project: Proj [ # TODO: (Christopher) The ticket does not specify what to do when question type is not an # open questions, hence for now all titles will be set to task.question. - Task(title=task.question, description="", project_id=project.id, status_id=status, sort_order=idx) + Task( + title=task.question[:1024], description="", project_id=project.id, status_id=status, sort_order=idx + ) for idx, task in enumerate(tasks) ] )