fix: 补充 Alembic 迁移 + bzzoiro 批量优化 + validation 兼容层

- 新增 0004_snapshot_and_constraints.py: 重命名 confidence → subjective_confidence,
  新增 cutoff_at/input_hash, 添加 CHECK 约束
- bzzoiro.py: 预加载完整 Match 对象到内存,更新路径不再重复查询
- validation.py: 旧字段 confidence 兼容并打 deprecation 日志
This commit is contained in:
shangfangjian
2026-09-15 01:42:52 +08:00
parent 2c205c68b1
commit 98e3d07cf5
3 changed files with 70 additions and 25 deletions
@@ -0,0 +1,49 @@
"""add snapshot fields and check constraints
Revision ID: 0004_snapshot_and_constraints
Revises: 0003_injuries
Create Date: 2026-09-15
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '0004_snapshot_and_constraints'
down_revision: Union[str, None] = '0003_injuries'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# 1. 重命名 confidence → subjective_confidence
op.alter_column('predictions', 'confidence', new_column_name='subjective_confidence')
# 2. 新增快照字段
op.add_column('predictions', sa.Column('cutoff_at', sa.DateTime(timezone=True), nullable=True))
op.add_column('predictions', sa.Column('input_hash', sa.String(length=64), nullable=True))
# 3. 新增 CHECK 约束
op.create_check_constraint('ck_pred_home_goals_nonneg', 'predictions', 'pred_home_goals >= 0')
op.create_check_constraint('ck_pred_away_goals_nonneg', 'predictions', 'pred_away_goals >= 0')
op.create_check_constraint('ck_confidence_range', 'predictions', 'subjective_confidence >= 0 AND subjective_confidence <= 1')
op.create_check_constraint('ck_pred_1x2_enum', 'predictions', "pred_1x2 IN ('1', 'X', '2')")
op.create_check_constraint('ck_mode_enum', 'predictions', "mode IN ('single', 'multi')")
def downgrade() -> None:
# 1. 删除 CHECK 约束
op.drop_constraint('ck_mode_enum', 'predictions', type_='check')
op.drop_constraint('ck_pred_1x2_enum', 'predictions', type_='check')
op.drop_constraint('ck_confidence_range', 'predictions', type_='check')
op.drop_constraint('ck_pred_away_goals_nonneg', 'predictions', type_='check')
op.drop_constraint('ck_pred_home_goals_nonneg', 'predictions', type_='check')
# 2. 删除快照字段
op.drop_column('predictions', 'input_hash')
op.drop_column('predictions', 'cutoff_at')
# 3. 恢复列名
op.alter_column('predictions', 'subjective_confidence', new_column_name='confidence')