Documentation
¶
Index ¶
- Constants
- func RefFollowup(name string) string
- func Sort[K constraints.Ordered, T Sortable[K]](nodes []T) []T
- type Column
- type CompoundType
- type CompoundTypeColumn
- type Constraint
- type DBObject
- type Data
- type Dependency
- type Domain
- type DumpConfig
- type Enum
- type Extension
- type Followup
- type Function
- type Index
- type Object
- type Schema
- type Sequence
- type Sortable
- type Table
- type Trigger
- type View
Constants ¶
const DefaultSchema = "public"
Variables ¶
This section is empty.
Functions ¶
func RefFollowup ¶
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 DumpConfig, 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 ¶
func (*CompoundTypeColumn) Scan ¶
func (tc *CompoundTypeColumn) Scan(value any) error
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 DumpConfig, 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"`
// contains filtered or unexported fields
}
func (*Data) AddDependency ¶ added in v0.4.0
type Dependency ¶
func LoadDependencies ¶
func LoadDependencies(config DumpConfig, 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 DumpConfig, db *sql.DB) ([]*Domain, error)
func (*Domain) AddDependency ¶
type DumpConfig ¶ added in v0.4.0
type DumpConfig struct {
// The names of the postgres schemas to include in the dump, defaults to
// "public" if none are specified.
SchemaNames []string `yaml:"schema_names"`
// The name of the file to which the dump should be written. if `-`, then
// the result will be printed to STDOUT.
Out string `yaml:"out"`
// Any explicit dependencies between database objects, described by their
// fully-qualified names e.g., `schema.tablename`.
Dependencies map[string][]string `yaml:"dependencies"`
// Rules for dumping table data in the form of INSERT statements.
Data []Data `yaml:"data"`
// Lines to be written, in order, at the beginning of the generated schema
// dump --- before all the dumped DDL.
Header []string `yaml:"header"`
// --- after all the dumped DDL.
Footer []string `yaml:"footer"`
}
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 (*Enum) AddDependency ¶
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 DumpConfig, db *sql.DB) ([]*Extension, error)
func (*Extension) AddDependency ¶
type Followup ¶
func (*Followup) AddDependency ¶
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 DumpConfig, db *sql.DB) ([]*Function, error)
func (*Function) AddDependency ¶
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 DumpConfig, db *sql.DB) ([]*Index, error)
func (*Index) AddDependency ¶
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.
DumpConfig DumpConfig
Dependencies []*Dependency
}
func (*Schema) Load ¶
Load queries the database and populates the slices of database objects. It does not assign any additional dependencies between the objects.
func (*Schema) ObjectsByName ¶
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 ¶
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 DumpConfig, db *sql.DB) ([]*Sequence, error)
func (*Sequence) AddDependency ¶
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 DumpConfig, db *sql.DB) ([]*Table, error)
func (*Table) AddDependency ¶
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 DumpConfig, db *sql.DB) ([]*Trigger, error)