feat(shipping-notice): 新增发货通知重复检测与处理接口,支持重复决策保存与重新执行检测
This commit is contained in:
1 parent
729546a7b7
commit
ceb49461e8
72 files changed
+14270
-460
No files matched your search
+136
@@ -0,0 +1,136 @@
|
||||
"""
|
||||
backend/migrations/versions/a2b3c4d5e6f7_add_shipping_business_dedup_and_task_versions.py
|
||||
|
||||
用途:发货通知第三阶段业务去重字段、任务版本链与相关索引。
|
||||
依赖:Alembic revision c9d0e1f2a3b5。
|
||||
逻辑:为 shipping_notice_intakes / shipping_tasks 增加指纹与重复决策列;历史 document_version 回填为 1。
|
||||
|
||||
Revision ID: a2b3c4d5e6f7
|
||||
Revises: c9d0e1f2a3b5
|
||||
Create Date: 2026-07-14 09:35:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "a2b3c4d5e6f7"
|
||||
down_revision = "c9d0e1f2a3b5"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("shipping_notice_intakes", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("business_fingerprint", sa.String(length=64), nullable=True))
|
||||
batch_op.add_column(sa.Column("fingerprint_version", sa.String(length=32), nullable=True))
|
||||
batch_op.add_column(sa.Column("fingerprint_strength", sa.String(length=32), nullable=True))
|
||||
batch_op.add_column(
|
||||
sa.Column(
|
||||
"duplicate_status",
|
||||
sa.String(length=32),
|
||||
server_default="unchecked",
|
||||
nullable=False,
|
||||
)
|
||||
)
|
||||
batch_op.add_column(sa.Column("duplicate_matches_json", sa.JSON(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_checked_at", sa.DateTime(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_resolution", sa.String(length=32), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_resolution_reason", sa.Text(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_target_task_no", sa.String(length=64), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_resolved_by_id", sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_resolved_at", sa.DateTime(), nullable=True))
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_notice_intakes_duplicate_resolved_by_id_users",
|
||||
"users",
|
||||
["duplicate_resolved_by_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_index(
|
||||
"ix_shipping_notice_intakes_user_business_fingerprint",
|
||||
["user_id", "business_fingerprint"],
|
||||
unique=False,
|
||||
)
|
||||
batch_op.create_index(
|
||||
"ix_shipping_notice_intakes_duplicate_status_created",
|
||||
["duplicate_status", "created_at"],
|
||||
unique=False,
|
||||
)
|
||||
batch_op.create_index(
|
||||
"ix_shipping_notice_intakes_fp_version_fingerprint",
|
||||
["fingerprint_version", "business_fingerprint"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("business_fingerprint", sa.String(length=64), nullable=True))
|
||||
batch_op.add_column(sa.Column("fingerprint_version", sa.String(length=32), nullable=True))
|
||||
batch_op.add_column(
|
||||
sa.Column("document_version", sa.Integer(), server_default="1", nullable=False)
|
||||
)
|
||||
batch_op.add_column(sa.Column("supersedes_task_id", sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_source_task_id", sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_override_reason", sa.Text(), nullable=True))
|
||||
batch_op.add_column(sa.Column("duplicate_resolution_type", sa.String(length=32), nullable=True))
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_tasks_supersedes_task_id",
|
||||
"shipping_tasks",
|
||||
["supersedes_task_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_tasks_duplicate_source_task_id",
|
||||
"shipping_tasks",
|
||||
["duplicate_source_task_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_index(
|
||||
"ix_shipping_tasks_user_business_fingerprint",
|
||||
["user_id", "business_fingerprint"],
|
||||
unique=False,
|
||||
)
|
||||
batch_op.create_index("ix_shipping_tasks_supersedes_task_id", ["supersedes_task_id"], unique=False)
|
||||
batch_op.create_index(
|
||||
"ix_shipping_tasks_duplicate_source_task_id",
|
||||
["duplicate_source_task_id"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
op.execute("UPDATE shipping_tasks SET document_version = 1 WHERE document_version IS NULL")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_shipping_tasks_duplicate_source_task_id")
|
||||
batch_op.drop_index("ix_shipping_tasks_supersedes_task_id")
|
||||
batch_op.drop_index("ix_shipping_tasks_user_business_fingerprint")
|
||||
batch_op.drop_constraint("fk_shipping_tasks_duplicate_source_task_id", type_="foreignkey")
|
||||
batch_op.drop_constraint("fk_shipping_tasks_supersedes_task_id", type_="foreignkey")
|
||||
batch_op.drop_column("duplicate_resolution_type")
|
||||
batch_op.drop_column("duplicate_override_reason")
|
||||
batch_op.drop_column("duplicate_source_task_id")
|
||||
batch_op.drop_column("supersedes_task_id")
|
||||
batch_op.drop_column("document_version")
|
||||
batch_op.drop_column("fingerprint_version")
|
||||
batch_op.drop_column("business_fingerprint")
|
||||
|
||||
with op.batch_alter_table("shipping_notice_intakes", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_shipping_notice_intakes_fp_version_fingerprint")
|
||||
batch_op.drop_index("ix_shipping_notice_intakes_duplicate_status_created")
|
||||
batch_op.drop_index("ix_shipping_notice_intakes_user_business_fingerprint")
|
||||
batch_op.drop_constraint(
|
||||
"fk_shipping_notice_intakes_duplicate_resolved_by_id_users",
|
||||
type_="foreignkey",
|
||||
)
|
||||
batch_op.drop_column("duplicate_resolved_at")
|
||||
batch_op.drop_column("duplicate_resolved_by_id")
|
||||
batch_op.drop_column("duplicate_target_task_no")
|
||||
batch_op.drop_column("duplicate_resolution_reason")
|
||||
batch_op.drop_column("duplicate_resolution")
|
||||
batch_op.drop_column("duplicate_checked_at")
|
||||
batch_op.drop_column("duplicate_matches_json")
|
||||
batch_op.drop_column("duplicate_status")
|
||||
batch_op.drop_column("fingerprint_strength")
|
||||
batch_op.drop_column("fingerprint_version")
|
||||
batch_op.drop_column("business_fingerprint")
|
||||
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
backend/migrations/versions/a6b7c8d9e0f1_add_intake_parse_lease.py
|
||||
|
||||
用途:为 ShippingNoticeIntake 增加 parse_lease,隔离超时回收前后的解析执行代次。
|
||||
依赖:Alembic revision f5a6b7c8d9e0。
|
||||
逻辑:upgrade 增加可空 parse_lease;downgrade 删除该列。
|
||||
|
||||
Revision ID: a6b7c8d9e0f1
|
||||
Revises: f5a6b7c8d9e0
|
||||
Create Date: 2026-07-14 19:40:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "a6b7c8d9e0f1"
|
||||
down_revision = "f5a6b7c8d9e0"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""增加 parse_lease 列。"""
|
||||
op.add_column(
|
||||
"shipping_notice_intakes",
|
||||
sa.Column("parse_lease", sa.String(length=64), nullable=True),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""删除 parse_lease 列。"""
|
||||
op.drop_column("shipping_notice_intakes", "parse_lease")
|
||||
@@ -0,0 +1,145 @@
|
||||
"""
|
||||
backend/migrations/versions/b7c8d9e0f1a2_jobs_parse_active_unique.py
|
||||
|
||||
用途:为 shipping_notice_parse 的 queued Job 增加部分唯一索引,防止并发双插。
|
||||
依赖:Alembic revision a6b7c8d9e0f1。
|
||||
逻辑:先合并历史重复 queued 预解析 Job,将 Intake.parse_job_id 重指 keep_id,
|
||||
再取消其余重复行;最后创建 uq_jobs_shipping_parse_queued_ref。
|
||||
故意不含 running,以便 retry 在旧 running 残留时可另建 queued。
|
||||
|
||||
Revision ID: b7c8d9e0f1a2
|
||||
Revises: a6b7c8d9e0f1
|
||||
Create Date: 2026-07-14 20:00:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "b7c8d9e0f1a2"
|
||||
down_revision = "a6b7c8d9e0f1"
|
||||
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 _retarget_intake_parse_job_ids(bind, *, keep_id: int, drop_ids: list[int]) -> None:
|
||||
"""
|
||||
将被取消 Job 上的 Intake.parse_job_id 重指向保留的 queued Job。
|
||||
|
||||
须在取消 drop 行之前调用,避免升级后出现 queued 状态挂着 cancelled job_id。
|
||||
"""
|
||||
for drop_id in drop_ids:
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE shipping_notice_intakes
|
||||
SET parse_job_id = :keep_id,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE parse_job_id = :drop_id
|
||||
"""
|
||||
),
|
||||
{"keep_id": int(keep_id), "drop_id": int(drop_id)},
|
||||
)
|
||||
|
||||
|
||||
def _dedupe_queued_parse_jobs() -> None:
|
||||
"""同 domain/business_ref 的多条 queued 预解析 Job:保留最小 id,其余标 cancelled。"""
|
||||
bind = op.get_bind()
|
||||
groups = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT domain, business_ref
|
||||
FROM jobs
|
||||
WHERE job_type = 'shipping_notice_parse'
|
||||
AND status = 'queued'
|
||||
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_notice_parse'
|
||||
AND business_ref = :business_ref
|
||||
AND status = 'queued'
|
||||
ORDER BY id ASC
|
||||
"""
|
||||
),
|
||||
{"domain": str(domain), "business_ref": str(business_ref)},
|
||||
).fetchall()
|
||||
]
|
||||
if len(ids) < 2:
|
||||
continue
|
||||
keep_id = ids[0]
|
||||
drop_ids = ids[1:]
|
||||
# [STEP 1] 先重指 Intake,再取消重复 Job,避免 parse_job_id 悬空
|
||||
_retarget_intake_parse_job_ids(bind, keep_id=keep_id, drop_ids=drop_ids)
|
||||
for drop_id in drop_ids:
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE jobs
|
||||
SET status = 'cancelled',
|
||||
message = '并发去重:合并 queued shipping_notice_parse',
|
||||
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 = 'queued'
|
||||
"""
|
||||
),
|
||||
{"drop_id": drop_id, "keep_id": keep_id},
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""去重后创建 shipping_notice_parse queued 部分唯一索引。"""
|
||||
_dedupe_queued_parse_jobs()
|
||||
# 兼容误建的旧名
|
||||
for obsolete in ("uq_jobs_shipping_parse_active_ref", "uq_jobs_shipping_parse_queued_ref"):
|
||||
if obsolete == "uq_jobs_shipping_parse_queued_ref":
|
||||
continue
|
||||
if obsolete in _index_names("jobs"):
|
||||
op.drop_index(obsolete, table_name="jobs")
|
||||
if "uq_jobs_shipping_parse_queued_ref" in _index_names("jobs"):
|
||||
return
|
||||
op.create_index(
|
||||
"uq_jobs_shipping_parse_queued_ref",
|
||||
"jobs",
|
||||
["domain", "job_type", "business_ref"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text(
|
||||
"job_type = 'shipping_notice_parse' "
|
||||
"AND status = 'queued' "
|
||||
"AND business_ref IS NOT NULL"
|
||||
),
|
||||
sqlite_where=sa.text(
|
||||
"job_type = 'shipping_notice_parse' "
|
||||
"AND status = 'queued' "
|
||||
"AND business_ref IS NOT NULL"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""删除部分唯一索引。"""
|
||||
if "uq_jobs_shipping_parse_queued_ref" in _index_names("jobs"):
|
||||
op.drop_index("uq_jobs_shipping_parse_queued_ref", table_name="jobs")
|
||||
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
backend/migrations/versions/c2d3e4f5a6b7_intake_idempotency_and_supersedes_unique.py
|
||||
|
||||
用途:intake 建单幂等键落库;任务 supersedes 唯一约束防并发双修订。
|
||||
依赖:Alembic revision a2b3c4d5e6f7。
|
||||
逻辑:shipping_notice_intakes.create_idempotency_key 支撑混合批次 /
|
||||
纯 open_existing 重放;shipping_tasks.supersedes_task_id 唯一保证线性版本链。
|
||||
|
||||
Revision ID: c2d3e4f5a6b7
|
||||
Revises: a2b3c4d5e6f7
|
||||
Create Date: 2026-07-14 11:00:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "c2d3e4f5a6b7"
|
||||
down_revision = "a2b3c4d5e6f7"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("shipping_notice_intakes", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("create_idempotency_key", sa.String(length=128), nullable=True))
|
||||
batch_op.create_index(
|
||||
"ix_shipping_notice_intakes_user_create_idempotency",
|
||||
["user_id", "create_idempotency_key"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_shipping_tasks_supersedes_task_id")
|
||||
# PostgreSQL/SQLite:UNIQUE 允许多个 NULL,非空时每任务至多一个后继修订
|
||||
batch_op.create_index(
|
||||
"uq_shipping_tasks_supersedes_task_id",
|
||||
["supersedes_task_id"],
|
||||
unique=True,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.drop_index("uq_shipping_tasks_supersedes_task_id")
|
||||
batch_op.create_index("ix_shipping_tasks_supersedes_task_id", ["supersedes_task_id"], unique=False)
|
||||
|
||||
with op.batch_alter_table("shipping_notice_intakes", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_shipping_notice_intakes_user_create_idempotency")
|
||||
batch_op.drop_column("create_idempotency_key")
|
||||
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
backend/migrations/versions/c8d9e0f1a2b3_repair_intake_parse_job_id_after_dedupe.py
|
||||
|
||||
用途:修复已执行 b7c8d9e0f1a2 但未重指 parse_job_id 的环境。
|
||||
依赖:Alembic revision b7c8d9e0f1a2。
|
||||
逻辑:若 Intake.parse_job_id 指向已取消的 shipping_notice_parse Job,
|
||||
且同 (domain, business_ref) 仍有 queued 同伴,则重指到最小 queued id。
|
||||
|
||||
Revision ID: c8d9e0f1a2b3
|
||||
Revises: b7c8d9e0f1a2
|
||||
Create Date: 2026-07-14 20:10:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "c8d9e0f1a2b3"
|
||||
down_revision = "b7c8d9e0f1a2"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""将指向 cancelled 预解析 Job 的 Intake.parse_job_id 重指到同 domain 的 queued Job。"""
|
||||
bind = op.get_bind()
|
||||
rows = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT i.id AS intake_id,
|
||||
j.domain AS domain,
|
||||
j.business_ref AS business_ref
|
||||
FROM shipping_notice_intakes AS i
|
||||
INNER JOIN jobs AS j ON j.id = i.parse_job_id
|
||||
WHERE j.job_type = 'shipping_notice_parse'
|
||||
AND j.status = 'cancelled'
|
||||
AND j.business_ref IS NOT NULL
|
||||
"""
|
||||
)
|
||||
).fetchall()
|
||||
for intake_id, domain, business_ref in rows:
|
||||
keep = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT id
|
||||
FROM jobs
|
||||
WHERE domain = :domain
|
||||
AND job_type = 'shipping_notice_parse'
|
||||
AND status = 'queued'
|
||||
AND business_ref = :business_ref
|
||||
ORDER BY id ASC
|
||||
LIMIT 1
|
||||
"""
|
||||
),
|
||||
{"domain": str(domain), "business_ref": str(business_ref)},
|
||||
).fetchone()
|
||||
if keep is None:
|
||||
continue
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE shipping_notice_intakes
|
||||
SET parse_job_id = :keep_id,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :intake_id
|
||||
AND parse_job_id IS NOT NULL
|
||||
"""
|
||||
),
|
||||
{"keep_id": int(keep[0]), "intake_id": int(intake_id)},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""数据修复不可逆。"""
|
||||
pass
|
||||
@@ -0,0 +1,238 @@
|
||||
"""
|
||||
backend/migrations/versions/d3e4f5a6b7c8_shipping_notice_documents_candidates.py
|
||||
|
||||
用途:阶段二——FileAsset 派生字段、ShippingNoticeDocument/Candidate 表、Task.source_candidate_id。
|
||||
依赖:Alembic revision c2d3e4f5a6b7。
|
||||
逻辑:EML 附件子资产挂 parent_asset_id;文档/候选支撑「一邮件多订单」识别(不创建 Task)。
|
||||
|
||||
Revision ID: d3e4f5a6b7c8
|
||||
Revises: c2d3e4f5a6b7
|
||||
Create Date: 2026-07-14 17:30:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "d3e4f5a6b7c8"
|
||||
down_revision = "c2d3e4f5a6b7"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("file_assets", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("parent_asset_id", sa.Integer(), nullable=True))
|
||||
batch_op.add_column(
|
||||
sa.Column("asset_role", sa.String(length=32), server_default="upload", nullable=False)
|
||||
)
|
||||
batch_op.add_column(sa.Column("source_attachment_key", sa.String(length=64), nullable=True))
|
||||
batch_op.create_foreign_key(
|
||||
"fk_file_assets_parent_asset_id",
|
||||
"file_assets",
|
||||
["parent_asset_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_index("ix_file_assets_parent_asset_id", ["parent_asset_id"], unique=False)
|
||||
batch_op.create_index(
|
||||
"ix_file_assets_parent_content_hash",
|
||||
["parent_asset_id", "content_hash"],
|
||||
unique=False,
|
||||
)
|
||||
batch_op.create_index(
|
||||
"ix_file_assets_parent_attachment_key",
|
||||
["parent_asset_id", "source_attachment_key"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"shipping_notice_documents",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("intake_id", sa.Integer(), nullable=False),
|
||||
sa.Column("asset_id", sa.Integer(), nullable=True),
|
||||
sa.Column("document_type", sa.String(length=32), nullable=False),
|
||||
sa.Column("attachment_key", sa.String(length=64), nullable=False),
|
||||
sa.Column("filename", sa.String(length=255), nullable=True),
|
||||
sa.Column("content_hash", sa.String(length=64), nullable=True),
|
||||
sa.Column("classification", sa.String(length=32), nullable=False),
|
||||
sa.Column("parse_status", sa.String(length=32), nullable=False),
|
||||
sa.Column("extracted_text", sa.Text(), nullable=True),
|
||||
sa.Column("parse_warnings_json", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(["asset_id"], ["file_assets.id"]),
|
||||
sa.ForeignKeyConstraint(["intake_id"], ["shipping_notice_intakes.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint(
|
||||
"intake_id",
|
||||
"attachment_key",
|
||||
name="uq_shipping_notice_documents_intake_attachment_key",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_documents_intake_id",
|
||||
"shipping_notice_documents",
|
||||
["intake_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_documents_asset_id",
|
||||
"shipping_notice_documents",
|
||||
["asset_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_documents_content_hash",
|
||||
"shipping_notice_documents",
|
||||
["content_hash"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_documents_parse_status",
|
||||
"shipping_notice_documents",
|
||||
["parse_status"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_documents_created_at",
|
||||
"shipping_notice_documents",
|
||||
["created_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"shipping_notice_candidates",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("candidate_no", sa.String(length=64), nullable=False),
|
||||
sa.Column("intake_id", sa.Integer(), nullable=False),
|
||||
sa.Column("primary_document_id", sa.Integer(), nullable=False),
|
||||
sa.Column("po_number", sa.String(length=128), nullable=True),
|
||||
sa.Column("structured_result_json", sa.JSON(), nullable=True),
|
||||
sa.Column("confidence", sa.Float(), nullable=True),
|
||||
sa.Column(
|
||||
"validation_status",
|
||||
sa.String(length=32),
|
||||
server_default="pending",
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("conflicts_json", sa.JSON(), nullable=True),
|
||||
sa.Column("selected", sa.Boolean(), server_default=sa.text("true"), nullable=False),
|
||||
sa.Column("business_fingerprint", sa.String(length=64), nullable=True),
|
||||
sa.Column("fingerprint_version", sa.String(length=32), nullable=True),
|
||||
sa.Column("fingerprint_strength", sa.String(length=32), nullable=True),
|
||||
sa.Column(
|
||||
"duplicate_status",
|
||||
sa.String(length=32),
|
||||
server_default="unchecked",
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("duplicate_matches_json", sa.JSON(), nullable=True),
|
||||
sa.Column("duplicate_resolution", sa.String(length=32), nullable=True),
|
||||
sa.Column("duplicate_resolution_reason", sa.Text(), nullable=True),
|
||||
sa.Column("duplicate_target_task_no", sa.String(length=64), nullable=True),
|
||||
sa.Column("duplicate_checked_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("consumed_task_id", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(["consumed_task_id"], ["shipping_tasks.id"]),
|
||||
sa.ForeignKeyConstraint(["intake_id"], ["shipping_notice_intakes.id"]),
|
||||
sa.ForeignKeyConstraint(["primary_document_id"], ["shipping_notice_documents.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("candidate_no", name="uq_shipping_notice_candidates_candidate_no"),
|
||||
sa.UniqueConstraint(
|
||||
"intake_id",
|
||||
"primary_document_id",
|
||||
name="uq_shipping_notice_candidates_intake_primary_document",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_candidates_intake_id",
|
||||
"shipping_notice_candidates",
|
||||
["intake_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_candidates_primary_document_id",
|
||||
"shipping_notice_candidates",
|
||||
["primary_document_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_candidates_po_number",
|
||||
"shipping_notice_candidates",
|
||||
["po_number"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_candidates_user_fingerprint",
|
||||
"shipping_notice_candidates",
|
||||
["business_fingerprint"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_candidates_duplicate_status",
|
||||
"shipping_notice_candidates",
|
||||
["duplicate_status"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_shipping_notice_candidates_created_at",
|
||||
"shipping_notice_candidates",
|
||||
["created_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("source_candidate_id", sa.Integer(), nullable=True))
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_tasks_source_candidate_id",
|
||||
"shipping_notice_candidates",
|
||||
["source_candidate_id"],
|
||||
["id"],
|
||||
)
|
||||
batch_op.create_index(
|
||||
"ix_shipping_tasks_source_candidate_id",
|
||||
["source_candidate_id"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("shipping_tasks", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_shipping_tasks_source_candidate_id")
|
||||
batch_op.drop_constraint("fk_shipping_tasks_source_candidate_id", type_="foreignkey")
|
||||
batch_op.drop_column("source_candidate_id")
|
||||
|
||||
op.drop_index("ix_shipping_notice_candidates_created_at", table_name="shipping_notice_candidates")
|
||||
op.drop_index(
|
||||
"ix_shipping_notice_candidates_duplicate_status",
|
||||
table_name="shipping_notice_candidates",
|
||||
)
|
||||
op.drop_index(
|
||||
"ix_shipping_notice_candidates_user_fingerprint",
|
||||
table_name="shipping_notice_candidates",
|
||||
)
|
||||
op.drop_index("ix_shipping_notice_candidates_po_number", table_name="shipping_notice_candidates")
|
||||
op.drop_index(
|
||||
"ix_shipping_notice_candidates_primary_document_id",
|
||||
table_name="shipping_notice_candidates",
|
||||
)
|
||||
op.drop_index("ix_shipping_notice_candidates_intake_id", table_name="shipping_notice_candidates")
|
||||
op.drop_table("shipping_notice_candidates")
|
||||
|
||||
op.drop_index("ix_shipping_notice_documents_created_at", table_name="shipping_notice_documents")
|
||||
op.drop_index("ix_shipping_notice_documents_parse_status", table_name="shipping_notice_documents")
|
||||
op.drop_index("ix_shipping_notice_documents_content_hash", table_name="shipping_notice_documents")
|
||||
op.drop_index("ix_shipping_notice_documents_asset_id", table_name="shipping_notice_documents")
|
||||
op.drop_index("ix_shipping_notice_documents_intake_id", table_name="shipping_notice_documents")
|
||||
op.drop_table("shipping_notice_documents")
|
||||
|
||||
with op.batch_alter_table("file_assets", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_file_assets_parent_attachment_key")
|
||||
batch_op.drop_index("ix_file_assets_parent_content_hash")
|
||||
batch_op.drop_index("ix_file_assets_parent_asset_id")
|
||||
batch_op.drop_constraint("fk_file_assets_parent_asset_id", type_="foreignkey")
|
||||
batch_op.drop_column("source_attachment_key")
|
||||
batch_op.drop_column("asset_role")
|
||||
batch_op.drop_column("parent_asset_id")
|
||||
@@ -0,0 +1,225 @@
|
||||
"""
|
||||
backend/migrations/versions/e4f5a6b7c8d9_stage2_idempotency_and_candidate_fk.py
|
||||
|
||||
用途:阶段二并发幂等与 Candidate/Document intake 一致性约束。
|
||||
依赖:Alembic revision d3e4f5a6b7c8。
|
||||
逻辑:合并重复派生资产 → 按 Document.asset_id 重算 reference_count/lifecycle →
|
||||
建立「未删除」部分唯一索引;Candidate 复合外键保证主文档同属该 Intake。
|
||||
|
||||
Revision ID: e4f5a6b7c8d9
|
||||
Revises: d3e4f5a6b7c8
|
||||
Create Date: 2026-07-14 18:15:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "e4f5a6b7c8d9"
|
||||
down_revision = "d3e4f5a6b7c8"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _drop_document_fk_on_candidates() -> None:
|
||||
"""删除 Candidate → Document 的旧单列外键(名称因库而异)。"""
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
for fk in inspector.get_foreign_keys("shipping_notice_candidates"):
|
||||
cols = list(fk.get("constrained_columns") or [])
|
||||
referred = str(fk.get("referred_table") or "")
|
||||
if referred == "shipping_notice_documents" and cols == ["primary_document_id"]:
|
||||
op.drop_constraint(fk["name"], "shipping_notice_candidates", type_="foreignkey")
|
||||
return
|
||||
|
||||
|
||||
def _dedupe_derived_file_assets() -> None:
|
||||
"""
|
||||
合并同父同哈希的活派生资产,再建立唯一约束。
|
||||
|
||||
核心逻辑:保留最小 id;Document.asset_id 重指向保留行;多余行 soft-delete。
|
||||
"""
|
||||
bind = op.get_bind()
|
||||
rows = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT parent_asset_id, content_hash
|
||||
FROM file_assets
|
||||
WHERE parent_asset_id IS NOT NULL
|
||||
AND deleted_at IS NULL
|
||||
AND content_hash IS NOT NULL
|
||||
GROUP BY parent_asset_id, content_hash
|
||||
HAVING COUNT(*) > 1
|
||||
"""
|
||||
)
|
||||
).fetchall()
|
||||
for parent_asset_id, content_hash in rows:
|
||||
ids = [
|
||||
int(r[0])
|
||||
for r in bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT id
|
||||
FROM file_assets
|
||||
WHERE parent_asset_id = :parent_id
|
||||
AND content_hash = :content_hash
|
||||
AND deleted_at IS NULL
|
||||
ORDER BY id ASC
|
||||
"""
|
||||
),
|
||||
{"parent_id": int(parent_asset_id), "content_hash": str(content_hash)},
|
||||
).fetchall()
|
||||
]
|
||||
if len(ids) < 2:
|
||||
continue
|
||||
keep_id = ids[0]
|
||||
for drop_id in ids[1:]:
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE shipping_notice_documents
|
||||
SET asset_id = :keep_id
|
||||
WHERE asset_id = :drop_id
|
||||
"""
|
||||
),
|
||||
{"keep_id": keep_id, "drop_id": drop_id},
|
||||
)
|
||||
# 转为 orphan_cleanup + expired(deleted_at 保持 NULL),交给 cleanup 删 MinIO
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE file_assets
|
||||
SET parent_asset_id = NULL,
|
||||
asset_role = 'orphan_cleanup',
|
||||
source_attachment_key = NULL,
|
||||
lifecycle_status = 'expired',
|
||||
reference_count = 0,
|
||||
expires_at = CURRENT_TIMESTAMP,
|
||||
deleted_at = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :drop_id
|
||||
"""
|
||||
),
|
||||
{"drop_id": drop_id},
|
||||
)
|
||||
|
||||
|
||||
def _recompute_derived_reference_counts() -> None:
|
||||
"""
|
||||
按 shipping_notice_documents.asset_id 重算活派生资产的引用数与生命周期。
|
||||
|
||||
核心逻辑:合并后保留行可能仍残留旧 reference_count;以 Document 关联边为准校正。
|
||||
"""
|
||||
bind = op.get_bind()
|
||||
dialect = str(getattr(bind.dialect, "name", "") or "")
|
||||
expires_expr = (
|
||||
"CURRENT_TIMESTAMP + INTERVAL '72 hours'"
|
||||
if dialect == "postgresql"
|
||||
else "datetime('now', '+72 hours')"
|
||||
)
|
||||
bind.execute(
|
||||
sa.text(
|
||||
f"""
|
||||
UPDATE file_assets
|
||||
SET
|
||||
reference_count = (
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notice_documents AS d
|
||||
WHERE d.asset_id = file_assets.id
|
||||
),
|
||||
lifecycle_status = CASE
|
||||
WHEN (
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notice_documents AS d
|
||||
WHERE d.asset_id = file_assets.id
|
||||
) > 0 THEN 'referenced'
|
||||
ELSE 'temporary'
|
||||
END,
|
||||
expires_at = CASE
|
||||
WHEN (
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notice_documents AS d
|
||||
WHERE d.asset_id = file_assets.id
|
||||
) > 0 THEN NULL
|
||||
ELSE {expires_expr}
|
||||
END,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE parent_asset_id IS NOT NULL
|
||||
AND deleted_at IS NULL
|
||||
AND asset_role = 'derived_attachment'
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
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 upgrade() -> None:
|
||||
names = _index_names("file_assets")
|
||||
with op.batch_alter_table("file_assets", schema=None) as batch_op:
|
||||
if "ix_file_assets_parent_content_hash" in names:
|
||||
batch_op.drop_index("ix_file_assets_parent_content_hash")
|
||||
if "uq_file_assets_parent_content_hash" in names:
|
||||
batch_op.drop_index("uq_file_assets_parent_content_hash")
|
||||
|
||||
_dedupe_derived_file_assets()
|
||||
_recompute_derived_reference_counts()
|
||||
|
||||
# PostgreSQL:仅未删除派生资产唯一;SQLite 同步 sqlite_where
|
||||
names = _index_names("file_assets")
|
||||
if "uq_file_assets_parent_content_hash_alive" not in names:
|
||||
op.create_index(
|
||||
"uq_file_assets_parent_content_hash_alive",
|
||||
"file_assets",
|
||||
["parent_asset_id", "content_hash"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("deleted_at IS NULL AND parent_asset_id IS NOT NULL"),
|
||||
sqlite_where=sa.text("deleted_at IS NULL AND parent_asset_id IS NOT NULL"),
|
||||
)
|
||||
|
||||
with op.batch_alter_table("shipping_notice_documents", schema=None) as batch_op:
|
||||
batch_op.create_unique_constraint(
|
||||
"uq_shipping_notice_documents_id_intake",
|
||||
["id", "intake_id"],
|
||||
)
|
||||
|
||||
_drop_document_fk_on_candidates()
|
||||
with op.batch_alter_table("shipping_notice_candidates", schema=None) as batch_op:
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_notice_candidates_document_intake",
|
||||
"shipping_notice_documents",
|
||||
["primary_document_id", "intake_id"],
|
||||
["id", "intake_id"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("shipping_notice_candidates", schema=None) as batch_op:
|
||||
batch_op.drop_constraint(
|
||||
"fk_shipping_notice_candidates_document_intake",
|
||||
type_="foreignkey",
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
"fk_shipping_notice_candidates_primary_document_id",
|
||||
"shipping_notice_documents",
|
||||
["primary_document_id"],
|
||||
["id"],
|
||||
)
|
||||
|
||||
with op.batch_alter_table("shipping_notice_documents", schema=None) as batch_op:
|
||||
batch_op.drop_constraint("uq_shipping_notice_documents_id_intake", type_="unique")
|
||||
|
||||
names = _index_names("file_assets")
|
||||
if "uq_file_assets_parent_content_hash_alive" in names:
|
||||
op.drop_index("uq_file_assets_parent_content_hash_alive", table_name="file_assets")
|
||||
with op.batch_alter_table("file_assets", schema=None) as batch_op:
|
||||
batch_op.create_index(
|
||||
"ix_file_assets_parent_content_hash",
|
||||
["parent_asset_id", "content_hash"],
|
||||
unique=False,
|
||||
)
|
||||
@@ -0,0 +1,181 @@
|
||||
"""
|
||||
backend/migrations/versions/f5a6b7c8d9e0_file_asset_partial_unique_alive.py
|
||||
|
||||
用途:将派生资产全量唯一索引改为「未删除」部分唯一,并合并历史重复行。
|
||||
依赖:Alembic revision e4f5a6b7c8d9。
|
||||
逻辑:兼容已执行旧版 e4(创建 uq_file_assets_parent_content_hash)的环境;
|
||||
去重后按 Document 重算 reference_count;新装库若 e4 已建 partial index 则索引创建 no-op。
|
||||
downgrade 恢复非唯一普通索引,避免软删除同键冲突。
|
||||
|
||||
Revision ID: f5a6b7c8d9e0
|
||||
Revises: e4f5a6b7c8d9
|
||||
Create Date: 2026-07-14 19:30:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "f5a6b7c8d9e0"
|
||||
down_revision = "e4f5a6b7c8d9"
|
||||
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 _dedupe_derived_file_assets() -> None:
|
||||
"""合并同父同哈希活记录:保留最小 id,重指向 Document,软删其余。"""
|
||||
bind = op.get_bind()
|
||||
rows = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT parent_asset_id, content_hash
|
||||
FROM file_assets
|
||||
WHERE parent_asset_id IS NOT NULL
|
||||
AND deleted_at IS NULL
|
||||
AND content_hash IS NOT NULL
|
||||
GROUP BY parent_asset_id, content_hash
|
||||
HAVING COUNT(*) > 1
|
||||
"""
|
||||
)
|
||||
).fetchall()
|
||||
for parent_asset_id, content_hash in rows:
|
||||
ids = [
|
||||
int(r[0])
|
||||
for r in bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT id
|
||||
FROM file_assets
|
||||
WHERE parent_asset_id = :parent_id
|
||||
AND content_hash = :content_hash
|
||||
AND deleted_at IS NULL
|
||||
ORDER BY id ASC
|
||||
"""
|
||||
),
|
||||
{"parent_id": int(parent_asset_id), "content_hash": str(content_hash)},
|
||||
).fetchall()
|
||||
]
|
||||
if len(ids) < 2:
|
||||
continue
|
||||
keep_id = ids[0]
|
||||
for drop_id in ids[1:]:
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE shipping_notice_documents
|
||||
SET asset_id = :keep_id
|
||||
WHERE asset_id = :drop_id
|
||||
"""
|
||||
),
|
||||
{"keep_id": keep_id, "drop_id": drop_id},
|
||||
)
|
||||
# 转为 orphan_cleanup + expired(deleted_at 保持 NULL),交给 cleanup 删 MinIO
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE file_assets
|
||||
SET parent_asset_id = NULL,
|
||||
asset_role = 'orphan_cleanup',
|
||||
source_attachment_key = NULL,
|
||||
lifecycle_status = 'expired',
|
||||
reference_count = 0,
|
||||
expires_at = CURRENT_TIMESTAMP,
|
||||
deleted_at = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :drop_id
|
||||
"""
|
||||
),
|
||||
{"drop_id": drop_id},
|
||||
)
|
||||
|
||||
|
||||
def _recompute_derived_reference_counts() -> None:
|
||||
"""按 Document.asset_id 重算活派生资产引用数与 lifecycle/expires_at。"""
|
||||
bind = op.get_bind()
|
||||
dialect = str(getattr(bind.dialect, "name", "") or "")
|
||||
expires_expr = (
|
||||
"CURRENT_TIMESTAMP + INTERVAL '72 hours'"
|
||||
if dialect == "postgresql"
|
||||
else "datetime('now', '+72 hours')"
|
||||
)
|
||||
bind.execute(
|
||||
sa.text(
|
||||
f"""
|
||||
UPDATE file_assets
|
||||
SET
|
||||
reference_count = (
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notice_documents AS d
|
||||
WHERE d.asset_id = file_assets.id
|
||||
),
|
||||
lifecycle_status = CASE
|
||||
WHEN (
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notice_documents AS d
|
||||
WHERE d.asset_id = file_assets.id
|
||||
) > 0 THEN 'referenced'
|
||||
ELSE 'temporary'
|
||||
END,
|
||||
expires_at = CASE
|
||||
WHEN (
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notice_documents AS d
|
||||
WHERE d.asset_id = file_assets.id
|
||||
) > 0 THEN NULL
|
||||
ELSE {expires_expr}
|
||||
END,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE parent_asset_id IS NOT NULL
|
||||
AND deleted_at IS NULL
|
||||
AND asset_role = 'derived_attachment'
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
names = _index_names("file_assets")
|
||||
if "uq_file_assets_parent_content_hash" in names:
|
||||
op.drop_index("uq_file_assets_parent_content_hash", table_name="file_assets")
|
||||
|
||||
_dedupe_derived_file_assets()
|
||||
_recompute_derived_reference_counts()
|
||||
|
||||
names = _index_names("file_assets")
|
||||
if "uq_file_assets_parent_content_hash_alive" not in names:
|
||||
op.create_index(
|
||||
"uq_file_assets_parent_content_hash_alive",
|
||||
"file_assets",
|
||||
["parent_asset_id", "content_hash"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("deleted_at IS NULL AND parent_asset_id IS NOT NULL"),
|
||||
sqlite_where=sa.text("deleted_at IS NULL AND parent_asset_id IS NOT NULL"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""
|
||||
降级:去掉部分唯一索引,恢复非唯一普通索引。
|
||||
|
||||
核心逻辑:不恢复全量唯一——软删除与活记录可共享 (parent, hash),全量唯一会冲突。
|
||||
"""
|
||||
names = _index_names("file_assets")
|
||||
if "uq_file_assets_parent_content_hash_alive" in names:
|
||||
op.drop_index("uq_file_assets_parent_content_hash_alive", table_name="file_assets")
|
||||
if "uq_file_assets_parent_content_hash" in names:
|
||||
op.drop_index("uq_file_assets_parent_content_hash", table_name="file_assets")
|
||||
names = _index_names("file_assets")
|
||||
if "ix_file_assets_parent_content_hash" not in names:
|
||||
op.create_index(
|
||||
"ix_file_assets_parent_content_hash",
|
||||
"file_assets",
|
||||
["parent_asset_id", "content_hash"],
|
||||
unique=False,
|
||||
)
|
||||
Reference in new issue
Block a user