Documentation
¶
Overview ¶
Package schema models introspected database table schemas, separates structural facts from data-generation decisions, and translates schemas into mockagen generation specifications.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMissingValues = errors.New("schema: column type requires values")
ErrMissingValues is returned by Render when a column's type requires a value list and none is available. It is not cosmetic: a "Custom List" with no values renders the faker tag "oneof:" with no arguments, which faker rejects with ErrNotEnoughTagArguments, and generateFakes panics on a faker error - so the spec would not produce bad data, it would abort the run.
var ErrUnknownKind = errors.New("schema: unknown kind")
ErrUnknownKind is returned when an envelope specifies a kind other than Schema.
var ErrUnknownType = errors.New("schema: unknown column generator type")
ErrUnknownType is returned by Render when a column names a generator type the catalog does not hold. Emptiness is not the only way to get random strings: generation looks a type up by exact string (mapToFaker[col.Type]), so a typo or a stray space misses the map, leaves the faker tag empty and silently fills the column with random text. Checking membership rather than emptiness is the whole reason the catalog was promoted out of package main.
var ErrUnresolvedType = errors.New("schema: unresolved column generator type")
ErrUnresolvedType is returned by Render when a column has no generator type assigned and Force is not enabled. Prevents silent fallback to random strings when converting introspected schemas into generation specs.
var ErrUnsupportedFormat = errors.New("schema: unsupported file format")
ErrUnsupportedFormat is returned when a file extension is not recognised as either YAML or JSON. Schema files are generated artifacts and support only YAML and JSON (TOML is excluded to avoid nested array complexity).
Functions ¶
func Marshal ¶
Marshal encodes s as formatted YAML or JSON based on ext, which is lowercased first for the reason given on Unmarshal.
func Render ¶
func Render(s Schema, opts RenderOptions) (mockagen.MockagenConfig, error)
Render translates s into a mockagen.MockagenConfig.
Types ¶
type Column ¶
type Column struct {
// Facts (introspected from database catalogs, refreshed on update).
Name string `yaml:"name" json:"name"`
SQLType string `yaml:"sql_type" json:"sql_type"`
MaxLength *int64 `yaml:"max_length,omitempty" json:"max_length,omitempty"`
NumericPrecision *int64 `yaml:"numeric_precision,omitempty" json:"numeric_precision,omitempty"`
NumericScale *int64 `yaml:"numeric_scale,omitempty" json:"numeric_scale,omitempty"`
Nullable bool `yaml:"nullable" json:"nullable"`
PrimaryKey bool `yaml:"primary_key,omitempty" json:"primary_key,omitempty"`
Unique bool `yaml:"unique,omitempty" json:"unique,omitempty"`
Default string `yaml:"default,omitempty" json:"default,omitempty"`
Identity bool `yaml:"identity,omitempty" json:"identity,omitempty"`
EnumValues []string `yaml:"enum_values,omitempty,flow" json:"enum_values,omitempty"`
References *ForeignKey `yaml:"references,omitempty" json:"references,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
Ordinal int `yaml:"ordinal,omitempty" json:"ordinal,omitempty"`
// Decisions (generator configuration chosen by rules or human review).
Type string `yaml:"type" json:"type"`
Values []string `yaml:"values,omitempty,flow" json:"values,omitempty"`
InferredBy string `yaml:"inferred_by,omitempty" json:"inferred_by,omitempty"`
NullPercentage int64 `yaml:"null_percentage,omitempty" json:"null_percentage,omitempty"`
Formula string `yaml:"formula,omitempty" json:"formula,omitempty"`
Min string `yaml:"min,omitempty" json:"min,omitempty"`
Max string `yaml:"max,omitempty" json:"max,omitempty"`
Format string `yaml:"format,omitempty" json:"format,omitempty"`
Blank int64 `yaml:"blank,omitempty" json:"blank,omitempty"`
Decimals int64 `yaml:"decimals,omitempty" json:"decimals,omitempty"`
SelectionStyle string `yaml:"selectionStyle,omitempty" json:"selectionStyle,omitempty"`
Distribution string `yaml:"distribution,omitempty" json:"distribution,omitempty"`
StartPosition int64 `yaml:"start_position,omitempty" json:"startPosition,omitempty"`
EndPosition int64 `yaml:"end_position,omitempty" json:"endPosition,omitempty"`
Alignment string `yaml:"alignment,omitempty" json:"alignment,omitempty"`
PadCharacter rune `yaml:"pad_character,omitempty" json:"padCharacter,omitempty"`
}
Column represents an introspected database column along with generator decisions. Fact fields are refreshed on re-introspect while decision fields are preserved across updates.
type ForeignKey ¶
type ForeignKey struct {
Table string `yaml:"table" json:"table"`
Column string `yaml:"column" json:"column"`
}
ForeignKey captures an outbound foreign key constraint referencing another table and column. Used by inference to guide relationship modeling.
type RenderOptions ¶
type RenderOptions struct {
// Force permits rendering even when one or more columns have unresolved
// generator types.
Force bool
}
RenderOptions controls schema translation into mockagen generation specs.
type Schema ¶
type Schema struct {
Version string `yaml:"version" json:"version"`
Kind string `yaml:"kind" json:"kind"`
Name string `yaml:"name" json:"name"`
Source Source `yaml:"source" json:"source"`
Columns []Column `yaml:"columns" json:"columns"`
}
Schema represents a complete database table schema specification.
func Unmarshal ¶
Unmarshal parses raw schema data in YAML or JSON format determined by ext. The extension is lowercased first: a file named Schema.YAML parses exactly like one named schema.yaml, and rejecting it for its spelling would be an error about nothing.
func (Schema) Render ¶
func (s Schema) Render(opts RenderOptions) (mockagen.MockagenConfig, error)
Render translates s into a mockagen.MockagenConfig, enforcing structural couplings: non-nullable columns have NullPercentage forced to 0, identity columns are omitted, unresolved types fail unless forced, and foreign-key or unique constraints emit warning comments since generation cannot guarantee cross-table or set-wide consistency.
func (Schema) Warnings ¶
Warnings reports the constraints this schema records but generation cannot honour, one line per column, so a caller can show them.
Render also writes these onto each column's Comment, which carries them into a rendered spec file - but nothing reads Comment during generation, so a user who renders and generates in one step would never see them. This is how the schema command surfaces them instead.
type Source ¶
type Source struct {
Dialect string `yaml:"dialect" json:"dialect"`
Database string `yaml:"database,omitempty" json:"database,omitempty"`
Table string `yaml:"table" json:"table"`
CapturedAt time.Time `yaml:"captured_at,omitempty" json:"captured_at,omitempty"`
}
Source records provenance metadata indicating which database engine, database instance, and table the schema was introspected from.