fix(kbbinding): 生产保存重排待入库并按环境配对
build / build (push) Successful in 2m20s

- 生产页保存对内/对外库时,在同一事务内把还没上传、原本瞄准旧生产库的待入库项改指向新库,并在保存提示里回报迁移条数;执行已开始的项目标保持冻结。
- 重排改为「上一次生效目标 → 新目标」成对过滤:测试与生产共用 test_publications,测试侧保存不再带走瞄准生产库的待入库(验收附带发现)。
- 用例补生产重排、测试侧保存不误搬生产作业、生产保存不动测试作业三组断言。
This commit is contained in:
狐友 GXXP & LEN
2026-11-22 23:31:41 +08:00
committed by 姚章浩
parent 768ad53fe2
commit e892bfdd48
7 changed files with 201 additions and 59 deletions
+50 -27
View File
@@ -60,14 +60,33 @@ ON CONFLICT(id) DO UPDATE SET
return nil
}
// replanRetireDuplicatesSQL retires a pending publication whose twin for the
// new knowledge base already exists — the unique key is
// (source_item_version_id, test_kb_id), so the row cannot simply be moved.
// EffectiveKBIDs is the pair of knowledge base ids consumers actually route to:
// the UI binding where it is set, the deployment default otherwise. A save must
// name both the pair it replaces and the pair it establishes, so re-planning
// only moves work that was aimed at THIS environment's previous target.
type EffectiveKBIDs struct {
Internal string
External string
}
// RoutingBindingSave is one binding change: the ids to persist plus the
// effective routing before and after it.
type RoutingBindingSave struct {
InternalID string
ExternalID string
Previous EffectiveKBIDs
Effective EffectiveKBIDs
UpdatedBy string
}
// replanRetireDuplicatesSQL retires a pending publication aimed at the previous
// target whose twin for the new knowledge base already exists — the unique key
// is (source_item_version_id, test_kb_id), so the row cannot simply be moved.
const replanRetireDuplicatesSQL = `
UPDATE test_publications stale
SET superseded_at = $3, updated_at = $3
SET superseded_at = $4, updated_at = $4
WHERE stale.superseded_at IS NULL
AND stale.test_kb_id <> $1
AND stale.test_kb_id = $1
AND stale.processing_state = 'planned'
AND stale.ingest_state = 'pending'
AND stale.knowledge_id = ''
@@ -77,7 +96,7 @@ WHERE stale.superseded_at IS NULL
AND EXISTS (
SELECT 1 FROM test_publications twin
WHERE twin.source_item_version_id = stale.source_item_version_id
AND twin.test_kb_id = $1
AND twin.test_kb_id = $3
AND twin.superseded_at IS NULL)
`
@@ -86,9 +105,9 @@ WHERE stale.superseded_at IS NULL
// already in flight keeps its frozen target and stays visible in 待入库.
const replanMovePendingSQL = `
UPDATE test_publications pending
SET test_kb_id = $1, test_instance_url = '', test_instance_tenant = 0, updated_at = $3
SET test_kb_id = $3, test_instance_url = '', test_instance_tenant = 0, updated_at = $4
WHERE pending.superseded_at IS NULL
AND pending.test_kb_id <> $1
AND pending.test_kb_id = $1
AND pending.processing_state = 'planned'
AND pending.ingest_state = 'pending'
AND pending.knowledge_id = ''
@@ -98,20 +117,20 @@ WHERE pending.superseded_at IS NULL
AND NOT EXISTS (
SELECT 1 FROM test_publications twin
WHERE twin.source_item_version_id = pending.source_item_version_id
AND twin.test_kb_id = $1
AND twin.test_kb_id = $3
AND twin.superseded_at IS NULL)
`
// SaveRoutingKBBindingsWithReplan saves the binding and, in the SAME
// transaction, re-points 待入库 items that were still waiting for the old
// SaveRoutingKBBindingsWithReplan saves the test-side binding and, in the SAME
// transaction, re-points 待入库 items that were still waiting for the previous
// knowledge base. Without this, changing the binding left every pending
// publication aimed at the previous (possibly deleted) KB, and re-marking the
// file created a SECOND publication instead of moving the first.
//
// effectiveInternal/effectiveExternal are the ids the platform actually routes
// to (the saved binding, or the deployment default when a side is unbound);
// an empty effective id means that scope cannot be routed and is skipped.
func (s *Store) SaveRoutingKBBindingsWithReplan(ctx context.Context, internalID, externalID, effectiveInternal, effectiveExternal, updatedBy string, now time.Time) (RoutingBindingReplanResult, error) {
// save.Previous is the effective pair before the change (stored binding, or the
// deployment default where a side was unbound); an empty or unchanged side is
// not guessed at.
func (s *Store) SaveRoutingKBBindingsWithReplan(ctx context.Context, save RoutingBindingSave, now time.Time) (RoutingBindingReplanResult, error) {
var result RoutingBindingReplanResult
if now.IsZero() {
return result, errors.New("binding replan requires a timestamp")
@@ -121,10 +140,10 @@ func (s *Store) SaveRoutingKBBindingsWithReplan(ctx context.Context, internalID,
return result, fmt.Errorf("begin routing binding save: %w", err)
}
defer func() { _ = tx.Rollback(ctx) }()
if err := SaveRoutingKBBindingsTx(ctx, tx, internalID, externalID, updatedBy, now); err != nil {
if err := SaveRoutingKBBindingsTx(ctx, tx, save.InternalID, save.ExternalID, save.UpdatedBy, now); err != nil {
return result, err
}
result, err = ReplanPendingPublicationsTx(ctx, tx, effectiveInternal, effectiveExternal, now)
result, err = ReplanPendingPublicationsTx(ctx, tx, save.Previous, save.Effective, now)
if err != nil {
return RoutingBindingReplanResult{}, err
}
@@ -150,23 +169,27 @@ ON CONFLICT(id) DO UPDATE SET
return nil
}
// ReplanPendingPublicationsTx re-points untouched 待入库 rows at the new targets
// inside the caller's transaction. It only moves rows that were never uploaded
// anywhere; anything already in flight keeps its frozen target.
func ReplanPendingPublicationsTx(ctx context.Context, tx pgx.Tx, effectiveInternal, effectiveExternal string, now time.Time) (RoutingBindingReplanResult, error) {
// ReplanPendingPublicationsTx re-points untouched 待入库 rows from the previous
// effective targets to the new ones inside the caller's transaction. It only
// moves rows that were never uploaded anywhere AND were aimed at the previous
// target: that target check is what keeps one environment's save from carrying
// off pending work belonging to the other environment (both envs share the
// test_publications lane until an item is executed). Anything already in flight
// keeps its frozen target.
func ReplanPendingPublicationsTx(ctx context.Context, tx pgx.Tx, previous, effective EffectiveKBIDs, now time.Time) (RoutingBindingReplanResult, error) {
var result RoutingBindingReplanResult
for _, scope := range []struct{ name, kbID string }{
{"internal", effectiveInternal},
{"external", effectiveExternal},
for _, scope := range []struct{ name, from, to string }{
{"internal", previous.Internal, effective.Internal},
{"external", previous.External, effective.External},
} {
if scope.kbID == "" {
if scope.from == "" || scope.to == "" || scope.from == scope.to {
continue
}
retired, err := tx.Exec(ctx, replanRetireDuplicatesSQL, scope.kbID, scope.name, now)
retired, err := tx.Exec(ctx, replanRetireDuplicatesSQL, scope.from, scope.name, scope.to, now)
if err != nil {
return result, fmt.Errorf("retire duplicate pending publications: %w", err)
}
moved, err := tx.Exec(ctx, replanMovePendingSQL, scope.kbID, scope.name, now)
moved, err := tx.Exec(ctx, replanMovePendingSQL, scope.from, scope.name, scope.to, now)
if err != nil {
return result, fmt.Errorf("replan pending publications: %w", err)
}
@@ -33,16 +33,19 @@ INSERT INTO source_items(id,source_connection_id,stable_external_id,canonical_ur
('item-in','conn-replan','n-in',repeat('b',64),'docs/in.docx','in.docx','document','locator:r1','internal','configured_scope'),
('item-ext','conn-replan','n-ext',repeat('b',64),'docs/ext.docx','ext.docx','document','locator:r2','external','configured_scope'),
('item-sent','conn-replan','n-sent',repeat('b',64),'docs/sent.docx','sent.docx','document','locator:r3','internal','configured_scope'),
('item-prod','conn-replan','n-prod',repeat('b',64),'docs/prod.docx','prod.docx','document','locator:r5','internal','configured_scope'),
('item-dup','conn-replan','n-dup',repeat('b',64),'docs/dup.docx','dup.docx','document','locator:r4','internal','configured_scope');
INSERT INTO source_item_versions(id,source_item_id,revision,content_object_id,content_state) VALUES
('v-in','item-in','locator:r1',NULL,'unknown'),
('v-ext','item-ext','locator:r2',NULL,'unknown'),
('v-sent','item-sent','locator:r3',NULL,'unknown'),
('v-prod','item-prod','locator:r5',NULL,'unknown'),
('v-dup','item-dup','locator:r4',NULL,'unknown');
INSERT INTO test_publications(id,source_item_version_id,test_kb_id,processing_state,ingest_state,target_scope) VALUES
('pub-in','v-in','kb-old-internal','planned','pending','internal'),
('pub-ext','v-ext','kb-old-external','planned','pending','external'),
('pub-sent','v-sent','kb-old-internal','pending','uploaded','internal'),
('pub-prod','v-prod','p-internal','planned','pending','internal'),
('pub-dup-old','v-dup','kb-old-internal','planned','pending','internal'),
('pub-dup-new','v-dup','kb-new-internal','planned','pending','internal');
INSERT INTO jobs(id,type,payload,state,idempotency_key) VALUES
@@ -53,12 +56,19 @@ INSERT INTO jobs(id,type,payload,state,idempotency_key) VALUES
store := NewStore(pool)
now := time.Date(2026, time.October, 8, 7, 0, 0, 0, time.UTC)
result, err := store.SaveRoutingKBBindingsWithReplan(ctx, "kb-new-internal", "kb-new-external", "kb-new-internal", "kb-new-external", "tester", now)
result, err := store.SaveRoutingKBBindingsWithReplan(ctx, RoutingBindingSave{
InternalID: "kb-new-internal", ExternalID: "kb-new-external",
Previous: EffectiveKBIDs{Internal: "kb-old-internal", External: "kb-old-external"},
Effective: EffectiveKBIDs{Internal: "kb-new-internal", External: "kb-new-external"},
UpdatedBy: "tester",
}, now)
if err != nil {
t.Fatalf("SaveRoutingKBBindingsWithReplan() error = %v", err)
}
// pub-in and pub-ext move; pub-sent already has an upload job (untouched);
// pub-dup-old already has a twin for the new KB so it is retired instead.
// pub-dup-old already has a twin for the new KB so it is retired instead;
// pub-prod aims at the production binding and must not be carried off by a
// test-side save.
if result.Replanned != 2 || result.RetiredDuplicates != 1 {
t.Fatalf("replan result = %#v, want 2 moved / 1 retired", result)
}
@@ -95,9 +105,17 @@ INSERT INTO jobs(id,type,payload,state,idempotency_key) VALUES
if states["pub-dup-old"].kb != "kb-old-internal" || !states["pub-dup-old"].superseded {
t.Fatalf("the duplicate stale row must be retired: %#v", states["pub-dup-old"])
}
if states["pub-prod"].kb != "p-internal" || states["pub-prod"].superseded {
t.Fatalf("a pending item aimed at another environment's target must stay put: %#v", states["pub-prod"])
}
// Re-saving the same binding is a no-op: nothing left to move or retire.
again, err := store.SaveRoutingKBBindingsWithReplan(ctx, "kb-new-internal", "kb-new-external", "kb-new-internal", "kb-new-external", "tester", now)
again, err := store.SaveRoutingKBBindingsWithReplan(ctx, RoutingBindingSave{
InternalID: "kb-new-internal", ExternalID: "kb-new-external",
Previous: EffectiveKBIDs{Internal: "kb-new-internal", External: "kb-new-external"},
Effective: EffectiveKBIDs{Internal: "kb-new-internal", External: "kb-new-external"},
UpdatedBy: "tester",
}, now)
if err != nil {
t.Fatal(err)
}