schema

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package schema parses, inspects, and compares database schema definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pluralize

func Pluralize(name string) string

func QualifyPackage

func QualifyPackage(pkgPath string) string

func RegisterCustomType added in v0.1.1

func RegisterCustomType(name string, typ Type)

func RegisterCustomTypeResolver added in v0.1.1

func RegisterCustomTypeResolver(resolver CustomTypeResolver)

func SaveSnapshot

func SaveSnapshot(path string, snap *Snapshot) error

func StructFieldIndexMap added in v0.1.1

func StructFieldIndexMap(t reflect.Type) map[string][]int

func ToSnakeCase

func ToSnakeCase(in string) string

Types

type Builder

type Builder struct {
	Root string
}

func NewBuilder

func NewBuilder(root string) *Builder

NewBuilder returns a schema builder rooted at a source tree.

func (*Builder) Build

func (b *Builder) Build(ctx context.Context) (*Schema, error)

Build parses source files and returns the discovered schema.

type Column

type Column struct {
	Name          string
	GoName        string
	Type          Type
	Nullable      bool
	PrimaryKey    bool
	Unique        bool
	Identity      bool
	AutoIncrement bool
	Default       string
	Generated     string
	SoftDelete    bool
	CreatedAt     bool
	UpdatedAt     bool
	DeletedAt     bool
	CreatedBy     bool
	UpdatedBy     bool
	DeletedBy     bool
	Version       bool
	Scope         ScopeKind
	Tags          map[string]string
}

Column describes a database column and its inferred metadata.

func ColumnsFromType added in v0.1.1

func ColumnsFromType(t reflect.Type) []*Column

func (Column) IsAuditField

func (c Column) IsAuditField() bool

func (Column) IsScopeField

func (c Column) IsScopeField() bool

func (Column) TagsString

func (c Column) TagsString() string

type Constraint

type Constraint struct {
	Name              string
	Kind              ConstraintKind
	Columns           []string
	ReferencedTable   string
	ReferencedColumns []string
	OnDelete          string
	OnUpdate          string
	Expression        string
	Metadata          map[string]string
}

Constraint describes a table constraint.

type ConstraintKind

type ConstraintKind string

ConstraintKind identifies a constraint category.

const (
	ConstraintPrimaryKey ConstraintKind = "primary_key"
	ConstraintUnique     ConstraintKind = "unique"
	ConstraintForeignKey ConstraintKind = "foreign_key"
	ConstraintCheck      ConstraintKind = "check"
)

type CustomTypeResolver added in v0.1.1

type CustomTypeResolver func(reflect.Type) (Type, bool)

type Diff

type Diff struct {
	Operations []Operation
}

func Compare

func Compare(expected, actual *Schema) (*Diff, error)

func (Diff) Empty

func (d Diff) Empty() bool

func (Diff) String

func (d Diff) String() string

type DriftReport added in v0.1.1

type DriftReport struct {
	Expected     *Schema
	Snapshot     *Snapshot
	Actual       *Schema
	ExpectedDiff *Diff
	SnapshotDiff *Diff
}

func DetectDrift added in v0.1.1

func DetectDrift(expected, actual *Schema) (*DriftReport, error)

DetectDrift compares two schemas and returns a drift report.

func DetectDriftFromSource added in v0.1.1

func DetectDriftFromSource(ctx context.Context, root string, inspector Inspector, db *sql.DB, schemaName, snapshotPath string) (*DriftReport, error)

DetectDriftFromSource builds the expected schema from source and compares it with the live database.

func DetectDriftWithSnapshot added in v0.1.1

func DetectDriftWithSnapshot(expected *Schema, snapshot *Snapshot, actual *Schema) (*DriftReport, error)

DetectDriftWithSnapshot compares a live schema against an expected schema and snapshot.

func (*DriftReport) Clean added in v0.1.1

func (r *DriftReport) Clean() bool

Clean reports whether no drift was detected.

func (*DriftReport) HasDrift added in v0.1.1

func (r *DriftReport) HasDrift() bool

HasDrift reports whether the live schema differs from the expected schema.

func (*DriftReport) HasSnapshotDrift added in v0.1.1

func (r *DriftReport) HasSnapshotDrift() bool

HasSnapshotDrift reports whether the live schema differs from the snapshot.

type EnumType

type EnumType struct {
	Name     string
	Values   []string
	Metadata map[string]string
}

