From bc7f486aa7b543a934f4cf23dc80a95f44afcb64 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Mon, 23 Mar 2026 00:07:08 +0300 Subject: Add initial database migration --- alembic/versions/5d998cafe1ba_init_database.py | 55 ++++++++++++++++++++++++++ models/__init__.py | 4 ++ models/announcement.py | 19 ++++----- models/rich_text.py | 7 ++++ models/suggest.py | 23 +++++++++++ models/user.py | 15 +++++++ 6 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 alembic/versions/5d998cafe1ba_init_database.py create mode 100644 models/rich_text.py create mode 100644 models/suggest.py diff --git a/alembic/versions/5d998cafe1ba_init_database.py b/alembic/versions/5d998cafe1ba_init_database.py new file mode 100644 index 0000000..e55e4ed --- /dev/null +++ b/alembic/versions/5d998cafe1ba_init_database.py @@ -0,0 +1,55 @@ +"""init database + +Revision ID: 5d998cafe1ba +Revises: +Create Date: 2026-03-23 00:05:14.621886 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '5d998cafe1ba' +down_revision: Union[str, Sequence[str], None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('invoice', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('amount', sa.Float(), nullable=False), + sa.Column('datetime', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_invoice')) + ) + op.create_table('user', + sa.Column('id', sa.Integer(), autoincrement=False, nullable=False), + sa.Column('role', sa.Enum('REGULAR', 'ADMIN', name='userrole'), nullable=False), + sa.Column('vpn_link', sa.String(), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_user')) + ) + op.create_table('payment', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('invoice_id', sa.Integer(), nullable=False), + sa.Column('receipt_file_id', sa.String(), nullable=False), + sa.Column('datetime', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['invoice_id'], ['invoice.id'], name=op.f('fk_payment_invoice_id_invoice')), + sa.ForeignKeyConstraint(['user_id'], ['user.id'], name=op.f('fk_payment_user_id_user')), + sa.PrimaryKeyConstraint('id', name=op.f('pk_payment')) + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('payment') + op.drop_table('user') + op.drop_table('invoice') + # ### end Alembic commands ### diff --git a/models/__init__.py b/models/__init__.py index 70c841d..412f467 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -1,11 +1,15 @@ # isort: off from .base import BaseTable +from .rich_text import RichText from .user import User from .invoce import Invoice +from .payment import Payment # isort: on __all__ = [ "BaseTable", "User", "Invoice", + "Payment", + "RichText", ] diff --git a/models/announcement.py b/models/announcement.py index 5f752d7..9a6eef2 100644 --- a/models/announcement.py +++ b/models/announcement.py @@ -1,18 +1,19 @@ -from aiogram.types import MessageEntity -from pydantic import BaseModel from sqlalchemy import JSON from sqlalchemy.orm import Mapped, mapped_column -from models import BaseTable - - -class Message(BaseModel): - text: str - entities: list[MessageEntity] = [] +from models import BaseTable, RichText class Announcement(BaseTable): __tablename__ = "announcement" id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) - message: Mapped[str] = mapped_column(JSON()) + __message: Mapped[str] = mapped_column("message", JSON()) + + @property + def message(self) -> RichText: + return RichText.model_validate_json(self.__message) + + @message.setter + def message_set(self, value: RichText) -> None: + self.__message = value.model_dump_json() diff --git a/models/rich_text.py b/models/rich_text.py new file mode 100644 index 0000000..2b433ec --- /dev/null +++ b/models/rich_text.py @@ -0,0 +1,7 @@ +from aiogram.types import MessageEntity +from pydantic import BaseModel + + +class RichText(BaseModel): + text: str + entities: list[MessageEntity] = [] diff --git a/models/suggest.py b/models/suggest.py new file mode 100644 index 0000000..1ba18a0 --- /dev/null +++ b/models/suggest.py @@ -0,0 +1,23 @@ +from sqlalchemy import JSON +from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.sql.schema import ForeignKey + +from models import RichText, User +from models.base import BaseTable + + +class Suggest(BaseTable): + __tablename__ = "suggest" + + id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) + user_id: Mapped[int] = mapped_column(ForeignKey(User.id)) + suggested_user_id: Mapped[int] + __message: Mapped[str] = mapped_column("message", JSON()) + + @property + def message(self) -> RichText: + return RichText.model_validate_json(self.__message) + + @message.setter + def message_set(self, value: RichText) -> None: + self.__message = value.model_dump_json() diff --git a/models/user.py b/models/user.py index 7118725..4983a13 100644 --- a/models/user.py +++ b/models/user.py @@ -1,9 +1,24 @@ +from enum import IntEnum + from sqlalchemy.orm import Mapped, mapped_column from models import BaseTable +class UserRole(IntEnum): + REGULAR = 0 + ADMIN = 1 + + class User(BaseTable): __tablename__ = "user" id: Mapped[int] = mapped_column(primary_key=True, autoincrement=False) + role: Mapped[UserRole] = mapped_column(default=UserRole.REGULAR) + vpn_link: Mapped[str] + + def is_regular(self) -> bool: + return self.role >= UserRole.REGULAR + + def is_admin(self) -> bool: + return self.role == UserRole.ADMIN -- cgit v1.3