Skip to content

Commit

Permalink
Добавляет внешние ключи к таблицам
Browse files Browse the repository at this point in the history
  • Loading branch information
codEnjoyer committed Nov 2, 2023
1 parent baab615 commit 5b1f533
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Добавляет внешние ключи
Revision ID: 5aa9681ef1db
Revises: 6e79091c3fb1
Create Date: 2023-11-02 17:33:20.640710
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '5aa9681ef1db'
down_revision: Union[str, None] = '6e79091c3fb1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('employees', sa.Column('tutor_id', sa.UUID(), nullable=False))
op.create_foreign_key(None, 'employees', 'tutors', ['tutor_id'], ['id'])
op.add_column('levels', sa.Column('module_id', sa.UUID(), nullable=False))
op.create_foreign_key(None, 'levels', 'modules', ['module_id'], ['id'])
op.add_column('modules', sa.Column('previous_module_id', sa.UUID(), nullable=False))
op.add_column('modules', sa.Column('next_module_id', sa.UUID(), nullable=False))
op.create_foreign_key(None, 'modules', 'modules', ['next_module_id'], ['id'])
op.create_foreign_key(None, 'modules', 'modules', ['previous_module_id'], ['id'])
op.add_column('theory_videos', sa.Column('theory_id', sa.UUID(), nullable=False))
op.create_foreign_key(None, 'theory_videos', 'theory_blocks', ['theory_id'], ['id'])
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'theory_videos', type_='foreignkey')
op.drop_column('theory_videos', 'theory_id')
op.drop_constraint(None, 'modules', type_='foreignkey')
op.drop_constraint(None, 'modules', type_='foreignkey')
op.drop_column('modules', 'next_module_id')
op.drop_column('modules', 'previous_module_id')
op.drop_constraint(None, 'levels', type_='foreignkey')
op.drop_column('levels', 'module_id')
op.drop_constraint(None, 'employees', type_='foreignkey')
op.drop_column('employees', 'tutor_id')
# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion backend/src/game/levels/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing
import uuid

from sqlalchemy import String, Boolean, UUID
from sqlalchemy import String, Boolean, UUID, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from database import BaseModel
Expand All @@ -16,6 +16,7 @@ class Level(BaseModel):
__tablename__ = 'levels'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=False)
name: Mapped[str] = mapped_column(String(length=255), nullable=False)
is_accomplished: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)

Expand Down
6 changes: 4 additions & 2 deletions backend/src/game/modules/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing
import uuid

from sqlalchemy import String, UUID
from sqlalchemy import String, UUID, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from database import BaseModel
Expand All @@ -14,8 +14,10 @@ class Module(BaseModel):
__tablename__ = 'modules'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
previous_module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=False)
next_module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=False)
name: Mapped[str] = mapped_column(String(length=255), nullable=False)

levels: Mapped[list["Level"]] = relationship(back_populates='module')
previous_module: Mapped["Module"] = relationship(back_populates='next_module')
next_module: Mapped["Module"] = relationship(back_populates='previous_module')
previous_module: Mapped["Module"] = relationship(back_populates='next_module')
1 change: 1 addition & 0 deletions backend/src/game/units/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Task(BaseModel):

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
is_accomplished: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
# TODO: Задание не может быть выполнено глобально, нужна таблица для выполненных заданий пользователями
# requires_review: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
score_reward: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
content: Mapped[str] = mapped_column(Text, nullable=False, default="Текст задания")
Expand Down
3 changes: 2 additions & 1 deletion backend/src/game/units/theory/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid

from sqlalchemy import UUID, Text, String, URL
from sqlalchemy import UUID, Text, String, URL, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy_utils import URLType

Expand All @@ -21,6 +21,7 @@ class TheoryVideo(BaseModel):
__tablename__ = 'theory_videos'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
theory_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('theory_blocks.id'), nullable=False)
url: Mapped[URL] = mapped_column(URLType, nullable=False)

theory: Mapped["Theory"] = relationship(back_populates="theory_video")
3 changes: 2 additions & 1 deletion backend/src/users/employees/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime

from pydantic import EmailStr
from sqlalchemy import UUID, String, Date, func
from sqlalchemy import UUID, String, Date, func, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from database import BaseModel
Expand All @@ -16,6 +16,7 @@ class Employee(BaseModel):
__tablename__ = "employees"

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
tutor_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('tutors.id'), nullable=False)
name: Mapped[str] = mapped_column(String(length=255), nullable=False)
last_name: Mapped[str] = mapped_column(String(length=255), nullable=False)
email: Mapped[EmailStr] = mapped_column(String(length=255), nullable=False, unique=True)
Expand Down

0 comments on commit 5b1f533

Please sign in to comment.