1f5bf72aec
网页可能在实例 A 上列库并保存生产绑定,而文档发布执行器实际连实例 B。 克隆实例可以有相同知识库 ID、名称甚至租户,单靠 ID 无法证明写的是同一个 目标。现在绑定保存会冻结候选实例地址/租户,发布计划把身份冻结到每个项, 执行器在 Contract/Prepare/Activate/Restore/Compensate 前核对;不一致以 release_contract_mismatch 非重试失败关闭,不写目标。整库发布目标不变。 测试入库对象在上传前记录实际实例地址/租户;轮询、验收、移除先核对当前 连接,改绑实例或租户后旧对象不会被克隆实例接管。旧行没有身份时失败关闭。 「知识库绑定」保存要求对内/对外各选一个库,不再把显式未绑定回落到部署 默认 ID,避免换绑实例后带过去旧对象的默认库。 验证:隔离 PostgreSQL 覆盖绑定身份落库与发布计划冻结;v071 用同 ID/同租户 的 A/B 实例做正反例;四个测试 handler 的实例错配负例;前端 607 项通过。
69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"knowledge-governance/internal/governance"
|
|
)
|
|
|
|
type PublicationForRemoval struct {
|
|
ID string
|
|
TestKBID string
|
|
KnowledgeID string
|
|
TestInstanceURL string
|
|
TestInstanceTenant uint64
|
|
RetrievalState governance.RetrievalState
|
|
}
|
|
|
|
func (s *Store) LoadPublicationForRemoval(ctx context.Context, publicationID string) (PublicationForRemoval, error) {
|
|
var candidate PublicationForRemoval
|
|
var retrievalState string
|
|
err := s.db.QueryRow(ctx, `SELECT id,test_kb_id,knowledge_id,test_instance_url,test_instance_tenant,retrieval_state
|
|
FROM test_publications
|
|
WHERE id=$1 AND knowledge_id<>'' AND retrieval_state='disabling'`, publicationID).Scan(
|
|
&candidate.ID, &candidate.TestKBID, &candidate.KnowledgeID,
|
|
&candidate.TestInstanceURL, &candidate.TestInstanceTenant, &retrievalState,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return PublicationForRemoval{}, ErrPublicationStateConflict
|
|
}
|
|
if err != nil {
|
|
return PublicationForRemoval{}, fmt.Errorf("load publication for removal: %w", err)
|
|
}
|
|
candidate.RetrievalState = governance.RetrievalState(retrievalState)
|
|
return candidate, nil
|
|
}
|
|
|
|
func (s *Store) RecordRemovalCompleted(ctx context.Context, publicationID string, now time.Time) error {
|
|
tag, err := s.db.Exec(ctx, `UPDATE test_publications
|
|
SET retrieval_state='disabled',last_error='',updated_at=$2
|
|
WHERE id=$1 AND retrieval_state='disabling'`, publicationID, now)
|
|
if err != nil {
|
|
return fmt.Errorf("record publication removal: %w", err)
|
|
}
|
|
if tag.RowsAffected() != 1 {
|
|
return ErrPublicationStateConflict
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) RecordRemovalFailure(ctx context.Context, publicationID, code string, now time.Time) error {
|
|
if code != "removal_identity_invalid" && code != "removal_remote_rejected" {
|
|
return ErrInvalidErrorCode
|
|
}
|
|
tag, err := s.db.Exec(ctx, `UPDATE test_publications
|
|
SET retrieval_state='failed',last_error=$2,updated_at=$3
|
|
WHERE id=$1 AND retrieval_state='disabling'`, publicationID, code, now)
|
|
if err != nil {
|
|
return fmt.Errorf("record publication removal failure: %w", err)
|
|
}
|
|
if tag.RowsAffected() != 1 {
|
|
return ErrPublicationStateConflict
|
|
}
|
|
return nil
|
|
}
|