feat(release): 发布目标知识库可选(源库 → 目标库映射)

- 迁移 000044:production_kb_bindings + release_item_executions.target_knowledge_base_id
- 系统设置新增「生产知识库绑定(对内/对外)」:从生产 WeKnora 实际列表选库,
  保存时真校验(不存在拒收;生产端不可达只提示并如实回报),仅管理员可改
- 执行计划按绑定解析目标库并冻结进 release_item_executions;空目标直接拒收;
  已发布过的条目保留原目标(重试/回滚回到同一处)
- DocumentBundle.RemapKnowledgeBase:knowledges/chunks/knowledge_tags/wiki_pages/
  wiki_folders 的 knowledge_base_id 与 ES 文档字段改写为目标库,重算表/文档摘要并重新封装
- 契约比对改为「声明的映射对」(KnowledgeBaseMapping),其余字段继续 fail-closed
- 契约检查器 / 目标身份 / 验证器全部走映射后的目标库;对象通道无需改写
- Compose 拆分:数据面接线 compose.release-sim.yaml + 两道门禁 compose.release-gates.yaml
- 测试:RemapKnowledgeBase 全通道/无操作/fail-closed、映射契约、绑定接口、RBAC、真实 PG 集成
This commit is contained in:
姚章浩
2026-10-08 15:41:13 +08:00
parent cddc4c90d1
commit 6440e3f679
40 changed files with 1823 additions and 92 deletions
@@ -132,4 +132,4 @@ VALUES('conn-fetch','node-1','internal','tester','技术文档/环境部署/关
} else if !errors.Is(err, ErrJobFailureNotRequeueable) {
t.Fatalf("unexpected error: %v", err)
}
}
}
@@ -0,0 +1,2 @@
ALTER TABLE release_item_executions DROP COLUMN IF EXISTS target_knowledge_base_id;
DROP TABLE IF EXISTS production_kb_bindings;
@@ -0,0 +1,22 @@
-- Cross-environment release needs an explicit 源库 → 目标库 mapping. Until now
-- the production KB was forced to carry the same id as the tested KB, which
-- meant only same-id "mirror" bases could ever be published to. One row stores
-- which (production-side) knowledge base receives 对内 and which receives 对外
-- releases; an empty side keeps the historical same-id semantics (and fails
-- closed later if that id does not exist on the target).
CREATE TABLE production_kb_bindings (
id TEXT PRIMARY KEY DEFAULT 'active' CHECK (id = 'active'),
internal_kb_id VARCHAR(64) NOT NULL DEFAULT '',
external_kb_id VARCHAR(64) NOT NULL DEFAULT '',
updated_by VARCHAR(64) NOT NULL DEFAULT '',
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
INSERT INTO production_kb_bindings(id,internal_kb_id,external_kb_id,updated_by)
VALUES('active','','','system:migration');
-- The resolved target base is frozen per release item when the execution
-- starts, so an audit shows exactly where each knowledge went and a retry
-- reuses the same target instead of re-resolving a binding that may have moved.
ALTER TABLE release_item_executions
ADD COLUMN target_knowledge_base_id VARCHAR(64) NOT NULL DEFAULT '';
@@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
if err != nil {
t.Fatalf("loadMigrations() error = %v", err)
}
if len(migrations) != 43 {
t.Fatalf("migration count = %d, want 43", len(migrations))
if len(migrations) != 44 {
t.Fatalf("migration count = %d, want 44", len(migrations))
}
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
t.Fatalf("migration 1 = %#v", migrations[0])
@@ -0,0 +1,77 @@
package postgres
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5"
)
// ProductionBinding is the UI-managed 源库 → 目标库 mapping row. The left side
// of the release (the tested knowledge base) is fixed by the publication; the
// right side is the production-side base this table chooses. An empty side
// keeps the historical same-id semantics.
type ProductionBinding struct {
InternalKBID string
ExternalKBID string
UpdatedBy string
}
// LoadProductionKBBindings reads the single active mapping row. A missing row
// reads back as unbound rather than an error.
func (s *Store) LoadProductionKBBindings(ctx context.Context) (string, string, string, error) {
var binding ProductionBinding
err := s.db.QueryRow(ctx, `SELECT internal_kb_id,external_kb_id,updated_by FROM production_kb_bindings WHERE id='active'`).
Scan(&binding.InternalKBID, &binding.ExternalKBID, &binding.UpdatedBy)
if errors.Is(err, pgx.ErrNoRows) {
return "", "", "", nil
}
if err != nil {
return "", "", "", errors.New("load production kb bindings")
}
return binding.InternalKBID, binding.ExternalKBID, binding.UpdatedBy, nil
}
// SaveProductionKBBindings upserts the mapping row. The knowledge base ids must
// already have been validated against the live production WeKnora by the caller.
func (s *Store) SaveProductionKBBindings(ctx context.Context, internalID, externalID, updatedBy string, now time.Time) error {
if now.IsZero() {
return errors.New("production binding save requires a timestamp")
}
if _, err := s.db.Exec(ctx, `
INSERT INTO production_kb_bindings(id,internal_kb_id,external_kb_id,updated_by,updated_at)
VALUES('active',$1,$2,$3,$4)
ON CONFLICT(id) DO UPDATE SET
internal_kb_id=EXCLUDED.internal_kb_id,
external_kb_id=EXCLUDED.external_kb_id,
updated_by=EXCLUDED.updated_by,
updated_at=EXCLUDED.updated_at`, internalID, externalID, updatedBy, now); err != nil {
return errors.New("save production kb bindings")
}
return nil
}
// ResolveProductionTargetKB maps a publication scope to the production base the
// release should write into. An unbound scope falls back to the tested base id,
// which is the historical same-id behaviour (and fails closed at the contract
// check when no such base exists on the target).
func (s *Store) ResolveProductionTargetKB(ctx context.Context, scope, testKBID string) (string, error) {
internal, external, _, err := s.LoadProductionKBBindings(ctx)
if err != nil {
return "", err
}
bound := ""
switch scope {
case "internal":
bound = internal
case "external":
bound = external
default:
return "", errors.New("unknown release scope")
}
if bound == "" {
return testKBID, nil
}
return bound, nil
}
@@ -0,0 +1,72 @@
package postgres
import (
"context"
"os"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// The 源库 → 目标库 mapping has to survive a round trip and, when a scope is
// unbound, fall back to the tested base (the historical same-id semantics).
func TestProductionKBBindingsResolveAndFallBack(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err = ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
store := NewStore(pool)
now := time.Date(2026, time.October, 8, 9, 0, 0, 0, time.UTC)
internal, external, _, err := store.LoadProductionKBBindings(ctx)
if err != nil {
t.Fatal(err)
}
if internal != "" || external != "" {
t.Fatalf("fresh mapping = %q/%q, want unbound", internal, external)
}
if target, err := store.ResolveProductionTargetKB(ctx, "internal", "test-kb"); err != nil || target != "test-kb" {
t.Fatalf("unbound resolve = %q/%v, want the tested base", target, err)
}
if err := store.SaveProductionKBBindings(ctx, "prod-internal", "prod-external", "admin", now); err != nil {
t.Fatal(err)
}
if target, err := store.ResolveProductionTargetKB(ctx, "internal", "test-kb"); err != nil || target != "prod-internal" {
t.Fatalf("internal resolve = %q/%v", target, err)
}
if target, err := store.ResolveProductionTargetKB(ctx, "external", "test-kb"); err != nil || target != "prod-external" {
t.Fatalf("external resolve = %q/%v", target, err)
}
if _, err := store.ResolveProductionTargetKB(ctx, "sideways", "test-kb"); err == nil {
t.Fatal("an unknown scope must not resolve to anything")
}
// Unbinding one side returns that side to the same-id semantics only.
if err := store.SaveProductionKBBindings(ctx, "", "prod-external", "admin", now); err != nil {
t.Fatal(err)
}
if target, err := store.ResolveProductionTargetKB(ctx, "internal", "test-kb"); err != nil || target != "test-kb" {
t.Fatalf("unbound side = %q/%v", target, err)
}
if target, _ := store.ResolveProductionTargetKB(ctx, "external", "test-kb"); target != "prod-external" {
t.Fatalf("bound side regressed = %q", target)
}
_, _, saver, err := store.LoadProductionKBBindings(ctx)
if err != nil || saver != "admin" {
t.Fatalf("updated_by = %q (%v)", saver, err)
}
if err := store.SaveProductionKBBindings(ctx, "prod-internal", "", "admin", time.Time{}); err == nil {
t.Fatal("a save without a timestamp must be rejected")
}
}
@@ -63,14 +63,45 @@ execution_gate AS (
AND (gate_version.revision = '' OR gate_publication.parser_generation = '' OR gate_publication.index_generation = '')
)
RETURNING execution.id, execution.release_batch_id, execution.operation, execution.prior_execution_id
), seeded AS (
INSERT INTO release_item_executions (
id, release_execution_id, publication_id, action, state, created_at, updated_at
)
SELECT 'ri-' || SUBSTR(MD5(started.id || ':' || item.publication_id), 1, 32),
started.id, item.publication_id, item.action, 'pending', $4::TIMESTAMPTZ, $4::TIMESTAMPTZ
), target_base AS (
-- 源库 → 目标库 mapping, frozen per item. A publication that was already
-- verified keeps the base it went to (retry / rollback return to the same
-- place); otherwise the UI binding decides, and an unbound scope keeps the
-- historical same-id semantics so existing routings keep working.
SELECT item.publication_id,
COALESCE(
NULLIF(prior_item.target_knowledge_base_id, ''),
NULLIF(CASE publication.target_scope
WHEN 'internal' THEN binding.internal_kb_id
WHEN 'external' THEN binding.external_kb_id
END, ''),
publication.test_kb_id) AS target_kb_id
FROM started
JOIN release_batch_items AS item ON item.release_batch_id = started.release_batch_id
JOIN test_publications AS publication ON publication.id = item.publication_id
LEFT JOIN production_kb_bindings AS binding ON binding.id = 'active'
LEFT JOIN LATERAL (
SELECT candidate.target_knowledge_base_id
FROM release_item_executions AS candidate
JOIN release_executions AS candidate_execution ON candidate_execution.id = candidate.release_execution_id
WHERE candidate.publication_id = item.publication_id
AND candidate.target_knowledge_base_id <> ''
AND candidate.state = 'verified'
AND candidate_execution.operation = 'publish'
AND candidate_execution.state IN ('completed', 'failed')
ORDER BY candidate.completed_at DESC NULLS LAST, candidate.id DESC
LIMIT 1
) AS prior_item ON TRUE
), seeded AS (
INSERT INTO release_item_executions (
id, release_execution_id, publication_id, action, state, created_at, updated_at, target_knowledge_base_id
)
SELECT 'ri-' || SUBSTR(MD5(started.id || ':' || item.publication_id), 1, 32),
started.id, item.publication_id, item.action, 'pending', $4::TIMESTAMPTZ, $4::TIMESTAMPTZ,
COALESCE(target_base.target_kb_id, '')
FROM started
JOIN release_batch_items AS item ON item.release_batch_id = started.release_batch_id
LEFT JOIN target_base ON target_base.publication_id = item.publication_id
WHERE started.operation = 'publish' OR EXISTS (
SELECT 1
FROM release_item_executions AS prior_seed
@@ -97,6 +128,7 @@ execution_gate AS (
publication.parser_generation || '@' || publication.index_generation AS parse_revision,
item.review_id AS release_revision,
publication.test_kb_id,
COALESCE(target_base.target_kb_id, '') AS target_kb_id,
publication.knowledge_id,
item.decided_visibility,
COALESCE(previous.production_revision, '') AS previous_production_revision
@@ -105,6 +137,7 @@ execution_gate AS (
JOIN active_items ON active_items.publication_id = item.publication_id
JOIN test_publications AS publication ON publication.id = item.publication_id
LEFT JOIN target_base ON target_base.publication_id = item.publication_id
JOIN source_item_versions AS current_version ON current_version.id = item.source_item_version_id
LEFT JOIN LATERAL (
SELECT CASE
@@ -146,6 +179,7 @@ SELECT COUNT(DISTINCT started.id)::INTEGER,
'parse_revision', plan.parse_revision,
'release_revision', plan.release_revision,
'test_knowledge_base_id', plan.test_kb_id,
'target_knowledge_base_id', plan.target_kb_id,
'knowledge_id', plan.knowledge_id,
'decided_visibility', plan.decided_visibility,
'previous_production_revision', plan.previous_production_revision
@@ -271,7 +305,7 @@ func (s *Store) StartExecution(ctx context.Context, request releaseworkflow.Star
}
for _, item := range items {
if !validShortID(item.PublicationID) || !validShortID(item.SourceItemVersionID) ||
!validRemoteIdentifier(item.TestKnowledgeBaseID) || !validRemoteIdentifier(item.KnowledgeID) ||
!validRemoteIdentifier(item.TestKnowledgeBaseID) || !validRemoteIdentifier(item.TargetKnowledgeBaseID) || !validRemoteIdentifier(item.KnowledgeID) ||
(item.DecidedVisibility != governance.VisibilityInternal && item.DecidedVisibility != governance.VisibilityExternal) ||
len(item.PreviousProductionRevision) > 128 {
return nil, errors.New("invalid release execution plan")
@@ -13,7 +13,7 @@ import (
)
func TestStartReleaseExecutionSeedsItemsAndReturnsOnlyPendingPlan(t *testing.T) {
payload := `[{"release_execution_id":"rx-safe","publication_id":"tp-safe","action":"delete","source_item_version_id":"sv-safe","source_revision":"source-v1","parse_revision":"parser-v1@index-v1","release_revision":"review-v1","test_knowledge_base_id":"kb-safe","knowledge_id":"knowledge-safe","decided_visibility":"internal","previous_production_revision":"prod-old"}]`
payload := `[{"release_execution_id":"rx-safe","publication_id":"tp-safe","action":"delete","source_item_version_id":"sv-safe","source_revision":"source-v1","parse_revision":"parser-v1@index-v1","release_revision":"review-v1","test_knowledge_base_id":"kb-safe","target_knowledge_base_id":"prod-kb","knowledge_id":"knowledge-safe","decided_visibility":"internal","previous_production_revision":"prod-old"}]`
db := &recordingDB{row: startExecutionRow{executionCount: 1, payload: []byte(payload)}}
items, err := NewStore(db).StartExecution(context.Background(), releaseworkflow.StartExecutionRequest{
ExecutionID: "rx-safe", JobID: "job-safe", Operation: "publish", Now: time.Now(),
@@ -24,6 +24,9 @@ func TestStartReleaseExecutionSeedsItemsAndReturnsOnlyPendingPlan(t *testing.T)
if len(items) != 1 || items[0].PublicationID != "tp-safe" || items[0].Action != "delete" || items[0].KnowledgeID != "knowledge-safe" || items[0].PreviousProductionRevision != "prod-old" {
t.Fatalf("items=%#v", items)
}
if items[0].TargetBase() != "prod-kb" {
t.Fatalf("target base=%q, want the mapped 源库 → 目标库 target", items[0].TargetBase())
}
sql := strings.ToLower(db.queryRowSQL)
for _, required := range []string{
"update release_executions", "then 'rolling_back' else 'running' end", "job_id = $2", "operation = $3",
@@ -34,6 +37,8 @@ func TestStartReleaseExecutionSeedsItemsAndReturnsOnlyPendingPlan(t *testing.T)
"started.operation = 'publish' or exists", "prior_seed.state = 'verified'",
"prior_execution.state in ('completed', 'failed')",
"release_delete_ready", "item.action = 'delete'", "execution_gate.valid_items = execution_gate.item_count",
// the 源库 → 目标库 mapping is resolved into the plan and frozen per item
"production_kb_bindings", "target_knowledge_base_id", "target_base.target_kb_id",
} {
if !strings.Contains(sql, required) {
t.Fatalf("start execution SQL missing %q", required)
@@ -41,6 +46,18 @@ func TestStartReleaseExecutionSeedsItemsAndReturnsOnlyPendingPlan(t *testing.T)
}
}
// An empty resolved target must be rejected rather than silently published to
// an unknown base.
func TestStartReleaseExecutionRejectsEmptyTargetBase(t *testing.T) {
payload := `[{"release_execution_id":"rx-safe","publication_id":"tp-safe","action":"delete","source_item_version_id":"sv-safe","source_revision":"source-v1","parse_revision":"parser-v1@index-v1","release_revision":"review-v1","test_knowledge_base_id":"kb-safe","target_knowledge_base_id":"","knowledge_id":"knowledge-safe","decided_visibility":"internal"}]`
_, err := NewStore(&recordingDB{row: startExecutionRow{executionCount: 1, payload: []byte(payload)}}).StartExecution(context.Background(), releaseworkflow.StartExecutionRequest{
ExecutionID: "rx-safe", JobID: "job-safe", Operation: "publish", Now: time.Now(),
})
if err == nil {
t.Fatal("an unmapped, empty target base must fail closed")
}
}
func TestStartReleaseExecutionRejectsUnknownOrMismatchedExecution(t *testing.T) {
_, err := NewStore(&recordingDB{row: startExecutionRow{executionCount: 0, payload: []byte(`[]`)}}).StartExecution(context.Background(), releaseworkflow.StartExecutionRequest{
ExecutionID: "rx-safe", JobID: "job-safe", Operation: "publish", Now: time.Now(),
@@ -3,6 +3,8 @@ package postgres
import (
"context"
"errors"
"fmt"
"time"
"github.com/jackc/pgx/v5"
)
@@ -30,6 +32,17 @@ func (s *Store) LoadRoutingKBBindings(ctx context.Context) (string, string, stri
return binding.InternalKBID, binding.ExternalKBID, binding.UpdatedBy, nil
}
// RoutingBindingReplanResult reports what a binding change did to publications
// that were still waiting in 待入库.
type RoutingBindingReplanResult struct {
// Replanned counts untouched pending publications that were re-pointed at
// the newly bound knowledge base for their scope.
Replanned int
// RetiredDuplicates counts pending publications that already had a twin for
// the new knowledge base, so the stale row was superseded instead of moved.
RetiredDuplicates int
}
// SaveRoutingKBBindings upserts the binding row. Each side is either empty
// (unbind, fall back to env) or a knowledge base id validated by the caller.
func (s *Store) SaveRoutingKBBindings(ctx context.Context, internalID, externalID, updatedBy string) error {
@@ -46,3 +59,98 @@ 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.
const replanRetireDuplicatesSQL = `
UPDATE test_publications stale
SET superseded_at = $3, updated_at = $3
WHERE stale.superseded_at IS NULL
AND stale.test_kb_id <> $1
AND stale.processing_state = 'planned'
AND stale.ingest_state = 'pending'
AND stale.knowledge_id = ''
AND stale.retrieval_state = 'unpublished'
AND stale.target_scope = $2
AND NOT EXISTS (SELECT 1 FROM jobs job WHERE job.type = 'test.upload' AND job.payload->>'publication_id' = stale.id)
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.superseded_at IS NULL)
`
// replanMovePendingSQL re-points the untouched remainder at the new knowledge
// base. Only rows that have never been uploaded anywhere are eligible: anything
// already in flight keeps its frozen target and stays visible in 待入库.
const replanMovePendingSQL = `
UPDATE test_publications pending
SET test_kb_id = $1, updated_at = $3
WHERE pending.superseded_at IS NULL
AND pending.test_kb_id <> $1
AND pending.processing_state = 'planned'
AND pending.ingest_state = 'pending'
AND pending.knowledge_id = ''
AND pending.retrieval_state = 'unpublished'
AND pending.target_scope = $2
AND NOT EXISTS (SELECT 1 FROM jobs job WHERE job.type = 'test.upload' AND job.payload->>'publication_id' = pending.id)
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.superseded_at IS NULL)
`
// SaveRoutingKBBindingsWithReplan saves the binding and, in the SAME
// transaction, re-points 待入库 items that were still waiting for the old
// 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) {
var result RoutingBindingReplanResult
if now.IsZero() {
return result, errors.New("binding replan requires a timestamp")
}
tx, err := s.db.Begin(ctx)
if err != nil {
return result, fmt.Errorf("begin routing binding save: %w", err)
}
defer func() { _ = tx.Rollback(ctx) }()
if _, err := tx.Exec(ctx, `
INSERT INTO routing_kb_bindings(id,internal_kb_id,external_kb_id,updated_by,updated_at)
VALUES('active',$1,$2,$3,$4)
ON CONFLICT(id) DO UPDATE SET
internal_kb_id=EXCLUDED.internal_kb_id,
external_kb_id=EXCLUDED.external_kb_id,
updated_by=EXCLUDED.updated_by,
updated_at=EXCLUDED.updated_at`, internalID, externalID, updatedBy, now); err != nil {
return result, errors.New("save routing kb bindings")
}
for _, scope := range []struct{ name, kbID string }{
{"internal", effectiveInternal},
{"external", effectiveExternal},
} {
if scope.kbID == "" {
continue
}
retired, err := tx.Exec(ctx, replanRetireDuplicatesSQL, scope.kbID, scope.name, 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)
if err != nil {
return result, fmt.Errorf("replan pending publications: %w", err)
}
result.RetiredDuplicates += int(retired.RowsAffected())
result.Replanned += int(moved.RowsAffected())
}
if err := tx.Commit(ctx); err != nil {
return result, fmt.Errorf("commit routing binding save: %w", err)
}
return result, nil
}
@@ -0,0 +1,115 @@
package postgres
import (
"context"
"os"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// Changing the knowledge-base binding used to re-route only FUTURE annotations:
// every publication already sitting in 待入库 kept its frozen test_kb_id, so a
// re-pointed binding left pending items aimed at the previous (deleted) KB, and
// re-marking the file created a SECOND publication instead of moving the first.
func TestSaveRoutingKBBindingsReplansPendingPublications(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err = ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
if _, err = pool.Exec(ctx, `
INSERT INTO source_connections(id,source_type,name,enabled) VALUES('conn-replan','manual','Replan',true);
INSERT INTO source_items(id,source_connection_id,stable_external_id,canonical_uri_hash,normalized_path,title,item_type,revision,visibility,visibility_origin) VALUES
('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-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-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-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
('job-sent','test.upload','{"publication_id":"pub-sent"}'::jsonb,'completed','test-start:pub-sent');
`); err != nil {
t.Fatal(err)
}
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)
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.
if result.Replanned != 2 || result.RetiredDuplicates != 1 {
t.Fatalf("replan result = %#v, want 2 moved / 1 retired", result)
}
states := map[string]struct {
kb string
superseded bool
}{}
rows, err := pool.Query(ctx, `SELECT id, test_kb_id, superseded_at IS NOT NULL FROM test_publications`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id, kb string
var superseded bool
if err := rows.Scan(&id, &kb, &superseded); err != nil {
t.Fatal(err)
}
states[id] = struct {
kb string
superseded bool
}{kb, superseded}
}
for id, want := range map[string]string{
"pub-in": "kb-new-internal", "pub-ext": "kb-new-external", "pub-dup-new": "kb-new-internal",
} {
if states[id].kb != want || states[id].superseded {
t.Fatalf("%s = %#v, want kb %s and not superseded", id, states[id], want)
}
}
if states["pub-sent"].kb != "kb-old-internal" || states["pub-sent"].superseded {
t.Fatalf("an already-uploaded publication must keep its target: %#v", states["pub-sent"])
}
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"])
}
// 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)
if err != nil {
t.Fatal(err)
}
if again.Replanned != 0 || again.RetiredDuplicates != 0 {
t.Fatalf("replay must be a no-op, got %#v", again)
}
var internal, external, updatedBy string
if err = pool.QueryRow(ctx, `SELECT internal_kb_id, external_kb_id, updated_by FROM routing_kb_bindings WHERE id='active'`).
Scan(&internal, &external, &updatedBy); err != nil {
t.Fatal(err)
}
if internal != "kb-new-internal" || external != "kb-new-external" || updatedBy != "tester" {
t.Fatalf("binding row = %s / %s / %s", internal, external, updatedBy)
}
}