RemnaWave-bot Update

This commit is contained in:
2026-05-06 15:41:56 +03:00
parent ce7a1f70b2
commit 2cc2f12a21
22 changed files with 3087 additions and 129 deletions

View File

@@ -17,6 +17,7 @@ class TelegramUser(Base):
language_code: Mapped[str | None] = mapped_column(String(16), nullable=True)
is_admin: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
is_blocked: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
discount_ever_used: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
last_seen_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow, onupdate=utcnow)
@@ -131,6 +132,9 @@ class SupportTicket(Base):
support_message_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
status: Mapped[str] = mapped_column(String(16), nullable=False, default="OPEN")
support_note: Mapped[str | None] = mapped_column(Text, nullable=True)
closed_by_telegram_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
closed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow, onupdate=utcnow)
@@ -143,6 +147,7 @@ class ReferralInvite(Base):
invited_telegram_id: Mapped[int] = mapped_column(BigInteger, nullable=False, unique=True, index=True)
invited_username: Mapped[str | None] = mapped_column(String(64), nullable=True)
invited_display_name: Mapped[str] = mapped_column(String(255), nullable=False)
discount_used: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow)
@@ -156,6 +161,34 @@ class ReferralCode(Base):
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow, onupdate=utcnow)
class PromoCode(Base):
__tablename__ = "promo_codes"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
code: Mapped[str] = mapped_column(String(32), nullable=False, unique=True, index=True)
plan_code: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
discount_percent: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
expires_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, index=True)
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, index=True)
created_by_telegram_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True, index=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow, onupdate=utcnow)
class PromoCodeApplication(Base):
__tablename__ = "promo_code_applications"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
telegram_id: Mapped[int] = mapped_column(BigInteger, nullable=False, unique=True, index=True)
promo_code_id: Mapped[int] = mapped_column(
ForeignKey("promo_codes.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=utcnow, onupdate=utcnow)
class ReferralBonus(Base):
__tablename__ = "referral_bonuses"

View File

@@ -17,6 +17,9 @@ logger = logging.getLogger(__name__)
PAYMENT_ORDERS_TABLE_NAME = "payment_orders"
PAYMENT_ORDERS_PROVISION_USERNAME_COLUMN = "provision_username"
PAYMENT_ORDERS_PROVISION_USERNAME_INDEX = "ix_payment_orders_provision_username"
SUPPORT_TICKETS_TABLE_NAME = "support_tickets"
REFERRAL_INVITES_TABLE_NAME = "referral_invites"
TELEGRAM_USERS_TABLE_NAME = "telegram_users"
def create_engine_and_session_factory(
@@ -121,6 +124,60 @@ async def _normalize_payment_order_provision_username_index(
)
async def _get_mysql_column_names(
connection: AsyncConnection,
*,
table_name: str,
) -> set[str]:
result = await connection.execute(
text(
"""
SELECT column_name
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = :table_name
"""
),
{"table_name": table_name},
)
return {str(row[0]) for row in result}
async def _normalize_support_ticket_columns(connection: AsyncConnection) -> None:
if connection.dialect.name != "mysql":
return
existing_columns = await _get_mysql_column_names(
connection,
table_name=SUPPORT_TICKETS_TABLE_NAME,
)
quoted_table_name = _quote_mysql_identifier(SUPPORT_TICKETS_TABLE_NAME)
required_columns = {
"support_note": "TEXT NULL",
"closed_by_telegram_id": "BIGINT NULL",
"closed_at": "DATETIME NULL",
}
for column_name, ddl in required_columns.items():
if column_name in existing_columns:
continue
logger.info(
"Adding missing column %s to %s",
column_name,
SUPPORT_TICKETS_TABLE_NAME,
)
await connection.execute(
text(
"ALTER TABLE "
f"{quoted_table_name} "
"ADD COLUMN "
f"{_quote_mysql_identifier(column_name)} {ddl}"
)
)
async def ensure_database_exists(
server_url: str,
database_name: str,
@@ -146,7 +203,60 @@ async def ensure_database_exists(
await engine.dispose()
async def _normalize_referral_invites_columns(connection: AsyncConnection) -> None:
"""Add discount_used column to referral_invites if missing."""
if connection.dialect.name != "mysql":
return
existing_columns = await _get_mysql_column_names(
connection,
table_name=REFERRAL_INVITES_TABLE_NAME,
)
quoted_table_name = _quote_mysql_identifier(REFERRAL_INVITES_TABLE_NAME)
if "discount_used" not in existing_columns:
logger.info(
"Adding missing column discount_used to %s",
REFERRAL_INVITES_TABLE_NAME,
)
await connection.execute(
text(
"ALTER TABLE "
f"{quoted_table_name} "
"ADD COLUMN `discount_used` BOOLEAN NOT NULL DEFAULT FALSE"
)
)
async def _normalize_telegram_users_columns(connection: AsyncConnection) -> None:
"""Add discount_ever_used column to telegram_users if missing."""
if connection.dialect.name != "mysql":
return
existing_columns = await _get_mysql_column_names(
connection,
table_name=TELEGRAM_USERS_TABLE_NAME,
)
quoted_table_name = _quote_mysql_identifier(TELEGRAM_USERS_TABLE_NAME)
if "discount_ever_used" not in existing_columns:
logger.info(
"Adding missing column discount_ever_used to %s",
TELEGRAM_USERS_TABLE_NAME,
)
await connection.execute(
text(
"ALTER TABLE "
f"{quoted_table_name} "
"ADD COLUMN `discount_ever_used` BOOLEAN NOT NULL DEFAULT FALSE"
)
)
async def init_db(engine: AsyncEngine) -> None:
async with engine.begin() as connection:
await connection.run_sync(Base.metadata.create_all)
await _normalize_payment_order_provision_username_index(connection)
await _normalize_support_ticket_columns(connection)
await _normalize_referral_invites_columns(connection)
await _normalize_telegram_users_columns(connection)