50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
"""read_receipts hypertable
|
|
|
|
Revision ID: f3a8d1c5b7e2
|
|
Revises: e5c1f0a72b9d
|
|
Create Date: 2026-05-29 21:30:00.000000
|
|
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
revision: str = "f3a8d1c5b7e2"
|
|
down_revision: str | None = "e5c1f0a72b9d"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"read_receipts",
|
|
sa.Column("account_id", sa.Integer(), nullable=False),
|
|
sa.Column("chat_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("reader_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("ts", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("kind", sa.String(), nullable=False),
|
|
sa.Column("message_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("raw", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
|
|
sa.PrimaryKeyConstraint("account_id", "chat_id", "message_id", "ts"),
|
|
)
|
|
|
|
op.execute(
|
|
"SELECT create_hypertable('read_receipts', by_range('ts', INTERVAL '1 week'))"
|
|
)
|
|
op.execute(
|
|
"ALTER TABLE read_receipts SET ("
|
|
"timescaledb.enable_columnstore = true, "
|
|
"timescaledb.segmentby = 'chat_id', "
|
|
"timescaledb.orderby = 'ts DESC')"
|
|
)
|
|
op.execute(
|
|
"CALL add_columnstore_policy('read_receipts', after => INTERVAL '30 days')"
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("read_receipts")
|