forked from FOSS/ASF-Control-Bot
60 lines
2.7 KiB
Python
60 lines
2.7 KiB
Python
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from bot.db.base import Base
|
|
|
|
|
|
def utc_now() -> datetime:
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
telegram_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
|
|
username: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
first_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
last_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
is_admin: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
# Имя столбца сохраняется для совместимости с существующей базой данных.
|
|
balance_rubles: Mapped[int] = mapped_column(
|
|
"balance_credits", BigInteger, default=0, nullable=False
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=utc_now,
|
|
onupdate=utc_now,
|
|
)
|
|
bots: Mapped[list["BotAccount"]] = relationship(back_populates="owner", cascade="all, delete-orphan")
|
|
|
|
|
|
class AppSetting(Base):
|
|
__tablename__ = "app_settings"
|
|
|
|
key: Mapped[str] = mapped_column(String(128), primary_key=True)
|
|
integer_value: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, onupdate=utc_now)
|
|
updated_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
|
|
|
|
|
|
class BotAccount(Base):
|
|
__tablename__ = "bot_accounts"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
owner_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
|
|
bot_name: Mapped[str] = mapped_column(String(255), unique=True, index=True)
|
|
steam_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
source_filename: Mapped[str] = mapped_column(String(255))
|
|
config_sha256: Mapped[str] = mapped_column(String(64), index=True)
|
|
upload_state: Mapped[str] = mapped_column(String(32), default="attached")
|
|
last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
is_attached: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, onupdate=utc_now)
|
|
attached_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
owner: Mapped[User] = relationship(back_populates="bots")
|