fix(catalog): 快照节点绑定物化条目以显示导入进度

This commit is contained in:
姚章浩
2026-09-15 16:50:15 +08:00
parent 40d0c511d6
commit 0ef41594c2
8 changed files with 208 additions and 16 deletions
@@ -88,6 +88,23 @@ WHERE source_connection_id=$1 AND node_id=$2 FOR UPDATE`, request.ConnectionID,
if err != nil {
return false, false, 0, false, fmt.Errorf("read catalog intent: %w", err)
}
if existingItemID == nil {
// The node itself may already carry an item binding from an earlier
// scanner generation (or a sibling intent); reuse it instead of
// materializing a duplicate item and refetching identical bytes.
var nodeItemID *string
lookup := tx.QueryRow(ctx, `SELECT node.source_item_id
FROM source_catalog_heads head JOIN source_catalog_nodes node
ON node.snapshot_id=head.snapshot_id AND node.node_type='file'
WHERE head.source_connection_id=$1 AND node.node_id=$2
AND node.source_item_id IS NOT NULL
ORDER BY head.activated_at DESC NULLS LAST LIMIT 1 FOR UPDATE OF node`, request.ConnectionID, file.NodeID).Scan(&nodeItemID)
if lookup == nil {
existingItemID = nodeItemID
} else if !errors.Is(lookup, pgx.ErrNoRows) {
return false, false, 0, false, fmt.Errorf("read catalog node item: %w", lookup)
}
}
itemID := ""
if existingItemID != nil {
@@ -114,6 +131,18 @@ VALUES($1,$2,$3,$4,$5,'file','',$7,'configured_scope','unchanged',0,'object_id',
}
itemID = newItemID
}
// Bind the snapshot nodes too (new or reused item): the browser's
// 已导入/已解析 counters join source_catalog_nodes.source_item_id; without
// this write every catalog-only source keeps showing "尚未导入" even after
// the document finished parsing. node_id is deterministic per path, so we
// bind across all snapshots of this connection, current and future heads.
if _, err := tx.Exec(ctx, `UPDATE source_catalog_nodes node SET source_item_id=$3
FROM source_catalog_snapshots snap
WHERE snap.source_connection_id=$1 AND node.snapshot_id=snap.id
AND node.node_type='file' AND node.node_id=$2 AND node.source_item_id IS NULL`,
request.ConnectionID, file.NodeID, itemID); err != nil {
return false, false, 0, false, fmt.Errorf("bind catalog node item: %w", err)
}
if request.Visibility == governance.VisibilityBlocked {
if _, err := tx.Exec(ctx, `UPDATE source_items SET policy_state='blocked',updated_at=NOW() WHERE id=$1`, itemID); err != nil {