type Index

type Index struct {
	Name       string
	Columns    []string
	Unique     bool
	Expression string
	Where      string
	Method     string
	Metadata   map[string]string
}

Index describes a database index definition.

type Inspector

type Inspector interface {
	Inspect(ctx context.Context, db *sql.DB, schemaName string) (*Schema, error)
}

type Operation

type Operation struct {
	Kind       OperationKind
	Table      string
	TableDef   *Table
	Column     *Column
	Previous   *Column
	Index      *Index
	Constraint *Constraint
}

type OperationKind

type OperationKind string
const (
	OpCreateTable      OperationKind = "create_table"
	OpDropTable        OperationKind = "drop_table"
	OpAddColumn        OperationKind = "add_column"
	OpDropColumn       OperationKind = "drop_column"
	OpAlterColumn      OperationKind = "alter_column"
	OpCreateIndex      OperationKind = "create_index"
	OpDropIndex        OperationKind = "drop_index"
	OpCreateConstraint OperationKind = "create_constraint"
	OpDropConstraint   OperationKind = "drop_constraint"
)

type PostgresInspector

type PostgresInspector struct{}

PostgresInspector inspects PostgreSQL metadata into a Schema.

func (PostgresInspector) Inspect

func (PostgresInspector) Inspect(ctx context.Context, db *sql.DB, schemaName string) (*Schema, error)

Inspect loads PostgreSQL tables, columns, and indexes for a schema.

type Schema

type Schema struct {
	Name    string
	Version int
	Tables  []*Table
	Enums   []*EnumType
	Views   []*View
}

Schema is the in-memory representation of discovered database structure.

func (*Schema) Clone

func (s *Schema) Clone() *Schema

func (*Schema) Sort

func (s *Schema) Sort()

func (*Schema) Validate

func (s *Schema) Validate() error

type ScopeKind

type ScopeKind string

ScopeKind identifies an access-control scope attached to a column.

const (
	ScopeNone         ScopeKind = ""
	ScopeCompany      ScopeKind = "company"
	ScopeTenant       ScopeKind = "tenant"
	ScopeOrganization ScopeKind = "organization"
	ScopeWorkspace    ScopeKind = "workspace"
	ScopeWarehouse    ScopeKind = "warehouse"
	ScopeUser         ScopeKind = "user"
)

type Snapshot

type Snapshot struct {
	GeneratedAt time.Time `json:"generated_at"`
	Schema      *Schema   `json:"schema"`
}

func LoadSnapshot

func LoadSnapshot(path string) (*Snapshot, error)

func (*Snapshot) Clone added in v0.1.1

func (s *Snapshot) Clone() *Snapshot

type Table

type Table struct {
	Name        string
	GoTypeName  string
	PackageName string
	Columns     []*Column
	Indexes     []*Index
	Constraints []*Constraint
	Comments    []string
	Metadata    map[string]string
}

Table describes a database table or view backing structure.

type Type

type Type struct {
	Name       string
	Kind       TypeKind
	Length     int
	Precision  int
	Scale      int
	Nullable   bool
	ArrayOf    *Type
	EnumValues []string
	Metadata   map[string]string
}

Type describes a database type with optional metadata.

func ResolveCustomType added in v0.1.1

func ResolveCustomType(t reflect.Type) (Type, bool)

func ResolveCustomTypeName added in v0.1.1

func ResolveCustomTypeName(name string) (Type, bool)

ResolveCustomTypeName looks up a registered custom schema type by name.

type TypeKind

type TypeKind string

TypeKind identifies the broad kind of a column type.

const (
	TypeUnknown TypeKind = "unknown"
	TypeBool    TypeKind = "bool"
	TypeInt     TypeKind = "int"
	TypeFloat   TypeKind = "float"
	TypeString  TypeKind = "string"
	TypeBytes   TypeKind = "bytes"
	TypeTime    TypeKind = "time"
	TypeUUID    TypeKind = "uuid"
	TypeJSON    TypeKind = "json"
	TypeArray   TypeKind = "array"
	TypeEnum    TypeKind = "enum"
	TypeCustom  TypeKind = "custom"
)

type View

type View struct {
	Name         string
	SQL          string
	Materialized bool
	Metadata     map[string]string
}

View describes a database view definition.

Jump to

Keyboard shortcuts

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