Files
AlphaPulse/backend/alembic/versions/20261101_e5f701234567_news_concurrency.py
eezd cbd2618a20
Development CI / check-and-build (push) Successful in 2m35s
GitHub Git mirror / mirror (push) Successful in 5s
Development CI / deploy-development (push) Failing after 7s
feat: bound news AI concurrency with immediate task refill
2026-11-01 12:29:03 +08:00

29 lines
1.4 KiB
Python

"""Add the news pipeline concurrency limit and durable item states."""
from alembic import op
import sqlalchemy as sa
revision = 'e5f701234567'
down_revision = 'd4e6f8012345'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('risk_settings', sa.Column('max_concurrency', sa.Integer(), server_default='5', nullable=False))
op.create_check_constraint('risk_concurrency_positive', 'risk_settings', 'max_concurrency > 0')
op.execute("""UPDATE news_ai_reprocess_jobs j SET items = (
SELECT COALESCE(jsonb_agg(item || jsonb_build_object('state', CASE
WHEN ordinal > j.position THEN 'pending'
WHEN EXISTS (SELECT 1 FROM jsonb_array_elements(j.failures) f
WHERE f->>'stage' = 'grouping' AND f->>'id' = item->>'id') THEN 'failed'
ELSE 'done' END) ORDER BY ordinal), '[]'::jsonb)
FROM jsonb_array_elements(j.items) WITH ORDINALITY AS entries(item, ordinal))""")
def downgrade():
op.execute("""UPDATE news_ai_reprocess_jobs SET items = (
SELECT COALESCE(jsonb_agg(item - 'state' ORDER BY CASE WHEN item->>'state' IN ('done', 'failed') THEN 0 ELSE 1 END, ordinal), '[]'::jsonb)
FROM jsonb_array_elements(items) WITH ORDINALITY AS entries(item, ordinal))""")
op.drop_constraint('risk_concurrency_positive', 'risk_settings', type_='check')
op.drop_column('risk_settings', 'max_concurrency')