schema

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSchema = "public"

Variables

This section is empty.

Functions

func RefFollowup

func RefFollowup(name string) string

func Sort

func Sort[K constraints.Ordered, T Sortable[K]](nodes []T) []T

Sort a a slice in-place by name, and then return a new slice that is sorted in dependency order. The initial sort makes the dependency-ordered result stable regardless of the initial ordering of the input slice.

Types

type Column

type Column struct {
	// These fields are read from the database
	BelongsTo        int // The name of the [Table] or [View] that owns this Column.
	Number           int // The position of this Column within its owners full set of columns. The first column is number 0.
	Name             string
	NotNull          bool
	DataType         string // The Postgres data type of this column
	IsIdentity       bool
	IsIdentityAlways bool           // If True, then IsIdentity is also True.
	IsGenerated      bool           // If True, then IsIdentity is False and IsIdentityAlways is False.
	Collation        sql.NullString // The collation rules for this column, if any.
	DefaultDef       sql.NullString // The default definition for this column, if any.
	Comment          sql.NullString // The comment on this column, if any.
	// These fields will be populated during Parse()
	Sequence *Sequence // If set, the sequence associated with this column. Usually set in the case of primary keys or IS IDENTITY GENERATED ALWAYS.
}

Column represents a column in a Table or a View, and will never exist "on its own".

type CompoundType

type CompoundType struct {
	OID     int
	Schema  string
	Name    string
	Columns []CompoundTypeColumn
	// contains filtered or unexported fields
}

func LoadCompoundTypes

func LoadCompoundTypes(config Config, db *sql.DB) ([]*CompoundType, error)

func (*CompoundType) AddDependency

func (t *CompoundType) AddDependency(dep string)

func (CompoundType) DependsOn

func (t CompoundType) DependsOn() []string

func (CompoundType) SortKey

func (t CompoundType) SortKey() string

func (CompoundType) String

func (t CompoundType) String() string

type CompoundTypeColumn

type CompoundTypeColumn struct {
	Name string
	Type string
}

func (*CompoundTypeColumn) Scan

func (tc *CompoundTypeColumn) Scan(value any) error

type Config

type Config struct {
	// The name of the schema whose contents should be dumped.
	Schema string `yaml:"name"`
	// The name of the file to which the dump should be written.
	Out string `yaml:"out"`
	// Any explicit dependencies between database objects.
	Dependencies map[string][]string `yaml:"dependencies"`
	// Rules for dumping table data in the form of INSERT statements.
	Data []Data `yaml:"data"`
}

type Constraint

type Constraint struct {
	OID                int
	Schema             string
	Name               string
	TableName          string
	Definition         string
	Type               string
	Index              string
	ForeignTableSchema string
	ForeignTableName   string
	ForeignColumns     []string
	LocalColumns       []string
	IsDeferrable       bool
	InitiallyDeferred  bool
	// contains filtered or unexported fields
}

func LoadConstraints

func LoadConstraints(config Config, db *sql.DB) ([]*Constraint, error)

func (*Constraint) AddDependency

func (c *Constraint) AddDependency(dep string)

func (*Constraint) DependsOn

func (c *Constraint) DependsOn() []string

func (Constraint) SortKey

func (c Constraint) SortKey() string

func (Constraint) String

func (c Constraint) String() string

type DBObject

type DBObject interface {
	SortKey() string
	String() string
	AddDependency(string)
	DependsOn() []string
}

DBObject is an interface satisifed by Table, View, Enum, etc. and allows for easier interaction during the sorting and printing parts of the code.

type Data

type Data struct {
	Schema  string   `yaml:"schema"`
	Name    string   `yaml:"name"`
	Columns []string `yaml:"columns"`
	OrderBy string   `yaml:"orderBy"`
	Rows    []any
}

func LoadData

func LoadData(config Config, db *sql.DB) ([]*Data, error)

func (Data) DependsOn added in v0.0.3

func (Data) DependsOn() []string

func (Data) SortKey added in v0.0.3

func (d Data) SortKey() string

func (Data) String

func (d Data) String() string

type Dependency

type Dependency struct {
	Object    Object
	DependsOn Object
}

func LoadDependencies

func LoadDependencies(config Config, db *sql.DB) ([]*Dependency, error)

