fba2713b4e
build / build (push) Successful in 1m54s
整库发布写入路径原先按 0.7.1 的固定表集合工作:0.9.0 没有 wiki_log_entries, Current/清理/编目/整实例指纹都会在不存在表上失败,跨版本闸门也因快照丢掉 SchemaVersion 而失效(未知版本被当作 74)。 改为在装配时解析目标 profile,表集合与清理顺序按解析结果;快照复制与范围改写 带上 SchemaVersion/Profile;跨版本发布显式拒绝并同时点名两个版本。单文档发布包 仍只支持 0.7.1,但改为明确拒绝并说明原因。 本机隔离验证:真实 0.9.0 schema 源库→导出→封存→写入干净 134 目标→逐表核对→ 写前镜像回滚;74→134 拒绝与 134 缺表不报错两个负例通过。make verify 17 项全绿。
133 lines
4.5 KiB
Go
133 lines
4.5 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
type migrationUniqueKey struct {
|
|
Name string
|
|
Columns []string
|
|
Predicate string
|
|
NullsEqual bool
|
|
Expression bool
|
|
}
|
|
|
|
// Preflight all catalog unique keys, including partial indexes. Source-local
|
|
// sequence keys are checked separately by the allocator, never compared as UUIDs.
|
|
func (t *ProductionLibraryTarget) preflightNaturalKeys(ctx context.Context, archive *librarysnapshot.Archive, manifest librarysnapshot.Manifest) error {
|
|
for _, part := range manifest.Parts {
|
|
if (part.Kind != "database" && part.Kind != "wiki") || part.Count == 0 {
|
|
continue
|
|
}
|
|
contract, known := migrationIdentityContracts[part.Name]
|
|
if !known {
|
|
return errors.New("unknown natural-key contract")
|
|
}
|
|
reader, err := archive.OpenPart(part)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var source []map[string]json.RawMessage
|
|
err = json.NewDecoder(reader).Decode(&source)
|
|
reader.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, row := range source {
|
|
if err := t.retargetRow(part.Name, row); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
raw, err := json.Marshal(source)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rows, err := t.pool.Query(ctx, `SELECT ic.relname,ARRAY(SELECT a.attname::text FROM unnest(i.indkey) WITH ORDINALITY k(attnum,ord) JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=k.attnum WHERE k.ord<=i.indnkeyatts ORDER BY k.ord),coalesce(pg_get_expr(i.indpred,i.indrelid),''),i.indnullsnotdistinct,i.indexprs IS NOT NULL FROM pg_index i JOIN pg_class c ON c.oid=i.indrelid JOIN pg_namespace n ON n.oid=c.relnamespace JOIN pg_class ic ON ic.oid=i.indexrelid WHERE n.nspname='public' AND c.relname=$1 AND i.indisunique ORDER BY ic.relname`, part.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var keys []migrationUniqueKey
|
|
for rows.Next() {
|
|
var key migrationUniqueKey
|
|
if err := rows.Scan(&key.Name, &key.Columns, &key.Predicate, &key.NullsEqual, &key.Expression); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
keys = append(keys, key)
|
|
}
|
|
rows.Close()
|
|
if err := rows.Err(); err != nil {
|
|
return err
|
|
}
|
|
table := pgx.Identifier{"public", part.Name}.Sanitize()
|
|
recordset := "jsonb_populate_recordset(NULL::" + table + ",$1::jsonb)"
|
|
for _, key := range keys {
|
|
if key.Expression || len(key.Columns) == 0 {
|
|
return fmt.Errorf("unreviewed unique expression: %s", key.Name)
|
|
}
|
|
skip := false
|
|
for _, column := range key.Columns {
|
|
if column == contract.LocalColumn {
|
|
skip = true
|
|
}
|
|
}
|
|
if skip {
|
|
continue
|
|
}
|
|
predicate := "true"
|
|
if key.Predicate != "" {
|
|
predicate = key.Predicate
|
|
}
|
|
cols, nonnull, join := []string{}, []string{}, []string{}
|
|
for _, column := range key.Columns {
|
|
q := pgx.Identifier{column}.Sanitize()
|
|
cols = append(cols, q)
|
|
nonnull = append(nonnull, q+" IS NOT NULL")
|
|
op := "="
|
|
if key.NullsEqual {
|
|
op = "IS NOT DISTINCT FROM"
|
|
}
|
|
join = append(join, "s."+q+" "+op+" t."+q)
|
|
}
|
|
sourceFilter := predicate
|
|
if !key.NullsEqual {
|
|
sourceFilter += " AND " + strings.Join(nonnull, " AND ")
|
|
}
|
|
var conflict bool
|
|
query := "SELECT EXISTS(SELECT 1 FROM " + recordset + " s WHERE " + sourceFilter + " GROUP BY " + strings.Join(cols, ",") + " HAVING count(*)>1)"
|
|
if err := t.pool.QueryRow(ctx, query, raw).Scan(&conflict); err != nil {
|
|
return fmt.Errorf("source unique preflight %s: %w", key.Name, err)
|
|
}
|
|
if conflict {
|
|
return fmt.Errorf("source duplicates natural key %s", key.Name)
|
|
}
|
|
targetFilter := predicate
|
|
args := []any{raw}
|
|
if part.Name != "resources" {
|
|
scope := strings.ReplaceAll(strings.ReplaceAll(t.scopeWhere(part.Name), "$2", "$3"), "$1", "$2")
|
|
targetFilter += " AND NOT (" + scope + ")"
|
|
args = append(args, t.scope.TenantID, t.scope.KnowledgeBaseID)
|
|
}
|
|
query = "SELECT EXISTS(SELECT 1 FROM (SELECT * FROM " + recordset + " WHERE " + sourceFilter + ") s JOIN (SELECT * FROM " + table + " WHERE " + targetFilter + ") t ON " + strings.Join(join, " AND ") + ")"
|
|
// Equal resource ID is handled by immutable row/object reuse evidence.
|
|
if part.Name == "resources" {
|
|
query = strings.TrimSuffix(query, ")") + " WHERE s.id IS DISTINCT FROM t.id)"
|
|
}
|
|
if err := t.pool.QueryRow(ctx, query, args...).Scan(&conflict); err != nil {
|
|
return fmt.Errorf("target unique preflight %s: %w", key.Name, err)
|
|
}
|
|
if conflict {
|
|
return fmt.Errorf("target natural key conflict outside replace scope: %s", key.Name)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|