feat(shipping-notice): 收敛复核状态隔离、STP 重试幂等与 XFA 合成预览标注
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
84a881b554
commit
74270adc22
149 files changed
+20111
-3871
No files matched your search
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
backend/migrations/versions/cq1a2b3c4d5e_add_stp_job_retry_of_and_active_unique.py
|
||||
|
||||
用途:为 STP 自动建单 Job 增加 retry_of_job_id,以及 active(queued/running) 部分唯一索引。
|
||||
依赖:Alembic revision cp0a1b2c3d4e。
|
||||
逻辑:先合并同幂等键下多余的 queued/running STP Job(保留最大 id),再加列与唯一索引。
|
||||
|
||||
Revision ID: cq1a2b3c4d5e
|
||||
Revises: cp0a1b2c3d4e
|
||||
Create Date: 2026-08-07 00:30:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = 'cq1a2b3c4d5e'
|
||||
down_revision = 'cp0a1b2c3d4e'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _index_names(table: str) -> set[str]:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
return {str(ix.get('name') or '') for ix in inspector.get_indexes(table)}
|
||||
|
||||
|
||||
def _column_names(table: str) -> set[str]:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
return {str(col.get('name') or '') for col in inspector.get_columns(table)}
|
||||
|
||||
|
||||
def _dedupe_active_stp_jobs() -> None:
|
||||
"""同 domain/business_ref 的多条 queued/running STP Job:保留最大 id,其余标 cancelled。"""
|
||||
bind = op.get_bind()
|
||||
groups = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT domain, business_ref
|
||||
FROM jobs
|
||||
WHERE job_type = 'shipping_stp_auto_create'
|
||||
AND status IN ('queued', 'running')
|
||||
AND business_ref IS NOT NULL
|
||||
GROUP BY domain, business_ref
|
||||
HAVING COUNT(*) > 1
|
||||
"""
|
||||
)
|
||||
).fetchall()
|
||||
for domain, business_ref in groups:
|
||||
ids = [
|
||||
int(row[0])
|
||||
for row in bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT id
|
||||
FROM jobs
|
||||
WHERE domain = :domain
|
||||
AND job_type = 'shipping_stp_auto_create'
|
||||
AND business_ref = :business_ref
|
||||
AND status IN ('queued', 'running')
|
||||
ORDER BY id DESC
|
||||
"""
|
||||
),
|
||||
{'domain': str(domain), 'business_ref': str(business_ref)},
|
||||
).fetchall()
|
||||
]
|
||||
if len(ids) < 2:
|
||||
continue
|
||||
keep_id = ids[0]
|
||||
for drop_id in ids[1:]:
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE jobs
|
||||
SET status = 'cancelled',
|
||||
message = '并发去重:合并 active shipping_stp_auto_create',
|
||||
progress = 100,
|
||||
locked_by = NULL,
|
||||
locked_at = NULL,
|
||||
finished_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :drop_id
|
||||
AND id <> :keep_id
|
||||
AND status IN ('queued', 'running')
|
||||
"""
|
||||
),
|
||||
{'drop_id': drop_id, 'keep_id': keep_id},
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""加 retry_of_job_id 列与 STP active 部分唯一索引。"""
|
||||
if 'retry_of_job_id' not in _column_names('jobs'):
|
||||
op.add_column('jobs', sa.Column('retry_of_job_id', sa.Integer(), nullable=True))
|
||||
op.create_foreign_key(
|
||||
'fk_jobs_retry_of_job_id',
|
||||
'jobs',
|
||||
'jobs',
|
||||
['retry_of_job_id'],
|
||||
['id'],
|
||||
)
|
||||
op.create_index('ix_jobs_retry_of_job_id', 'jobs', ['retry_of_job_id'])
|
||||
|
||||
_dedupe_active_stp_jobs()
|
||||
if 'uq_jobs_shipping_stp_auto_create_active_ref' not in _index_names('jobs'):
|
||||
op.create_index(
|
||||
'uq_jobs_shipping_stp_auto_create_active_ref',
|
||||
'jobs',
|
||||
['domain', 'job_type', 'business_ref'],
|
||||
unique=True,
|
||||
postgresql_where=sa.text(
|
||||
"job_type = 'shipping_stp_auto_create' "
|
||||
"AND status IN ('queued', 'running') "
|
||||
"AND business_ref IS NOT NULL"
|
||||
),
|
||||
sqlite_where=sa.text(
|
||||
"job_type = 'shipping_stp_auto_create' "
|
||||
"AND status IN ('queued', 'running') "
|
||||
"AND business_ref IS NOT NULL"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""删除 STP active 唯一索引与 retry_of_job_id。"""
|
||||
if 'uq_jobs_shipping_stp_auto_create_active_ref' in _index_names('jobs'):
|
||||
op.drop_index('uq_jobs_shipping_stp_auto_create_active_ref', table_name='jobs')
|
||||
if 'retry_of_job_id' in _column_names('jobs'):
|
||||
if 'ix_jobs_retry_of_job_id' in _index_names('jobs'):
|
||||
op.drop_index('ix_jobs_retry_of_job_id', table_name='jobs')
|
||||
op.drop_constraint('fk_jobs_retry_of_job_id', 'jobs', type_='foreignkey')
|
||||
op.drop_column('jobs', 'retry_of_job_id')
|
||||
Reference in new issue
Block a user