schema

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

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
	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 //
}

func (Column) GetName

func (c Column) GetName() string

func (Column) IsCollated

func (c Column) IsCollated() bool

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
	// TableUnescapedName is a hackaround until we switch over Tables to use fully-qualified, escaped names
	OwningTableUnescapedName string
	ForeignTable             SchemaQualifiedName
	// ForeignTableUnescapedName is hackaround for the same as above
	ForeignTableUnescapedName string
	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 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 {
	TableName string
	Name      string
	Columns   []string
	IsInvalid bool
	IsUnique  bool

	Constraint *IndexConstraint

	// GetIndexDefStmt is the output of pg_getindexdef
	GetIndexDefStmt GetIndexDefStatement

	// ParentIdxName is the name of the parent index if the index is a partition of an index
	ParentIdxName string
}

func (Index) GetName

func (i Index) GetName() string

func (Index) IsPartitionOfIndex

func (i Index) IsPartitionOfIndex() bool

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 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() string
}

Object represents a resource in a schema (table, column, index...)

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 {
	Extensions            []Extension
	Tables                []Table
	Indexes               []Index
	ForeignKeyConstraints []ForeignKeyConstraint
	Sequences             []Sequence
	Functions             []Function
	Triggers              []Trigger
}

func GetPublicSchema

func GetPublicSchema(ctx context.Context, db queries.DBTX) (Schema, error)

GetPublicSchema fetches the "public" 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
	// TableUnescapedName is a hackaround until we switch over Tables to use fully-qualified, escaped names
	TableUnescapedName string
	ColumnName         string
}

SequenceOwner represents the owner of a sequence. Once we remove TableUnescapedName, we can replace it with ColumnIdentifier struct that can also be used in the Column struct

type Table

type Table struct {
	Name             string
	Columns          []Column
	CheckConstraints []CheckConstraint
	ReplicaIdentity  ReplicaIdentity

	// PartitionKeyDef is the output of Pg function pg_get_partkeydef:
	// PARTITION BY $PartitionKeyDef
	// If empty, then the table is not partitioned
	PartitionKeyDef string

	ParentTableName string
	ForValues       string
}

func (Table) GetName

func (t Table) GetName() string

func (Table) IsPartition

func (t Table) IsPartition() bool

func (Table) IsPartitioned

func (t Table) IsPartitioned() bool

type Trigger

type Trigger struct {
	EscapedName string
	OwningTable SchemaQualifiedName
	// OwningTableUnescapedName lets us be backwards compatible with the TableSQLVertexGenerator, which
	// currently uses the unescaped name as the vertex id. This will be removed once the TableSQLVertexGenerator
	// is migrated to use SchemaQualifiedName
	OwningTableUnescapedName string
	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