type Domain

type Domain struct {
	Schema           string
	Name             string
	UnderlyingType   string
	NotNull          bool
	Collation        sql.NullString
	Default          sql.NullString
	CheckConstraints sql.NullString
	// contains filtered or unexported fields
}

func LoadDomains

func LoadDomains(config Config, db *sql.DB) ([]*Domain, error)

func (*Domain) AddDependency

func (d *Domain) AddDependency(dep string)

func (Domain) DependsOn

func (d Domain) DependsOn() []string

func (Domain) SortKey

func (d Domain) SortKey() string

func (Domain) String

func (d Domain) String() string

type Enum

type Enum struct {
	OID          int
	Schema       string
	Name         string
	InternalName string
	Description  sql.NullString
	Size         string
	Elements     []string
	// contains filtered or unexported fields
}

func LoadEnums

func LoadEnums(config Config, db *sql.DB) ([]*Enum, error)

func (*Enum) AddDependency

func (e *Enum) AddDependency(dep string)

func (Enum) DependsOn

func (e Enum) DependsOn() []string

func (Enum) SortKey

func (e Enum) SortKey() string

func (Enum) String

func (e Enum) String() string

type Extension

type Extension struct {
	OID         int
	Schema      string
	Name        string
	Version     string
	Description string
	// contains filtered or unexported fields
}

func LoadExtensions

func LoadExtensions(config Config, db *sql.DB) ([]*Extension, error)

func (*Extension) AddDependency

func (e *Extension) AddDependency(dep string)

func (Extension) DependsOn

func (e Extension) DependsOn() []string

func (Extension) SortKey

func (e Extension) SortKey() string

func (Extension) String

func (e Extension) String() string

type Followup

type Followup struct {
	Name string
	SQL  string
	// contains filtered or unexported fields
}

func (*Followup) AddDependency

func (f *Followup) AddDependency(dep string)

func (Followup) DependsOn

func (f Followup) DependsOn() []string

func (Followup) SortKey

func (f Followup) SortKey() string

func (Followup) String

func (f Followup) String() string

type Function

type Function struct {
	OID           int
	Schema        string
	Name          string
	Language      string
	Kind          string
	Volatility    string
	Parallel      string
	Security      string
	ResultType    string
	ArgumentTypes string
	Definition    string
	// contains filtered or unexported fields
}

func LoadFunctions

func LoadFunctions(config Config, db *sql.DB) ([]*Function, error)

func (*Function) AddDependency

func (f *Function) AddDependency(dep string)

func (Function) DependsOn

func (f Function) DependsOn() []string

func (Function) SortKey

func (f Function) SortKey() string

func (Function) String

func (f Function) String() string

type Index

type Index struct {
	OID                 int
	Schema              string
	TableName           string
	Name                string
	Definition          string
	IndexColumns        []string
	KeyOptions          string
	TotalColumnCount    int
	KeyColumnCount      int
	NumAtt              int
	IncludedColumnCount int
	IsUnique            bool
	IsPrimaryKey        bool
	IsExclusion         bool
	IsImmediate         bool
	IsClustered         bool
	KeyCollations       string
	KeyExpressions      sql.NullString
	PartialPredicate    sql.NullString
	Algorithm           string
	KeyColumns          []string
	IncludedColumns     []string
	// contains filtered or unexported fields
}

func LoadIndexes

func LoadIndexes(config Config, db *sql.DB) ([]*Index, error)

func (*Index) AddDependency

func (i *Index) AddDependency(dep string)

func (Index) DependsOn

func (i Index) DependsOn() []string

func (Index) SortKey

func (i Index) SortKey() string

func (Index) String

func (i Index) String() string

type Object

type Object struct {
	OID    int
	Schema string
	Name   string
	Kind   string
}

type Schema

type Schema struct {
	// Database objects that can be dumped.
	Extensions    []*Extension
	Domains       []*Domain
	CompoundTypes []*CompoundType
	Enums         []*Enum
	Functions     []*Function
	Tables        []*Table
	Views         []*View
	Sequences     []*Sequence
	Indexes       []*Index
	Constraints   []*Constraint
	Triggers      []*Trigger
	Data          []*Data
	// Metadata that isn't explicitly dumped.
	Config       Config
	Dependencies []*Dependency
}

