schema

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ColumnIdentityTypeAlways    = "a"
	ColumnIdentityTypeByDefault = "d"
)

Variables

This section is empty.

Functions

func EscapeIdentifier

func EscapeIdentifier(name string) string

func FQEscapedColumnName added in v0.2.0

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) GetName

func (c Column) GetName() string

func (Column) IsCollated

func (c Column) IsCollated() bool

type ColumnIdentity added in v0.8.0

type ColumnIdentity struct {
	Type       ColumnIdentityType
	MinValue   int64
	MaxValue   int64
	StartValue int64
	Increment  int64
	CacheSize  int64
	Cycle      bool
}

type ColumnIdentityType added in v0.8.0

type ColumnIdentityType string

type Enum added in v0.7.0

type Enum struct {
	SchemaQualifiedName
	Labels []string
}

type Extension added in v0.3.0

type Extension struct {
	SchemaQualifiedName
	Version string
}

type ForeignKeyConstraint added in v0.3.0

type ForeignKeyConstraint struct {
	EscapedName   string
	OwningTable   SchemaQualifiedName
	ForeignTable  SchemaQualifiedName
	ConstraintDef string
	IsValid       bool
}

func (ForeignKeyConstraint) GetName added in v0.3.0

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 added in v0.6.0

type GetSchemaOpt func(*getSchemaOptions)

func WithExcludeSchemas added in v0.6.0

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 WithIncludeSchemas added in v0.6.0

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) GetName

func (i Index) GetName() string

func (Index) GetSchemaQualifiedName added in v0.6.0

func (i Index) GetSchemaQualifiedName() SchemaQualifiedName

func (Index) IsPk

func (i Index) IsPk() bool

type IndexConstraint added in v0.4.0

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 added in v0.4.0

type IndexConstraintType string
const (
	PkIndexConstraintType IndexConstraintType = "p"
)

type NamedSchema added in v0.6.0

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 added in v0.6.0

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 Policy added in v0.7.0

type Policy struct {
	EscapedName     string
	IsPermissive    bool
	AppliesTo       []string
	Cmd             PolicyCmd
	CheckExpression string
	UsingExpression string
	// Columns are the columns that the policy applies to.
	Columns []string
}

func (Policy) GetName added in v0.7.0

func (p Policy) GetName() string

type PolicyCmd added in v0.7.0

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

const (
	SelectPolicyCmd PolicyCmd = "r"
	InsertPolicyCmd PolicyCmd = "a"
	UpdatePolicyCmd PolicyCmd = "w"
	DeletePolicyCmd PolicyCmd = "d"
	AllPolicyCmd    PolicyCmd = "*"
)

type ReplicaIdentity added in v0.4.0

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
	Triggers              []Trigger
}

Schema is the schema of the database, not just a single Postgres schema.

func GetSchema added in v0.6.0

func GetSchema(ctx context.Context, db queries.DBTX, opts ...GetSchemaOpt) (Schema, error)

GetSchema fetches the database schema. It is a non-atomic operation.

func (Schema) Hash

func (s Schema) Hash() (string, error)

func (Schema) Normalize

func (s Schema) Normalize() Schema

Normalize normalizes the schema (alphabetically sorts tables and columns in tables) Useful for hashing and testing

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 added in v0.2.0

type Sequence struct {
	SchemaQualifiedName
	Owner      *SequenceOwner
	Type       string
	StartValue int64
	Increment  int64
	MaxValue   int64
	MinValue   int64
	CacheSize  int64
	Cycle      bool
}

type SequenceOwner added in v0.2.0

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

func (t Table) IsPartition() bool

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

func (t Table) IsPartitioned() bool

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
}

func (Trigger) GetName

func (t Trigger) GetName() string

Jump to

Keyboard shortcuts

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