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, productionBindingIdentityUpsertSQL, internalID, externalID, updatedBy, now, "", "", 0, 0); err != nil { return errors.New("save production kb bindings") } return nil } // productionBindingIdentityUpsertSQL freezes the instance identity the console // verified each side on. An empty URL keeps the legacy same-id behaviour for // old callers; the executor then cannot prove the instance and refuses a mapped // write instead of guessing. const productionBindingIdentityUpsertSQL = ` INSERT INTO production_kb_bindings( id,internal_kb_id,external_kb_id,updated_by,updated_at, internal_instance_url,external_instance_url,internal_tenant_id,external_tenant_id) VALUES('active',$1,$2,$3,$4,$5,$6,$7,$8) 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, internal_instance_url=EXCLUDED.internal_instance_url, external_instance_url=EXCLUDED.external_instance_url, internal_tenant_id=EXCLUDED.internal_tenant_id, external_tenant_id=EXCLUDED.external_tenant_id` // SaveProductionKBBindingsTx lets the environment-scoped 「知识库绑定」save // update the same row inside its own transaction, so connection and bindings // cannot be half-written. func SaveProductionKBBindingsTx(ctx context.Context, tx pgx.Tx, internalID, externalID, updatedBy string, now time.Time) error { return SaveProductionKBBindingsWithIdentityTx(ctx, tx, internalID, externalID, "", "", 0, 0, updatedBy, now) } // SaveProductionKBBindingsWithIdentityTx is used by the environment-scoped // 「知识库绑定」save: it stores which instance/tenant each side was verified on, // so the release executor can refuse a cloned instance with the same base id. func SaveProductionKBBindingsWithIdentityTx(ctx context.Context, tx pgx.Tx, internalID, externalID, internalURL, externalURL string, internalTenant, externalTenant uint64, updatedBy string, now time.Time) error { if _, err := tx.Exec(ctx, productionBindingIdentityUpsertSQL, internalID, externalID, updatedBy, now, internalURL, externalURL, internalTenant, externalTenant); 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 }