""" Канал push + настройки: таблицы app_settings и push_subscriptions, ck_channel с 'push'
Revision ID: e2f6a9b3c4d7
Revises: a1c4e7d52b98
Create Date: 2026-10-03
"""
from alembic import op
import sqlalchemy as sa
revision = "e2f6a9b3c4d7"
down_revision = "a1c4e7d52b98"
branch_labels = None
depends_on = None
CHANNEL_WITH_PUSH = "channel IN ('telegram', 'email', 's2s', 'internal_log', 'user', 'push')"
CHANNEL_NO_PUSH = "channel IN ('telegram', 'email', 's2s', 'internal_log', 'user')"
CHECK_TABLES = ("routing_rule_actions", "channel_targets", "deliveries")
def upgrade() -> None:
# ck_channel есть на трёх таблицах (см. 0004) — обновить каждую.
for table in CHECK_TABLES:
op.drop_constraint("ck_channel", table, type_="check")
for table in CHECK_TABLES:
op.create_check_constraint("ck_channel", table, CHANNEL_WITH_PUSH)
op.create_table(
"app_settings",
sa.Column("key", sa.String(64), primary_key=True),
sa.Column("value", sa.Text(), nullable=True),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
)
op.create_table(
"push_subscriptions",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("user_id", sa.String(64), nullable=False),
sa.Column("endpoint", sa.String(500), nullable=False, unique=True),
sa.Column("p256dh", sa.String(255), nullable=False),
sa.Column("auth", sa.String(255), nullable=False),
sa.Column("ua", sa.String(255), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
)
op.create_index("ix_pushsubs_user", "push_subscriptions", ["user_id", "created_at"])
def downgrade() -> None:
op.drop_index("ix_pushsubs_user", table_name="push_subscriptions")
op.drop_table("push_subscriptions")
op.drop_table("app_settings")
for table in CHECK_TABLES:
op.drop_constraint("ck_channel", table, type_="check")
for table in CHECK_TABLES:
op.create_check_constraint("ck_channel", table, CHANNEL_NO_PUSH)