8d838d9cb4
build / build (push) Successful in 1m45s
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
84 lines
3.1 KiB
Go
84 lines
3.1 KiB
Go
package v071
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// The contract names physical identities, not inferred *_id conventions. A
|
|
// string ID is preserved byte-for-byte (older archives also contain non-UUID
|
|
// varchar IDs). Database-local integers must never pass through float64.
|
|
const migrationIdentityContractVersion = 1
|
|
|
|
type rowIdentityContract struct {
|
|
Columns []string
|
|
Kind string
|
|
LocalColumn string
|
|
Sequence string
|
|
}
|
|
|
|
var migrationIdentityContracts = map[string]rowIdentityContract{
|
|
"knowledges": {Columns: []string{"id"}, Kind: "stable-string"},
|
|
"chunks": {Columns: []string{"id"}, Kind: "stable-string", LocalColumn: "seq_id", Sequence: "chunks_seq_id_seq"},
|
|
"knowledge_tags": {Columns: []string{"id"}, Kind: "stable-string", LocalColumn: "seq_id", Sequence: "knowledge_tags_seq_id_seq"},
|
|
"knowledge_tag_relations": {Columns: []string{"knowledge_id", "tag_id"}, Kind: "tuple"},
|
|
"resources": {Columns: []string{"id"}, Kind: "stable-string"},
|
|
"resource_bindings": {Columns: []string{"id"}, Kind: "stable-string"},
|
|
"wiki_folders": {Columns: []string{"id"}, Kind: "stable-string"},
|
|
"wiki_pages": {Columns: []string{"id"}, Kind: "stable-string"},
|
|
"wiki_page_issues": {Columns: []string{"id"}, Kind: "stable-string"},
|
|
"wiki_log_entries": {Columns: []string{"id"}, Kind: "local-bigint", LocalColumn: "id", Sequence: "wiki_log_entries_id_seq"},
|
|
}
|
|
|
|
type typedRowIdentity struct {
|
|
Kind string
|
|
Values []string
|
|
}
|
|
|
|
// key retains the V1 ledger representation, but tuple components are validated
|
|
// independently and used as separate SQL parameters, never as a single column.
|
|
func (id typedRowIdentity) key() string { return strings.Join(id.Values, ":") }
|
|
|
|
func positiveBigint(raw json.RawMessage) (string, error) {
|
|
raw = bytes.TrimSpace(raw)
|
|
if len(raw) == 0 {
|
|
return "", errors.New("missing integer")
|
|
}
|
|
for _, b := range raw {
|
|
if b < '0' || b > '9' {
|
|
return "", errors.New("expected JSON integer, not string/fraction/exponent")
|
|
}
|
|
}
|
|
n, err := strconv.ParseInt(string(raw), 10, 64)
|
|
if err != nil || n <= 0 || strconv.FormatInt(n, 10) != string(raw) {
|
|
return "", errors.New("integer out of positive bigint range or noncanonical")
|
|
}
|
|
return string(raw), nil
|
|
}
|
|
|
|
func parseRowIdentity(table string, row map[string]json.RawMessage) (typedRowIdentity, error) {
|
|
contract, ok := migrationIdentityContracts[table]
|
|
if !ok {
|
|
return typedRowIdentity{}, fmt.Errorf("unknown migration identity contract: %s", table)
|
|
}
|
|
id := typedRowIdentity{Kind: contract.Kind}
|
|
for _, column := range contract.Columns {
|
|
var value string
|
|
if contract.Kind == "local-bigint" {
|
|
n, err := positiveBigint(row[column])
|
|
if err != nil {
|
|
return id, fmt.Errorf("%s.%s: %w", table, column, err)
|
|
}
|
|
value = n
|
|
} else if err := json.Unmarshal(row[column], &value); err != nil || !validSearchIdentifier(value) || strings.Contains(value, ":") {
|
|
return id, fmt.Errorf("invalid %s.%s identity", table, column)
|
|
}
|
|
id.Values = append(id.Values, value)
|
|
}
|
|
return id, nil
|
|
}
|