Documentation
¶
Index ¶
- Constants
- Variables
- func CollationsEqual(a, b SchemaQualifiedName) bool
- func EscapeIdentifier(name string) string
- func FQEscapedColumnName(table SchemaQualifiedName, columnName string) string
- type CheckConstraint
- type Column
- type ColumnIdentity
- type ColumnIdentityType
- type Enum
- type Extension
- type ForeignKeyConstraint
- type Function
- type GetIndexDefStatement
- type GetSchemaOpt
- type GetTriggerDefStatement
- type Index
- type IndexConstraint
- type IndexConstraintType
- type NamedSchema
- type Object
- type ObjectType
- type Policy
- type PolicyCmd
- type Procedure
- type ReplicaIdentity
- type Schema
- type SchemaQualifiedName
- type Sequence
- type SequenceOwner
- type Table
- type Trigger
Constants ¶
const ( ColumnIdentityTypeAlways = "a" ColumnIdentityTypeByDefault = "d" )
Variables ¶
var AllObjectTypes = []ObjectType{ ObjectTypeNamedSchema, ObjectTypeExtension, ObjectTypeEnum, ObjectTypeTable, ObjectTypeIndex, ObjectTypeForeignKeyConstraint, ObjectTypeSequence, ObjectTypeFunction, ObjectTypeProcedure, ObjectTypeTrigger, ObjectTypeCheckConstraint, ObjectTypePolicy, ObjectTypeReplicaIdentity, ObjectTypePartitioning, }
AllObjectTypes is the default: everything. Callers that pass no WithIncludeObjectTypes option get exactly the behavior they had before the option existed.
Keep this in sync with the constants above; TestAllObjectTypesIsExhaustive guards it.
Functions ¶
func CollationsEqual ¶ added in v0.9.0
func CollationsEqual(a, b SchemaQualifiedName) bool
CollationsEqual compares two collations, treating "unset" and the explicit database default pg_catalog."default" as equal — Postgres does.
buildTable already canonicalizes the default to the empty name, so this is belt and braces for Schemas constructed by any other path (tests, callers of GetSchemaHash). Without it, the two spellings compare unequal and produce an ALTER ... SET DATA TYPE that changes nothing: information_schema keeps reporting collation_name as NULL because attcollation is 100 either way, so the same statement is proposed on every subsequent run, forever.
func EscapeIdentifier ¶
func FQEscapedColumnName ¶
func FQEscapedColumnName(table SchemaQualifiedName, columnName string) string
FQEscapedColumnName builds a fully-qualified escape column name
Types ¶
type CheckConstraint ¶
type CheckConstraint struct {
Name string
// KeyColumns are the columns that the constraint applies to
KeyColumns []string
Expression string
IsValid bool
IsInheritable bool
DependsOnFunctions []SchemaQualifiedName
}
func (CheckConstraint) GetName ¶
func (c CheckConstraint) GetName() string
type Column ¶
type Column struct {
Name string
Type string
Collation SchemaQualifiedName
// If the column has a default value, this will be a SQL string representing that value.
// Examples:
// ”::text
// CURRENT_TIMESTAMP
// If empty, indicates that there is no default value.
Default string
IsNullable bool
// Size is the number of bytes required to store the value.
// It is used for data-packing purposes
Size int
Identity *ColumnIdentity
}
func (Column) IsCollated ¶
type ColumnIdentity ¶
type ColumnIdentityType ¶
type ColumnIdentityType string
type Enum ¶
type Enum struct {
SchemaQualifiedName
Labels []string
}
type Extension ¶
type Extension struct {
SchemaQualifiedName
Version string
}
type ForeignKeyConstraint ¶
type ForeignKeyConstraint struct {
EscapedName string
OwningTable SchemaQualifiedName
ForeignTable SchemaQualifiedName
ConstraintDef string
IsValid bool
}
func (ForeignKeyConstraint) GetName ¶
func (f ForeignKeyConstraint) GetName() string
type Function ¶
type Function struct {
SchemaQualifiedName
// FunctionDef is the statement required to completely (re)create
// the function, as returned by `pg_get_functiondef`. It is a CREATE OR REPLACE
// statement
FunctionDef string
// Language is the language of the function. This is relevant in determining if we
// can track the dependencies of the function (or not)
Language string
DependsOnFunctions []SchemaQualifiedName
}
type GetIndexDefStatement ¶
type GetIndexDefStatement string
GetIndexDefStatement is the output of pg_getindexdef. It is a `CREATE INDEX` statement that will re-create the index. This statement does not contain `CONCURRENTLY`. For unique indexes, it does contain `UNIQUE` For partitioned tables, it does contain `ONLY`
func (GetIndexDefStatement) ToCreateIndexConcurrently ¶
func (i GetIndexDefStatement) ToCreateIndexConcurrently() (string, error)
type GetSchemaOpt ¶
type GetSchemaOpt func(*getSchemaOptions)
func WithExcludeSchemas ¶
func WithExcludeSchemas(schemas ...string) GetSchemaOpt
WithExcludeSchemas filters the schema to exclude the given schemas. This unions with any schemas that are already excluded via WithExcludeSchemas. If empty, then no schemas are excluded.
func WithIncludeObjectTypes ¶ added in v0.9.0
func WithIncludeObjectTypes(types ...ObjectType) GetSchemaOpt
WithIncludeObjectTypes restricts the fetched schema to the given object types. Object types that are not listed are omitted from the returned Schema, as are the attributes derived from them (see Schema.project for the exact rules — excluding sequences, for example, also strips nextval() column defaults and identity columns, because those are sequence-derived state).
Defaults to AllObjectTypes. Calling it with no arguments is an error rather than "include nothing", so a bug that drops the argument list can't silently produce a plan that drops the entire schema.
As with WithIncludeSchemas, no dependency validation is performed: excluding a type that a retained object depends on (an enum used as a column type, a function used by a retained trigger) leaves the retained object referring to something that isn't in the schema. Choose sets that are self-consistent.
func WithIncludeSchemas ¶
func WithIncludeSchemas(schemas ...string) GetSchemaOpt
WithIncludeSchemas filters the schema to only include the given schemas. This unions with any schemas that are already included via WithIncludeSchemas. If empty, then all schemas are included.
type GetTriggerDefStatement ¶
type GetTriggerDefStatement string
GetTriggerDefStatement is the output of pg_get_triggerdef. It is a `CREATE TRIGGER` statement that will create the trigger. This statement does not contain `OR REPLACE`
func (GetTriggerDefStatement) ToCreateOrReplace ¶
func (g GetTriggerDefStatement) ToCreateOrReplace() (string, error)
type Index ¶
type Index struct {
// Name is the name of the index. We don't store the schema because the schema is just the schema of the table.
// Referencing the name is an anti-pattern because it is not qualified. Use should use GetSchemaQualifiedName instead.
Name string
OwningTable SchemaQualifiedName
Columns []string
IsInvalid bool
IsUnique bool
Constraint *IndexConstraint
// GetIndexDefStmt is the output of pg_getindexdef
GetIndexDefStmt GetIndexDefStatement
ParentIdx *SchemaQualifiedName
}
func (Index) GetSchemaQualifiedName ¶
func (i Index) GetSchemaQualifiedName() SchemaQualifiedName
type IndexConstraint ¶
type IndexConstraint struct {
Type IndexConstraintType
EscapedConstraintName string
ConstraintDef string
IsLocal bool
}
IndexConstraint informally represents a constraint that is always 1:1 with an index, i.e., primary and unique constraints. It's easiest to just treat these like a property of the index rather than a separate entity
type IndexConstraintType ¶
type IndexConstraintType string
const (
PkIndexConstraintType IndexConstraintType = "p"
)
type NamedSchema ¶
type NamedSchema struct {
Name string
}
NamedSchema represents a schema in the database. We call it NamedSchema to distinguish it from the Postgres Database schema
func (NamedSchema) GetName ¶
func (n NamedSchema) GetName() string
type Object ¶
type Object interface {
// GetName is used to identify the old and new versions of a schema object between the old and new schemas
// If the name is not present in the old schema objects list, then it is added
// If the name is not present in the new schemas objects list, then it is removed
// Otherwise, it has persisted across two schemas and is possibly altered
//
// GetName should be qualified with the schema name.
GetName() string
}
Object represents a resource in a schema (table, column, index...)
type ObjectType ¶ added in v0.9.0
type ObjectType string
ObjectType identifies a class of schema object that GetSchema can be asked to include. It exists so a caller whose "desired" schema only ever describes a subset of Postgres (e.g. tables, indexes and foreign keys, and nothing else) can project the live database down to that same subset. Without it, every object class the caller doesn't model shows up on one side of a diff only, and the plan proposes dropping it.
The option is an ALLOWLIST rather than a denylist on purpose: a class added to Schema in the future must be inert for existing callers until they opt in. A denylist would silently start emitting DROP statements for the new class.
const ( ObjectTypeNamedSchema ObjectType = "named_schema" ObjectTypeExtension ObjectType = "extension" ObjectTypeEnum ObjectType = "enum" ObjectTypeTable ObjectType = "table" ObjectTypeIndex ObjectType = "index" ObjectTypeForeignKeyConstraint ObjectType = "foreign_key_constraint" ObjectTypeSequence ObjectType = "sequence" ObjectTypeFunction ObjectType = "function" ObjectTypeProcedure ObjectType = "procedure" ObjectTypeTrigger ObjectType = "trigger" ObjectTypeCheckConstraint ObjectType = "check_constraint" // ObjectTypePolicy covers row-level-security policies AND the table's RLS // enabled/forced flags: a caller that doesn't model policies doesn't model // whether RLS is on. ObjectTypePolicy ObjectType = "policy" ObjectTypeReplicaIdentity ObjectType = "replica_identity" ObjectTypePartitioning ObjectType = "partitioning" )
type Policy ¶
type PolicyCmd ¶
type PolicyCmd string
PolicyCmd represents the polcmd value in the pg_policy system catalog. See docs for possible values: https://www.postgresql.org/docs/current/catalog-pg-policy.html#CATALOG-PG-POLICY
type Procedure ¶
type Procedure struct {
SchemaQualifiedName
// Def is the statement required to completely (re)create
// the procedure, as returned by `pg_get_functiondef`. It is a CREATE OR REPLACE
// statement.
Def string
}
type ReplicaIdentity ¶
type ReplicaIdentity string
const ( ReplicaIdentityDefault ReplicaIdentity = "d" ReplicaIdentityNothing ReplicaIdentity = "n" ReplicaIdentityFull ReplicaIdentity = "f" ReplicaIdentityIndex ReplicaIdentity = "i" )
type Schema ¶
type Schema struct {
NamedSchemas []NamedSchema
Extensions []Extension
Enums []Enum
Tables []Table
Indexes []Index
ForeignKeyConstraints []ForeignKeyConstraint
Sequences []Sequence
Functions []Function
Procedures []Procedure
Triggers []Trigger
}
Schema is the schema of the database, not just a single Postgres schema.
type SchemaQualifiedName ¶
type SchemaQualifiedName struct {
SchemaName string
// EscapedName is the name of the object. It should already be escaped
// We take an escaped name because there are weird exceptions, like functions, where we can't just
// surround the name in quotes
EscapedName string
}
SchemaQualifiedName represents a schema object name scoped within a schema
func (SchemaQualifiedName) GetFQEscapedName ¶
func (o SchemaQualifiedName) GetFQEscapedName() string
GetFQEscapedName gets the fully-qualified, escaped name of the schema object, including the schema name
func (SchemaQualifiedName) GetName ¶
func (o SchemaQualifiedName) GetName() string
func (SchemaQualifiedName) IsEmpty ¶
func (o SchemaQualifiedName) IsEmpty() bool
type Sequence ¶
type Sequence struct {
SchemaQualifiedName
Owner *SequenceOwner
Type string
StartValue int64
Increment int64
MaxValue int64
MinValue int64
CacheSize int64
Cycle bool
}
type SequenceOwner ¶
type SequenceOwner struct {
TableName SchemaQualifiedName
ColumnName string
}
SequenceOwner represents the owner of a sequence.
type Table ¶
type Table struct {
SchemaQualifiedName
Columns []Column
CheckConstraints []CheckConstraint
Policies []Policy
ReplicaIdentity ReplicaIdentity
RLSEnabled bool
RLSForced bool
// PartitionKeyDef is the output of Pg function pg_get_partkeydef:
// PARTITION BY $PartitionKeyDef
// If empty, then the table is not partitioned
PartitionKeyDef string
ParentTable *SchemaQualifiedName
ForValues string
}
func (Table) IsPartition ¶
IsPartition returns whether the table is a partition. It represents a mismatch in modeling because the ForValues and ParentTable are stored separately. Instead, the fields should be stored under the same struct as a nilable pointer, and this function should be deleted.
func (Table) IsPartitioned ¶
type Trigger ¶
type Trigger struct {
EscapedName string
OwningTable SchemaQualifiedName
Function SchemaQualifiedName
// GetTriggerDefStmt is the statement required to completely (re)create the trigger, as returned
// by pg_get_triggerdef
GetTriggerDefStmt GetTriggerDefStatement
}