feat(chat): add random public_id and fix users blocked route
Some checks failed
CI / test (push) Failing after 20s
Some checks failed
CI / test (push) Failing after 20s
- add chats.public_id random identifier with migration 0011
- expose public_id in chat API payloads
- use chat public_id in message search UI label
- fix users router order so /users/blocked no longer conflicts with /users/{user_id}
This commit is contained in:
43
alembic/versions/0011_chat_public_id.py
Normal file
43
alembic/versions/0011_chat_public_id.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""add chats public_id
|
||||
|
||||
Revision ID: 0011_chat_public_id
|
||||
Revises: 0010_blocked_users
|
||||
Create Date: 2026-03-08 15:00:00.000000
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
from app.utils.id_generator import generate_public_id
|
||||
|
||||
|
||||
revision: str = "0011_chat_public_id"
|
||||
down_revision: Union[str, Sequence[str], None] = "0010_blocked_users"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("chats", sa.Column("public_id", sa.String(length=24), nullable=True))
|
||||
conn = op.get_bind()
|
||||
rows = list(conn.execute(sa.text("SELECT id FROM chats")))
|
||||
used: set[str] = set()
|
||||
for row in rows:
|
||||
chat_id = row[0]
|
||||
public_id = generate_public_id()
|
||||
while public_id in used:
|
||||
public_id = generate_public_id()
|
||||
used.add(public_id)
|
||||
conn.execute(
|
||||
sa.text("UPDATE chats SET public_id = :public_id WHERE id = :id"),
|
||||
{"public_id": public_id, "id": chat_id},
|
||||
)
|
||||
op.alter_column("chats", "public_id", existing_type=sa.String(length=24), nullable=False)
|
||||
op.create_index(op.f("ix_chats_public_id"), "chats", ["public_id"], unique=True)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(op.f("ix_chats_public_id"), table_name="chats")
|
||||
op.drop_column("chats", "public_id")
|
||||
Reference in New Issue
Block a user