aac8538372
build / build (push) Successful in 1m48s
- 迁移 000084 保存对内/对外绑定名称快照;清除密钥后仍显示已绑定库,不再只显示裸 ID - 测试/生产换绑只重指向同一侧的旧目标,避免测试保存误改另一环境的待入库 - 已保存连接但没有可用密钥时拒绝改绑定,禁止静默回落文件 Key - 生产换绑返回重指向/退休数量,页面提示与测试侧一致 验证:make verify(含隔离 PostgreSQL 集成用例)15/15 通过
141 lines
5.2 KiB
Go
141 lines
5.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"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
|
|
InternalKBName string
|
|
ExternalKBName string
|
|
UpdatedBy string
|
|
}
|
|
|
|
// ProductionBindingSave is one UI save of the production-side targets. Names
|
|
// are display snapshots; see RoutingBindingSave for the preserve rules.
|
|
type ProductionBindingSave struct {
|
|
InternalID string
|
|
ExternalID string
|
|
InternalName string
|
|
ExternalName string
|
|
UpdatedBy string
|
|
}
|
|
|
|
func (s *Store) loadProductionKBBinding(ctx context.Context) (ProductionBinding, bool, error) {
|
|
var binding ProductionBinding
|
|
err := s.db.QueryRow(ctx, `
|
|
SELECT internal_kb_id,external_kb_id,internal_kb_name,external_kb_name,updated_by
|
|
FROM production_kb_bindings WHERE id='active'`).
|
|
Scan(&binding.InternalKBID, &binding.ExternalKBID, &binding.InternalKBName, &binding.ExternalKBName, &binding.UpdatedBy)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ProductionBinding{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return ProductionBinding{}, false, errors.New("load production kb bindings")
|
|
}
|
|
return binding, true, nil
|
|
}
|
|
|
|
// 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) {
|
|
binding, _, err := s.loadProductionKBBinding(ctx)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
return binding.InternalKBID, binding.ExternalKBID, binding.UpdatedBy, nil
|
|
}
|
|
|
|
// LoadProductionKBBindingNames reads the display snapshot stored with the
|
|
// production mapping.
|
|
func (s *Store) LoadProductionKBBindingNames(ctx context.Context) (string, string, error) {
|
|
binding, _, err := s.loadProductionKBBinding(ctx)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return binding.InternalKBName, binding.ExternalKBName, nil
|
|
}
|
|
|
|
// SaveProductionKBBindingsWithReplan saves the production mapping and, in the
|
|
// SAME transaction, re-points 待入库 publications that were still waiting for
|
|
// the previous production target. Only rows already aimed at the previous
|
|
// production id move; a test-side publication with the same scope is untouched.
|
|
func (s *Store) SaveProductionKBBindingsWithReplan(ctx context.Context, save ProductionBindingSave, previous, effective EffectiveKBIDs, now time.Time) (RoutingBindingReplanResult, error) {
|
|
var result RoutingBindingReplanResult
|
|
if now.IsZero() {
|
|
return result, errors.New("production binding replan requires a timestamp")
|
|
}
|
|
tx, err := s.db.Begin(ctx)
|
|
if err != nil {
|
|
return result, fmt.Errorf("begin production binding save: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
|
|
var previousRow ProductionBinding
|
|
err = tx.QueryRow(ctx, `
|
|
SELECT internal_kb_id,external_kb_id,internal_kb_name,external_kb_name
|
|
FROM production_kb_bindings WHERE id='active' FOR UPDATE`).
|
|
Scan(&previousRow.InternalKBID, &previousRow.ExternalKBID, &previousRow.InternalKBName, &previousRow.ExternalKBName)
|
|
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
|
return result, errors.New("load previous production kb bindings")
|
|
}
|
|
internalName := preservedBindingName(save.InternalID, save.InternalName, previousRow.InternalKBID, previousRow.InternalKBName)
|
|
externalName := preservedBindingName(save.ExternalID, save.ExternalName, previousRow.ExternalKBID, previousRow.ExternalKBName)
|
|
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO production_kb_bindings(id,internal_kb_id,external_kb_id,internal_kb_name,external_kb_name,updated_by,updated_at)
|
|
VALUES('active',$1,$2,$3,$4,$5,$6)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
internal_kb_id=EXCLUDED.internal_kb_id,
|
|
external_kb_id=EXCLUDED.external_kb_id,
|
|
internal_kb_name=EXCLUDED.internal_kb_name,
|
|
external_kb_name=EXCLUDED.external_kb_name,
|
|
updated_by=EXCLUDED.updated_by,
|
|
updated_at=EXCLUDED.updated_at`,
|
|
save.InternalID, save.ExternalID, internalName, externalName, save.UpdatedBy, now); err != nil {
|
|
return result, errors.New("save production kb bindings")
|
|
}
|
|
result, err = replanPendingPublicationsTx(ctx, tx, previous, effective, now)
|
|
if err != nil {
|
|
return RoutingBindingReplanResult{}, err
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return result, fmt.Errorf("commit production binding save: %w", err)
|
|
}
|
|
return result, 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
|
|
}
|