Skip to content

Commit

Permalink
Adding tables for databases
Browse files Browse the repository at this point in the history
  • Loading branch information
uittenbroekrobbert committed May 14, 2024
1 parent 2142f62 commit 86e3c7e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 15 deletions.
67 changes: 67 additions & 0 deletions tad/migrations/versions/eb2eed884ae9_a_message.py
Original file line number Diff line number Diff line change
@@ -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 ###
5 changes: 4 additions & 1 deletion tad/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
6 changes: 3 additions & 3 deletions tad/models/status.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tad/models/task.py
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 4 additions & 3 deletions tad/models/user.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 8 additions & 4 deletions tad/services/tasks_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 86e3c7e

Please sign in to comment.