8adad30ff2
- 新增 device 表 + /api/v1/device/{register,heartbeat} + 迁移 device_table
- heartbeat_monitor_worker 周期扫描心跳超时(App 被杀/无障碍停)→ 服务器终端打印告警
(推送本期未接,先用 logger 终端打印代替真实通知;integrations/jpush.py 已备,后续直接替换)
- config / .env.example 增 JPUSH_* / HEARTBEAT_*
- 见 spec(仓库外 e:\codes\spec\accessibility-liveness-push.md)
注:本提交同时快照了并行 session 未提交的 coupon_state 改动 + 相关 migration
(与本功能同迁移链耦合,无法单独拆分,经确认一并提交)。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
54 lines
2.4 KiB
Python
54 lines
2.4 KiB
Python
"""device table (无障碍保护存活检测 + 极光推送)
|
|
|
|
Revision ID: device_liveness_table
|
|
Revises: f3d0a16bb4c2
|
|
Create Date: 2026-06-15 12:00:00.000000
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = 'device_liveness_table'
|
|
down_revision: Union[str, Sequence[str], None] = 'f3d0a16bb4c2'
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
'device',
|
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
|
sa.Column('device_id', sa.String(length=128), nullable=False),
|
|
sa.Column('registration_id', sa.String(length=64), nullable=True),
|
|
sa.Column('platform', sa.String(length=16), nullable=False),
|
|
sa.Column('app_version', sa.String(length=32), nullable=True),
|
|
sa.Column('ever_protected', sa.Boolean(), nullable=False),
|
|
sa.Column('last_heartbeat_at', sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column('last_report_protection_on', sa.Boolean(), nullable=False),
|
|
sa.Column('liveness_state', sa.String(length=16), nullable=False),
|
|
sa.Column('notified_at', sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('user_id', 'device_id', name='uq_device_user_device'),
|
|
)
|
|
with op.batch_alter_table('device', schema=None) as batch_op:
|
|
batch_op.create_index(batch_op.f('ix_device_user_id'), ['user_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_device_device_id'), ['device_id'], unique=False)
|
|
batch_op.create_index(batch_op.f('ix_device_last_heartbeat_at'), ['last_heartbeat_at'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
with op.batch_alter_table('device', schema=None) as batch_op:
|
|
batch_op.drop_index(batch_op.f('ix_device_last_heartbeat_at'))
|
|
batch_op.drop_index(batch_op.f('ix_device_device_id'))
|
|
batch_op.drop_index(batch_op.f('ix_device_user_id'))
|
|
|
|
op.drop_table('device')
|