From 86e3c7e8945547986da5e5e887f8ae797f17e2f9 Mon Sep 17 00:00:00 2001 From: robbertuittenbroek Date: Tue, 14 May 2024 14:32:59 +0200 Subject: [PATCH] Adding tables for databases --- .../versions/eb2eed884ae9_a_message.py | 67 +++++++++++++++++++ tad/models/__init__.py | 5 +- tad/models/status.py | 6 +- tad/models/task.py | 8 +-- tad/models/user.py | 7 +- tad/services/tasks_service.py | 12 ++-- 6 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 tad/migrations/versions/eb2eed884ae9_a_message.py diff --git a/tad/migrations/versions/eb2eed884ae9_a_message.py b/tad/migrations/versions/eb2eed884ae9_a_message.py new file mode 100644 index 000000000..0fc315225 --- /dev/null +++ b/tad/migrations/versions/eb2eed884ae9_a_message.py @@ -0,0 +1,67 @@ +"""a message + +Revision ID: eb2eed884ae9 +Revises: +Create Date: 2024-05-14 13:36:23.551663 + +""" + +from collections.abc import Sequence + +import sqlalchemy as sa +import sqlmodel.sql.sqltypes +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "eb2eed884ae9" +down_revision: str | None = None +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "hero", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "status", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("sort_order", sa.Float(), nullable=False), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "user", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("avatar", sqlmodel.sql.sqltypes.AutoString(), nullable=True), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "task", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("title", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("sort_order", sa.Float(), nullable=False), + sa.Column("status_id", sa.Integer(), nullable=False), + sa.Column("user_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["user_id"], + ["user.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("task") + op.drop_table("user") + op.drop_table("status") + op.drop_table("hero") + # ### end Alembic commands ### diff --git a/tad/models/__init__.py b/tad/models/__init__.py index e8084bec2..629df5cdc 100644 --- a/tad/models/__init__.py +++ b/tad/models/__init__.py @@ -1,3 +1,6 @@ from .hero import Hero +from .status import Status +from .task import Task +from .user import User -__all__ = ["Hero"] +__all__ = ["Hero", "Task", "Status", "User"] diff --git a/tad/models/status.py b/tad/models/status.py index 63a5569fa..514a50205 100644 --- a/tad/models/status.py +++ b/tad/models/status.py @@ -1,7 +1,7 @@ -from pydantic import BaseModel +from sqlmodel import Field, SQLModel -class Status(BaseModel): - id: int +class Status(SQLModel, table=True): + id: int = Field(default=None, primary_key=True) name: str sort_order: float diff --git a/tad/models/task.py b/tad/models/task.py index c9e906d07..12eef9c29 100644 --- a/tad/models/task.py +++ b/tad/models/task.py @@ -1,10 +1,10 @@ -from pydantic import BaseModel +from sqlmodel import Field, SQLModel -class Task(BaseModel): - id: int +class Task(SQLModel, table=True): + id: int = Field(default=None, primary_key=True) title: str description: str sort_order: float status_id: int - user_id: int | None = None + user_id: int | None = Field(default=None, foreign_key="user.id") diff --git a/tad/models/user.py b/tad/models/user.py index 0d1c3cb4a..0527e4697 100644 --- a/tad/models/user.py +++ b/tad/models/user.py @@ -1,6 +1,7 @@ -from pydantic import BaseModel +from sqlmodel import Field, SQLModel -class User(BaseModel): - id: int +class User(SQLModel, table=True): + id: int = Field(default=None, primary_key=True) name: str + avatar: str | None diff --git a/tad/services/tasks_service.py b/tad/services/tasks_service.py index daf698025..a495e4953 100644 --- a/tad/services/tasks_service.py +++ b/tad/services/tasks_service.py @@ -1,3 +1,6 @@ +from sqlmodel import Session + +from tad.core.db import engine from tad.models.status import Status from tad.models.task import Task from tad.models.user import User @@ -9,10 +12,6 @@ class TasksService: _tasks = list[Task] # make sure this is a singleton class, this may not be needed? - def __new__(cls): - if cls._self is None: - cls._self = super().__new__(cls) - return cls._self def __init__(self): # this is dummy data to get started, this should be retrieved from the database @@ -89,4 +88,9 @@ def move_task(self, task_id, status_id, previous_sibling_id, next_sibling_id) -> elif not previous_sibling_id and next_sibling_id: next_task = self.get_task(int(next_sibling_id)) task.sort_order = next_task.sort_order / 2 + + with Session(engine) as session: + session.add(task) + session.commit() + return task