245 lines
9.4 KiB
Go
245 lines
9.4 KiB
Go
package metadata
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// A core-compatible schema is a rehearsal scope, not an application readiness
|
|
// claim. New application tables and security triggers always block core-only
|
|
// copying; unknown data must never be silently dropped as a "cache".
|
|
type PortabilityReport struct {
|
|
Scope string `json:"scope"`
|
|
ApplicationReady bool `json:"application_ready"`
|
|
CoreCompatible bool `json:"core_compatible"`
|
|
Tables []TablePortability `json:"tables"`
|
|
Blockers []string `json:"blockers"`
|
|
}
|
|
type TablePortability struct {
|
|
Name string `json:"name"`
|
|
Component string `json:"component"`
|
|
State string `json:"state"`
|
|
}
|
|
type tableSpec struct {
|
|
name string
|
|
columns []string
|
|
keys []string
|
|
identity bool
|
|
}
|
|
|
|
// Explicit columns and primary-key order define an unambiguous streaming
|
|
// fingerprint and an FK-safe copy order. Migration ledgers stay backend-local.
|
|
var coreTables = []tableSpec{
|
|
{"objects", strings.Split("uuid,kind,created_at", ","), []string{"uuid"}, false},
|
|
{"object_origins", strings.Split("object_uuid,domain_uuid,created_at", ","), []string{"object_uuid"}, false},
|
|
{"paths", strings.Split("domain_uuid,path,object_uuid,created_at,is_primary", ","), []string{"domain_uuid", "path"}, false},
|
|
{"revisions", strings.Split(revisionColumns+",created_at,body", ","), []string{"uuid"}, false},
|
|
{"assets", strings.Split(assetColumns, ","), []string{"domain_uuid", "address"}, false},
|
|
{"asset_head_history", strings.Split("id,domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms", ","), []string{"id"}, true},
|
|
{"revision_document_refs", []string{"revision_uuid", "document_hash"}, []string{"revision_uuid", "document_hash"}, false},
|
|
{"activity_outbox", strings.Split("uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,status,attempts,next_attempt_ms,last_error,created_at_ms,claimed_at_ms", ","), []string{"uuid"}, false},
|
|
{"activity_outbox_heads", []string{"domain_uuid", "event_type", "last_sequence"}, []string{"domain_uuid", "event_type"}, false},
|
|
}
|
|
|
|
func componentForTable(name string) string {
|
|
for _, prefix := range []string{"auth", "principal", "group", "membership", "task", "calendar", "event", "notification", "activity", "federation", "domain", "campus", "config", "import", "resource", "adapter", "user", "profile", "contact", "feed", "periodical", "search", "locality", "city"} {
|
|
if strings.HasPrefix(name, prefix) {
|
|
return prefix
|
|
}
|
|
}
|
|
if name == "zones" || name == "spots" || strings.HasPrefix(name, "spot_") || strings.HasPrefix(name, "street_") || name == "topology_events" {
|
|
return "city"
|
|
}
|
|
return "unclassified"
|
|
}
|
|
|
|
func (r *Repository) InspectPortability(ctx context.Context) (PortabilityReport, error) {
|
|
tx, err := r.database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
|
if err != nil {
|
|
return PortabilityReport{}, r.failure(err)
|
|
}
|
|
defer tx.Rollback()
|
|
return r.inspectPortability(ctx, tx)
|
|
}
|
|
|
|
func (r *Repository) inspectPortability(ctx context.Context, tx *sql.Tx) (PortabilityReport, error) {
|
|
report := PortabilityReport{Scope: "portable-core-rehearsal-only", Tables: []TablePortability{}, Blockers: []string{}}
|
|
known := map[string]tableSpec{}
|
|
for _, s := range coreTables {
|
|
known[s.name] = s
|
|
}
|
|
query := `SELECT name,type FROM sqlite_master WHERE type IN ('table','view','trigger') AND substr(name,1,7)!='sqlite_' ORDER BY name`
|
|
if r.backend == PostgreSQL {
|
|
query = `SELECT c.relname,CASE WHEN c.relkind IN ('r','p') THEN 'table' ELSE 'view' END FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND c.relkind IN ('r','p','v','m','f')
|
|
UNION ALL SELECT t.tgname,'trigger' FROM pg_trigger t JOIN pg_class c ON c.oid=t.tgrelid JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND NOT t.tgisinternal
|
|
UNION ALL SELECT p.proname,'routine' FROM pg_proc p JOIN pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname=current_schema()
|
|
UNION ALL SELECT r.rulename,'rule' FROM pg_rewrite r JOIN pg_class c ON c.oid=r.ev_class JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND r.rulename!='_RETURN'
|
|
UNION ALL SELECT c.relname,'sequence' FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND c.relkind='S' AND NOT EXISTS(SELECT 1 FROM pg_depend d JOIN pg_class t ON t.oid=d.refobjid JOIN pg_namespace tn ON tn.oid=t.relnamespace WHERE d.objid=c.oid AND d.classid='pg_class'::regclass AND d.refclassid='pg_class'::regclass AND tn.nspname=current_schema() AND t.relname='asset_head_history' AND d.deptype='i') ORDER BY 1`
|
|
}
|
|
rows, err := tx.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return report, r.failure(err)
|
|
}
|
|
types := map[string]string{}
|
|
for rows.Next() {
|
|
var name, kind string
|
|
if err := rows.Scan(&name, &kind); err != nil {
|
|
rows.Close()
|
|
return report, r.failure(err)
|
|
}
|
|
if kind != "table" {
|
|
report.Blockers = append(report.Blockers, fmt.Sprintf("unported %s: %s", kind, name))
|
|
continue
|
|
}
|
|
types[name] = kind
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return report, r.failure(err)
|
|
}
|
|
names := make([]string, 0, len(types))
|
|
for name := range types {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
for _, name := range names {
|
|
kind := types[name]
|
|
if kind != "table" {
|
|
report.Blockers = append(report.Blockers, fmt.Sprintf("unported %s: %s", kind, name))
|
|
continue
|
|
}
|
|
if name == "schema_migration_steps" {
|
|
continue
|
|
}
|
|
item := TablePortability{Name: name, Component: componentForTable(name), State: "unported"}
|
|
if _, ok := known[name]; ok {
|
|
item.Component = "core-metadata"
|
|
item.State = "shape-pending"
|
|
} else {
|
|
report.Blockers = append(report.Blockers, "unported table: "+name)
|
|
}
|
|
report.Tables = append(report.Tables, item)
|
|
}
|
|
for _, spec := range coreTables {
|
|
if types[spec.name] != "table" {
|
|
report.Blockers = append(report.Blockers, "missing core table: "+spec.name)
|
|
}
|
|
}
|
|
if types["schema_migration_steps"] != "table" {
|
|
report.Blockers = append(report.Blockers, "missing portable migration ledger")
|
|
} else {
|
|
if err := r.inspectCoreHistory(ctx, tx, &report); err != nil {
|
|
return report, err
|
|
}
|
|
}
|
|
for i := range report.Tables {
|
|
item := &report.Tables[i]
|
|
spec, ok := known[item.Name]
|
|
if !ok {
|
|
continue
|
|
}
|
|
columns, err := r.tableColumns(ctx, tx, item.Name)
|
|
if err != nil {
|
|
return report, err
|
|
}
|
|
actual := map[string]bool{}
|
|
for _, c := range columns {
|
|
actual[c] = true
|
|
}
|
|
matches := len(actual) == len(spec.columns)
|
|
for _, c := range spec.columns {
|
|
matches = matches && actual[c]
|
|
}
|
|
if !matches {
|
|
item.State = "schema-drift"
|
|
report.Blockers = append(report.Blockers, "column mismatch: "+item.Name)
|
|
} else {
|
|
item.State = "core-shape-compatible"
|
|
}
|
|
}
|
|
if err := r.inspectCatalog(ctx, tx, &report); err != nil {
|
|
return report, err
|
|
}
|
|
sort.Strings(report.Blockers)
|
|
report.CoreCompatible = len(report.Blockers) == 0
|
|
// This core-only report does not certify full-application readiness. Use
|
|
// the Store application importer for the production migration inventory.
|
|
// ApplicationReady intentionally remains false here because non-core
|
|
// repositories, canonical files and worker state are outside this check.
|
|
return report, nil
|
|
}
|
|
|
|
func (r *Repository) tableColumns(ctx context.Context, tx *sql.Tx, table string) ([]string, error) {
|
|
query := `SELECT name FROM pragma_table_info(` + r.p(1) + `) ORDER BY cid`
|
|
if r.backend == PostgreSQL {
|
|
query = `SELECT column_name FROM information_schema.columns WHERE table_schema=current_schema() AND table_name=$1 ORDER BY ordinal_position`
|
|
}
|
|
rows, err := tx.QueryContext(ctx, query, table)
|
|
if err != nil {
|
|
return nil, r.failure(err)
|
|
}
|
|
defer rows.Close()
|
|
var out []string
|
|
for rows.Next() {
|
|
var name string
|
|
if err := rows.Scan(&name); err != nil {
|
|
return nil, r.failure(err)
|
|
}
|
|
out = append(out, name)
|
|
}
|
|
return out, r.failure(rows.Err())
|
|
}
|
|
|
|
func (r *Repository) inspectCoreHistory(ctx context.Context, tx *sql.Tx, report *PortabilityReport) error {
|
|
sqliteSteps, pgSteps := r.migrationDefinitions()
|
|
checksums := map[int]string{}
|
|
names := map[int]string{}
|
|
add := func(version int, name string, step any) {
|
|
data, _ := json.Marshal(step)
|
|
sum := sha256.Sum256(data)
|
|
checksums[version] = hex.EncodeToString(sum[:])
|
|
names[version] = name
|
|
}
|
|
for _, s := range sqliteSteps {
|
|
add(s.Version, s.Name, s)
|
|
}
|
|
for _, s := range pgSteps {
|
|
add(s.Version, s.Name, s)
|
|
}
|
|
rows, err := tx.QueryContext(ctx, `SELECT component,version,name,checksum FROM schema_migration_steps ORDER BY component,version`)
|
|
if err != nil {
|
|
return r.failure(err)
|
|
}
|
|
defer rows.Close()
|
|
applied := 0
|
|
for rows.Next() {
|
|
var component, name, checksum string
|
|
var version int
|
|
if err := rows.Scan(&component, &version, &name, &checksum); err != nil {
|
|
return r.failure(err)
|
|
}
|
|
if component != "core-metadata" {
|
|
report.Blockers = append(report.Blockers, "unported migration component: "+component)
|
|
continue
|
|
}
|
|
if version != applied+1 || names[version] != name || checksums[version] != checksum {
|
|
report.Blockers = append(report.Blockers, "portable migration history mismatch")
|
|
}
|
|
applied++
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return r.failure(err)
|
|
}
|
|
if applied != len(checksums) {
|
|
report.Blockers = append(report.Blockers, "portable migrations are not current")
|
|
}
|
|
return nil
|
|
}
|