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 ¶
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.
type Catalog ¶
type Catalog struct {
ID string `json:"id"`
Collections []Collection `json:"collections"`
}
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 {
UnionCollections int `json:"unionCollections"`
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 ¶
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 ¶
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.