func Parse

func Parse(config Config, db *sql.DB) (*Schema, error)

func (*Schema) Load

func (s *Schema) Load(db *sql.DB) error

Load queries the database and populates the slices of database objects. It does not assign any additional dependencies between the objects.

func (*Schema) ObjectsByName

func (s *Schema) ObjectsByName() map[string]DBObject

ObjectsByName returns a map of all the database objects represented as the DBObject interface. This representation allows assigning dependencies between them, printing them, and sorting them.

func (*Schema) Sort

func (s *Schema) Sort()

Sort orders each type of database objects into creation order. Does not perform a global ordering on the different types.

func (*Schema) String

func (s *Schema) String() string

String returns the contents of schema file that can be applied with `psql` to create a database with the same schema as the one that is parsed. Objects are grouped when possible, and ordered such that when an object is created all of its dependencies are guaranteed to exist.

This schema file is

  • usable: can `psql $NEW -f schema.sql` to create a new database with the same schema.
  • diffable: if there are migrations in different PRs/branches that will conflict with each other, diffing the generated schema.sql files from each branch should result in a merge conflict that cannot be automatically resolved.
  • roundtrippable: dumping `pgmigrate dump --database $NEW > schema.sql` will result in 0 changes.
  • customizable: you can include tables to dump values from (for enum tables) and you can explicitly add dependencies between objects that will be respected during the dump, to work around faulty dependency detection.

type Sequence

type Sequence struct {
	OID              int
	Schema           string
	Name             string
	DataType         string
	StartValue       int
	MinValue         int
	MaxValue         int
	IncrementBy      int
	Cache            int
	Cycle            bool
	TableName        sql.NullString
	ColumnName       sql.NullString
	IsIdentity       bool
	IsIdentityAlways bool
	// contains filtered or unexported fields
}

func LoadSequences

func LoadSequences(config Config, db *sql.DB) ([]*Sequence, error)

func (*Sequence) AddDependency

func (s *Sequence) AddDependency(dep string)

func (Sequence) DependsOn

func (s Sequence) DependsOn() []string

func (Sequence) Followup

func (s Sequence) Followup() *Followup

func (Sequence) SortKey

func (s Sequence) SortKey() string

func (Sequence) String

func (s Sequence) String() string

type Sortable

type Sortable[K constraints.Ordered] interface {
	SortKey() K     // sorted by this, ascending
	DependsOn() []K // what they depend on
}

type Table

type Table struct {
	OID          int
	Schema       string
	Name         string
	Comment      sql.NullString
	Columns      []*Column
	Dependencies []string
	Indexes      []*Index
	Constraints  []*Constraint
	Sequences    []*Sequence
	Triggers     []*Trigger
}

func LoadTables

func LoadTables(config Config, db *sql.DB) ([]*Table, error)

func (*Table) AddDependency

func (t *Table) AddDependency(dep string)

func (Table) DependsOn

func (t Table) DependsOn() []string

func (Table) SortKey

func (t Table) SortKey() string

func (Table) String

func (t Table) String() string

type Trigger

type Trigger struct {
	OID        int
	Schema     string
	Name       string
	TableName  string
	Definition string
	ProcSchema string
	ProcName   string
	Enabled    string
	// contains filtered or unexported fields
}

func LoadTriggers

func LoadTriggers(config Config, db *sql.DB) ([]*Trigger, error)

func (*Trigger) AddDependency

func (t *Trigger) AddDependency(dep string)

func (Trigger) DependsOn

func (t Trigger) DependsOn() []string

func (Trigger) SortKey

func (t Trigger) SortKey() string

func (Trigger) String

func (t Trigger) String() string

type View

type View struct {
	OID            int
	Schema         string
	Name           string
	Definition     string
	Comment        sql.NullString
	IsMaterialized bool
	Columns        []Column
	Dependencies   []string
}

func LoadViews

func LoadViews(config Config, db *sql.DB) ([]*View, error)

func (*View) AddDependency

func (v *View) AddDependency(dep string)

func (View) DependsOn

func (v View) DependsOn() []string

func (View) SortKey

func (v View) SortKey() string

func (View) String

func (v View) String() string

Jump to

Keyboard shortcuts

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