fix: change telegram_id from INTEGER to BIGINT to support large bot IDs
Some checks reported errors
continuous-integration/drone/push Build encountered an error

- Add migration 007 to change users.telegram_id from INTEGER to BIGINT
- Update User.telegram_id model to use BigInteger
- Update BannedUser.telegram_id model to use BigInteger
- Fixes asyncpg.exceptions.DataError: value 8300330445 out of int32 range
- PostgreSQL INTEGER supports only -2.1B to 2.1B, but Telegram IDs can exceed this
- BIGINT supports up to 9.2 quintillion, sufficient for all Telegram IDs
This commit is contained in:
2025-11-17 06:15:20 +09:00
parent 3d7338b3ed
commit d3f9f2fb53
2 changed files with 74 additions and 3 deletions

View File

@@ -0,0 +1,71 @@
"""Change telegram_id from INTEGER to BIGINT
Revision ID: 007
Revises: 006
Create Date: 2025-11-17 06:10:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '007'
down_revision = '006'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""
Изменяем тип telegram_id с INTEGER (int32) на BIGINT (int64)
для поддержки больших ID телеграм ботов (например, 8300330445).
PostgreSQL INTEGER поддерживает диапазон от -2,147,483,648 до 2,147,483,647.
Telegram ID могут превышать это значение, что вызывает ошибку:
"invalid input for query argument: value out of int32 range"
BIGINT поддерживает диапазон от -9,223,372,036,854,775,808 до 9,223,372,036,854,775,807.
"""
# Изменяем telegram_id в таблице users
op.alter_column(
'users',
'telegram_id',
existing_type=sa.INTEGER(),
type_=sa.BIGINT(),
existing_nullable=False
)
# Изменяем telegram_id в таблице banned_users
op.alter_column(
'banned_users',
'telegram_id',
existing_type=sa.INTEGER(),
type_=sa.BIGINT(),
existing_nullable=False
)
def downgrade() -> None:
"""
Откатываем изменения обратно на INTEGER.
ВНИМАНИЕ: Если в базе есть значения > 2,147,483,647, откат не удастся!
"""
# Откатываем telegram_id в таблице users
op.alter_column(
'users',
'telegram_id',
existing_type=sa.BIGINT(),
type_=sa.INTEGER(),
existing_nullable=False
)
# Откатываем telegram_id в таблице banned_users
op.alter_column(
'banned_users',
'telegram_id',
existing_type=sa.BIGINT(),
type_=sa.INTEGER(),
existing_nullable=False
)

View File

@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Text, JSON, UniqueConstraint from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Text, JSON, UniqueConstraint, BigInteger
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from datetime import datetime, timezone from datetime import datetime, timezone
from .database import Base from .database import Base
@@ -10,7 +10,7 @@ class User(Base):
__tablename__ = "users" __tablename__ = "users"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
telegram_id = Column(Integer, unique=True, nullable=False, index=True) telegram_id = Column(BigInteger, unique=True, nullable=False, index=True)
username = Column(String(255)) username = Column(String(255))
first_name = Column(String(255)) first_name = Column(String(255))
last_name = Column(String(255)) last_name = Column(String(255))
@@ -180,7 +180,7 @@ class BannedUser(Base):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"), nullable=False) user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
telegram_id = Column(Integer, nullable=False, index=True) telegram_id = Column(BigInteger, nullable=False, index=True)
banned_by = Column(Integer, ForeignKey("users.id"), nullable=False) banned_by = Column(Integer, ForeignKey("users.id"), nullable=False)
reason = Column(Text, nullable=True) reason = Column(Text, nullable=True)
banned_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)) banned_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))