Files
server-core/alembic/versions/0057_notification_system.py
T

71 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""实时通知体系:notifications 表扩展(分类/事件码/级别/深链/引用/已读时间)+ 索引 + 广播公告表
Revision ID: 0057
Revises: 0056
Create Date: 2026-09-05
"""
from alembic import op
import sqlalchemy as sa
revision = "0057"
down_revision = "0056"
branch_labels = None
depends_on = None
def upgrade():
# notifications 扩展列(MySQL VARCHAR 可带 DEFAULT;禁止 TEXT/JSON DEFAULT
for col, ctype, default in [
("category", sa.String(32), "system"),
("event_code", sa.String(64), ""),
("level", sa.String(16), "info"),
("link", sa.String(255), ""),
("ref_type", sa.String(64), ""),
("ref_id", sa.String(64), ""),
("read_at", sa.String(32), ""),
]:
try:
op.add_column("notifications", sa.Column(col, ctype, nullable=False, server_default=default))
except Exception: # noqa: BLE001 — 列已存在时跳过
pass
# 查询索引:用户未读角标 + 分页
try:
op.create_index("ix_notifications_user_read", "notifications", ["user_id", "read"])
except Exception: # noqa: BLE001
pass
try:
op.create_index("ix_notifications_user_created", "notifications", ["user_id", "created_at"])
except Exception: # noqa: BLE001
pass
# 广播公告表
op.create_table(
"announcements",
sa.Column("id", sa.String(32), primary_key=True),
sa.Column("title", sa.String(200), default=""),
sa.Column("content", sa.String(1000), default=""),
sa.Column("category", sa.String(32), default="system"),
sa.Column("level", sa.String(16), default="info"),
sa.Column("link", sa.String(255), default=""),
sa.Column("created_by", sa.String(32), default=""),
sa.Column("published_at", sa.String(32), default=""),
)
def downgrade():
op.drop_table("announcements")
try:
op.drop_index("ix_notifications_user_created", table_name="notifications")
except Exception: # noqa: BLE001
pass
try:
op.drop_index("ix_notifications_user_read", table_name="notifications")
except Exception: # noqa: BLE001
pass
for col in ["read_at", "ref_id", "ref_type", "link", "level", "event_code", "category"]:
try:
op.drop_column("notifications", col)
except Exception: # noqa: BLE001
pass