3f2626f621
build / build (push) Failing after 1m32s
按 schema 版本选 profile 并以 information_schema 能力探测放行,取代写死的 version==74;未知版本、脏库、缺表缺列、跨版本搬运一律拒绝并点名原因。 0.9.0 的索引名改从部署配置解析,wiki_log_entries 按 profile 变为可选, 归档部件集合随之一致。
166 lines
7.3 KiB
Go
166 lines
7.3 KiB
Go
package v071
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
|
||
"github.com/jackc/pgx/v5"
|
||
|
||
"knowledge-governance/internal/libraryprofile"
|
||
"knowledge-governance/internal/releasebundle"
|
||
releaseworkflow "knowledge-governance/internal/workflows/release"
|
||
)
|
||
|
||
type targetContractDB interface {
|
||
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
|
||
}
|
||
|
||
type elasticsearchMappingInspector interface {
|
||
MappingSHA256(ctx context.Context) (string, error)
|
||
}
|
||
|
||
type TargetContractInspectorConfig struct {
|
||
DB targetContractDB
|
||
Mapping elasticsearchMappingInspector
|
||
KnowledgeBaseID string
|
||
TenantID uint64
|
||
IndexName string
|
||
// ReadTestedBase makes the inspector read the TESTED base (the source side
|
||
// of a release) instead of the 源库 → 目标库 mapped base. The exporter and
|
||
// the target share this inspector type, so the side must be explicit: with
|
||
// a mapping in play, "which base do I read" is not the same question on
|
||
// both ends.
|
||
ReadTestedBase bool
|
||
}
|
||
|
||
type TargetContractInspector struct {
|
||
config TargetContractInspectorConfig
|
||
}
|
||
|
||
// targetContractSQL 用 LEFT JOIN 读模型与向量库:命中不到时仍然拿得到"声明了什么",
|
||
// 这样缺字段能被点名拒绝,而不是整行查不出来只报一句"读契约失败"。
|
||
const targetContractSQL = `
|
||
SELECT COALESCE(migration.version, 0),
|
||
COALESCE(migration.dirty, false),
|
||
knowledge_base.tenant_id,
|
||
COALESCE(knowledge_base.embedding_model_id, ''),
|
||
COALESCE(model.name, ''),
|
||
COALESCE(NULLIF(model.parameters->'embedding_parameters'->>'dimension', '')::INTEGER, 0),
|
||
(model.id IS NOT NULL),
|
||
COALESCE(vector_store.engine_type, ''),
|
||
COALESCE(vector_store.index_config->>'index_name', '')
|
||
FROM knowledge_bases AS knowledge_base
|
||
LEFT JOIN models AS model
|
||
ON model.id = knowledge_base.embedding_model_id
|
||
AND model.deleted_at IS NULL
|
||
LEFT JOIN schema_migrations AS migration ON TRUE
|
||
LEFT JOIN vector_stores AS vector_store
|
||
ON vector_store.id = knowledge_base.vector_store_id
|
||
AND vector_store.deleted_at IS NULL
|
||
WHERE knowledge_base.id = $1
|
||
AND knowledge_base.deleted_at IS NULL
|
||
`
|
||
|
||
// resolveTargetIndexName 是"索引名从哪来"的唯一判定点。
|
||
//
|
||
// 0.9.0 的 KB 不写 vector_store_id(vector_stores 没有对应行),索引名只在部署
|
||
// 配置里(ELASTICSEARCH_INDEX)。两条来源都拿不到时**不许**回退成空串——
|
||
// 空串会让后续过滤条件匹配到任何索引。0.7.1 若两处都有,必须一致。
|
||
func resolveTargetIndexName(fromVectorStore, configured string, profile libraryprofile.Profile) (string, error) {
|
||
resolved := fromVectorStore
|
||
if resolved == "" {
|
||
resolved = configured
|
||
}
|
||
if !validSearchIdentifier(resolved) {
|
||
return "", fmt.Errorf("target WeKnora index name unresolved (contract source: %s): vector_stores.index_config->>'index_name' and the configured index are both empty", profile.IndexContractSource)
|
||
}
|
||
if fromVectorStore != "" && fromVectorStore != configured {
|
||
return "", fmt.Errorf("target WeKnora contract mismatch: vector_stores.index_config.index_name=%q does not match configured index %q", fromVectorStore, configured)
|
||
}
|
||
return resolved, nil
|
||
}
|
||
|
||
func NewTargetContractInspector(config TargetContractInspectorConfig) (*TargetContractInspector, error) {
|
||
config.KnowledgeBaseID = strings.TrimSpace(config.KnowledgeBaseID)
|
||
config.IndexName = strings.TrimSpace(config.IndexName)
|
||
if config.DB == nil || config.Mapping == nil || config.TenantID == 0 || !validSearchIdentifier(config.KnowledgeBaseID) || !validSearchIdentifier(config.IndexName) {
|
||
return nil, errors.New("invalid target contract inspector configuration")
|
||
}
|
||
return &TargetContractInspector{config: config}, nil
|
||
}
|
||
|
||
func (inspector *TargetContractInspector) Contract(ctx context.Context, item releaseworkflow.PlanItem) (releasebundle.TargetContract, error) {
|
||
if item.TestKnowledgeBaseID != inspector.config.KnowledgeBaseID {
|
||
return releasebundle.TargetContract{}, errors.New("release plan knowledge base does not match target contract")
|
||
}
|
||
// The route is keyed by the TESTED base (where the content was verified). The
|
||
// contract is read for the base this side actually owns: the source side reads
|
||
// the tested base (what the bundle is exported from), the target side reads the
|
||
// 源库 → 目标库 mapped base (what the release writes into).
|
||
targetKnowledgeBaseID := item.TargetBase()
|
||
if inspector.config.ReadTestedBase {
|
||
targetKnowledgeBaseID = item.TestKnowledgeBaseID
|
||
}
|
||
if !validSearchIdentifier(targetKnowledgeBaseID) {
|
||
return releasebundle.TargetContract{}, errors.New("invalid release target knowledge base")
|
||
}
|
||
var (
|
||
migrationVersion int
|
||
dirty bool
|
||
tenantID uint64
|
||
declaredModel string
|
||
embeddingModel string
|
||
dimension int
|
||
modelBound bool
|
||
engineType string
|
||
indexName string
|
||
)
|
||
if err := inspector.config.DB.QueryRow(ctx, targetContractSQL, targetKnowledgeBaseID).Scan(
|
||
&migrationVersion, &dirty, &tenantID, &declaredModel, &embeddingModel, &dimension, &modelBound, &engineType, &indexName,
|
||
); err != nil {
|
||
return releasebundle.TargetContract{}, errors.New("read target WeKnora contract")
|
||
}
|
||
if dirty {
|
||
return releasebundle.TargetContract{}, fmt.Errorf("target WeKnora schema_migrations.version=%d is dirty: refusing to publish", migrationVersion)
|
||
}
|
||
// 版本号只用来选 profile;未知版本失败关闭,错误里带真实版本号。
|
||
profile, err := libraryprofile.For(migrationVersion)
|
||
if err != nil {
|
||
return releasebundle.TargetContract{}, fmt.Errorf("target WeKnora: %w", err)
|
||
}
|
||
if tenantID != inspector.config.TenantID {
|
||
return releasebundle.TargetContract{}, fmt.Errorf("target WeKnora contract mismatch: knowledge_bases.tenant_id=%d, expected %d", tenantID, inspector.config.TenantID)
|
||
}
|
||
if !validSearchIdentifier(declaredModel) {
|
||
return releasebundle.TargetContract{}, errors.New("target WeKnora contract mismatch: knowledge_bases.embedding_model_id is empty: embedding dimension is unknowable")
|
||
}
|
||
if !modelBound {
|
||
return releasebundle.TargetContract{}, fmt.Errorf("target WeKnora contract mismatch: models row for knowledge_bases.embedding_model_id %q is missing or deleted", declaredModel)
|
||
}
|
||
if !validSearchIdentifier(embeddingModel) || dimension < 1 {
|
||
return releasebundle.TargetContract{}, fmt.Errorf("target WeKnora contract mismatch: models.parameters.embedding_parameters.dimension is missing for model %q", declaredModel)
|
||
}
|
||
if engineType != "" && engineType != "elasticsearch" {
|
||
return releasebundle.TargetContract{}, fmt.Errorf("target WeKnora contract mismatch: vector_stores.engine_type=%q is not elasticsearch", engineType)
|
||
}
|
||
if _, err := resolveTargetIndexName(indexName, inspector.config.IndexName, profile); err != nil {
|
||
return releasebundle.TargetContract{}, err
|
||
}
|
||
mappingSHA256, err := inspector.config.Mapping.MappingSHA256(ctx)
|
||
if err != nil || !validLowerSHA256(mappingSHA256) {
|
||
return releasebundle.TargetContract{}, errors.New("read target Elasticsearch mapping contract")
|
||
}
|
||
return releasebundle.TargetContract{
|
||
WeKnoraVersion: profile.WeKnoraVersion,
|
||
MigrationVersion: profile.SchemaVersion,
|
||
TenantID: tenantID,
|
||
KnowledgeBaseID: targetKnowledgeBaseID,
|
||
RetrieverEngine: "elasticsearch_v8",
|
||
EmbeddingModel: embeddingModel,
|
||
EmbeddingDimension: dimension,
|
||
ElasticsearchMappingSHA256: mappingSHA256,
|
||
}, nil
|
||
}
|