schemafamily

package
v0.144.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schemafamily identifies which database family (DbModel) a catalog belongs to from its schema shape alone, and reconciles schemas across N instances of one family.

It implements the fingerprint and structural classification stages of the semantic-discovery "database identity" spec: fp1 fingerprints at the `types` and `keys` levels, and deterministic bands with measured evidence. Fingerprints identify structure, not systems: a match is evidence, never proof that two databases are the same logical system. The package reads no records and knows no database engine; callers convert their scanner output into Catalog.

Index

Constants

This section is empty.

Variables

View Source
var Default = Thresholds{CollectionOverlap: 0.8, FieldOverlap: 0.8, MinCollections: 3, IdentifierMatches: 3}

Default is the spec's threshold set.

Functions

This section is empty.

Types

type Band

type Band string

Band is a deterministic classification of a catalog against one family.

const (
	BandIdentical                  Band = "identical"
	BandSameSystemDifferentVersion Band = "same-system-different-version"
	BandRelatedDistinct            Band = "related-distinct"
	BandNew                        Band = "new"
	BandInsufficientEvidence       Band = "insufficient-evidence"
)

type Catalog

type Catalog struct {
	ID              string       `json:"id"`
	Collections     []Collection `json:"collections"`
	KeysUnavailable string       `json:"keysUnavailable,omitempty"`
}

Catalog is one database's shape snapshot. KeysUnavailable names why keys could not be read (for example an adapter that does not read foreign keys); the keys fingerprint is then absent.

type Collection

type Collection struct {
	Schema      string       `json:"schema,omitempty"`
	Name        string       `json:"name"`
	Kind        string       `json:"kind"` // table | view | collection
	Fields      []Field      `json:"fields"`
	UniqueKeys  [][]string   `json:"uniqueKeys,omitempty"`
	ForeignKeys []ForeignKey `json:"foreignKeys,omitempty"`
}

Collection is one table, view or document collection.

func (Collection) QualifiedName

func (c Collection) QualifiedName() string

QualifiedName is `schema.name`, or `name` without a schema.

type Evidence

type Evidence struct {
	SharedCollections   int      `json:"sharedCollections"`
	UnionCollections    int      `json:"unionCollections"`
	SharedFields        int      `json:"sharedFields"`
	UnionFields         int      `json:"unionFields"`
	TypeCompatible      int      `json:"typeCompatibleFields"`
	IdentifierMatches   int      `json:"identifierMatches"`
	SamePrimaryKeys     bool     `json:"samePrimaryKeys"`
	OnlyHere            []string `json:"onlyHere,omitempty"`
	OnlyInFamily        []string `json:"onlyInFamily,omitempty"`
	TypeChanges         []string `json:"typeChanges,omitempty"`
	FingerprintLevel    string   `json:"fingerprintLevel,omitempty"`
	CollectionOverlapPc int      `json:"collectionOverlapPercent"`
	FieldOverlapPc      int      `json:"fieldOverlapPercent"`
}

Evidence is reported instead of a single score.

type Family

type Family struct {
	ID      string  `json:"id"`
	Catalog Catalog `json:"catalog"`
}

Family is a known database family: its id (the DbModel id) and the shape of one representative catalog (the most recent attached scan).

type Field

type Field struct {
	Name       string `json:"name"`
	Type       string `json:"type"`
	Nullable   bool   `json:"nullable,omitempty"`
	PrimaryKey bool   `json:"primaryKey,omitempty"`
}

Field is one column or document field with its portable type.

type Fingerprint

type Fingerprint struct {
	Types           string  `json:"types"`
	Keys            *string `json:"keys"`
	KeysUnavailable string  `json:"keysUnavailable,omitempty"`
}

Fingerprint holds the fp1 levels of one catalog.

func Compute

func Compute(catalog Catalog) Fingerprint

Compute returns the catalog's fp1 fingerprints. Excluded by design: row counts, statistics, comments, index and constraint names, column ordinal, defaults, collation, owner, grants, timestamps and engine version.

type FingerprintGroup

type FingerprintGroup struct {
	Fingerprint string   `json:"fingerprint"`
	Instances   []string `json:"instances"`
}

FingerprintGroup is a set of instances with the same fingerprint.

type ForeignKey

type ForeignKey struct {
	Columns    []string `json:"columns"`
	Collection string   `json:"collection"`
	RefColumns []string `json:"refColumns"`
}

ForeignKey is `(columns) → collection(columns)`, without constraint names.

type Match

type Match struct {
	Family     string   `json:"family"`
	Band       Band     `json:"band"`
	Confidence string   `json:"confidence,omitempty"` // likely | possible
	Evidence   Evidence `json:"evidence"`
}

Match is the classification of a catalog against one family.

func Classify

func Classify(catalog Catalog, families []Family, thresholds Thresholds) []Match

Classify compares a catalog with every known family and returns matches ordered best band first. A family whose band is `new` is omitted; when no family matches, the single result has band `new` (or `insufficient-evidence`) and no family.

type SchemaDrift

type SchemaDrift struct {
	Collection string            `json:"collection"`
	Field      string            `json:"field,omitempty"`
	MissingIn  []string          `json:"missingIn,omitempty"`
	Types      map[string]string `json:"types,omitempty"` // set when types differ
}

SchemaDrift is one collection or field that is not the same across all instances of a family.

type SchemaReconciliation

type SchemaReconciliation struct {
	Instances []string `json:"instances"`
	// Groups lists instances sharing one keys-level (else types-level)
	// fingerprint, largest group first: one group means no drift.
	Groups []FingerprintGroup `json:"groups"`
	Drift  []SchemaDrift      `json:"drift"`
}

SchemaReconciliation is the N-way schema comparison of one family.

func ReconcileSchemas

func ReconcileSchemas(catalogs []Catalog) SchemaReconciliation

ReconcileSchemas compares N catalogs of one family in one pass over the union of their collections and fields. Majority is never treated as correct: drift lists where each element is missing or typed differently.

type Thresholds

type Thresholds struct {
	CollectionOverlap float64 `json:"collectionOverlap"`
	FieldOverlap      float64 `json:"fieldOverlap"`
	MinCollections    int     `json:"minCollections"`
	IdentifierMatches int     `json:"identifierMatches"`
}

Thresholds are configuration; Default carries the spec's defaults.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL