Skip to main content

schema Package

The schema package defines the core data models used throughout pgtofu. All types are JSON-serializable and represent PostgreSQL database objects.
import "github.com/accented-ai/pgtofu/internal/schema"

Database

The root container for all schema objects.
type Database struct {
    Version              string               `json:"version"`
    DatabaseName         string               `json:"database_name"`
    ExtractedAt          string               `json:"extracted_at,omitempty"`
    Schemas              []Schema             `json:"schemas,omitempty"`
    Extensions           []Extension          `json:"extensions,omitempty"`
    CustomTypes          []CustomType         `json:"custom_types,omitempty"`
    Sequences            []Sequence           `json:"sequences,omitempty"`
    Tables               []Table              `json:"tables,omitempty"`
    Views                []View               `json:"views,omitempty"`
    MaterializedViews    []MaterializedView   `json:"materialized_views,omitempty"`
    Functions            []Function           `json:"functions,omitempty"`
    Triggers             []Trigger            `json:"triggers,omitempty"`
    Hypertables          []Hypertable         `json:"hypertables,omitempty"`
    ContinuousAggregates []ContinuousAggregate `json:"continuous_aggregates,omitempty"`
}

Table

Represents a database table with its columns, constraints, and indexes.
type Table struct {
    Schema            string             `json:"schema"`
    Name              string             `json:"name"`
    Columns           []Column           `json:"columns"`
    Constraints       []Constraint       `json:"constraints,omitempty"`
    Indexes           []Index            `json:"indexes,omitempty"`
    Comment           string             `json:"comment,omitempty"`
    Owner             string             `json:"owner,omitempty"`
    Tablespace        string             `json:"tablespace,omitempty"`
    PartitionStrategy *PartitionStrategy `json:"partition_strategy,omitempty"`
}

// QualifiedName returns "schema.table"
func (t *Table) QualifiedName() string

Example

table := &schema.Table{
    Schema: "public",
    Name:   "users",
    Columns: []schema.Column{
        {
            Name:       "id",
            DataType:   "bigint",
            Position:   1,
            IsNullable: false,
            IsIdentity: true,
            IdentityGeneration: "ALWAYS",
        },
        {
            Name:       "email",
            DataType:   "character varying",
            MaxLength:  intPtr(255),
            Position:   2,
            IsNullable: false,
        },
    },
    Constraints: []schema.Constraint{
        {
            Name:    "users_pkey",
            Type:    "PRIMARY KEY",
            Columns: []string{"id"},
        },
    },
}

Column

Represents a table column with its type and attributes.
type Column struct {
    Name                 string  `json:"name"`
    DataType             string  `json:"data_type"`
    Position             int     `json:"position"`
    IsNullable           bool    `json:"is_nullable"`
    Default              string  `json:"default,omitempty"`
    Comment              string  `json:"comment,omitempty"`
    MaxLength            *int    `json:"max_length,omitempty"`
    Precision            *int    `json:"precision,omitempty"`
    Scale                *int    `json:"scale,omitempty"`
    IsArray              bool    `json:"is_array,omitempty"`
    IsIdentity           bool    `json:"is_identity,omitempty"`
    IdentityGeneration   string  `json:"identity_generation,omitempty"`
    IsGenerated          bool    `json:"is_generated,omitempty"`
    GenerationExpression string  `json:"generation_expression,omitempty"`
}

Column Type Properties

PropertyUsed For
MaxLengthVARCHAR, CHAR types
PrecisionNUMERIC types (total digits)
ScaleNUMERIC types (decimal digits)
IsArrayArray types (e.g., TEXT[])

Constraint

Represents a table constraint.
type Constraint struct {
    Name              string   `json:"name"`
    Type              string   `json:"type"` // PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, EXCLUDE
    Columns           []string `json:"columns,omitempty"`
    ReferencedSchema  string   `json:"referenced_schema,omitempty"`
    ReferencedTable   string   `json:"referenced_table,omitempty"`
    ReferencedColumns []string `json:"referenced_columns,omitempty"`
    OnDelete          string   `json:"on_delete,omitempty"`
    OnUpdate          string   `json:"on_update,omitempty"`
    CheckExpression   string   `json:"check_expression,omitempty"`
    IsDeferrable      bool     `json:"is_deferrable,omitempty"`
    InitiallyDeferred bool     `json:"initially_deferred,omitempty"`
    IndexName         string   `json:"index_name,omitempty"`
}

Constraint Types

const (
    ConstraintPrimaryKey = "PRIMARY KEY"
    ConstraintForeignKey = "FOREIGN KEY"
    ConstraintUnique     = "UNIQUE"
    ConstraintCheck      = "CHECK"
    ConstraintExclude    = "EXCLUDE"
)

Index

Represents a table index.
type Index struct {
    Schema             string            `json:"schema"`
    TableName          string            `json:"table_name"`
    Name               string            `json:"name"`
    Columns            []string          `json:"columns"`
    IncludeColumns     []string          `json:"include_columns,omitempty"`
    Type               string            `json:"type"` // btree, hash, gin, gist, etc.
    Where              string            `json:"where,omitempty"`
    Definition         string            `json:"definition,omitempty"`
    IsUnique           bool              `json:"is_unique,omitempty"`
    IsPrimary          bool              `json:"is_primary,omitempty"`
    IsExcludeConstraint bool             `json:"is_exclude_constraint,omitempty"`
    Tablespace         string            `json:"tablespace,omitempty"`
    StorageParams      map[string]string `json:"storage_params,omitempty"`
}

View

Represents a database view.
type View struct {
    Schema      string `json:"schema"`
    Name        string `json:"name"`
    Definition  string `json:"definition"`
    Comment     string `json:"comment,omitempty"`
    Owner       string `json:"owner,omitempty"`
    CheckOption string `json:"check_option,omitempty"`
    IsUpdatable bool   `json:"is_updatable,omitempty"`
}

type MaterializedView struct {
    Schema     string  `json:"schema"`
    Name       string  `json:"name"`
    Definition string  `json:"definition"`
    Comment    string  `json:"comment,omitempty"`
    Owner      string  `json:"owner,omitempty"`
    Tablespace string  `json:"tablespace,omitempty"`
    Indexes    []Index `json:"indexes,omitempty"`
    WithData   bool    `json:"with_data,omitempty"`
}

Function

Represents a database function.
type Function struct {
    Schema            string   `json:"schema"`
    Name              string   `json:"name"`
    Language          string   `json:"language"`
    Body              string   `json:"body,omitempty"`
    ArgumentTypes     []string `json:"argument_types,omitempty"`
    ArgumentNames     []string `json:"argument_names,omitempty"`
    ArgumentModes     []string `json:"argument_modes,omitempty"`
    ReturnType        string   `json:"return_type"`
    Volatility        string   `json:"volatility,omitempty"` // VOLATILE, STABLE, IMMUTABLE
    Definition        string   `json:"definition,omitempty"`
    IsAggregate       bool     `json:"is_aggregate,omitempty"`
    IsWindow          bool     `json:"is_window,omitempty"`
    IsStrict          bool     `json:"is_strict,omitempty"`
    IsSecurityDefiner bool     `json:"is_security_definer,omitempty"`
    Comment           string   `json:"comment,omitempty"`
    Owner             string   `json:"owner,omitempty"`
}

// Signature returns "name(arg_types...)" for function identity
func (f *Function) Signature() string

Trigger

Represents a table trigger.
type Trigger struct {
    Schema         string   `json:"schema"`
    Name           string   `json:"name"`
    TableName      string   `json:"table_name"`
    Timing         string   `json:"timing"` // BEFORE, AFTER, INSTEAD OF
    Events         []string `json:"events"` // INSERT, UPDATE, DELETE, TRUNCATE
    ForEachRow     bool     `json:"for_each_row,omitempty"`
    WhenCondition  string   `json:"when_condition,omitempty"`
    FunctionSchema string   `json:"function_schema,omitempty"`
    FunctionName   string   `json:"function_name"`
    Definition     string   `json:"definition,omitempty"`
    Comment        string   `json:"comment,omitempty"`
}

TimescaleDB Types

Hypertable

type Hypertable struct {
    Schema              string               `json:"schema"`
    TableName           string               `json:"table_name"`
    TimeColumnName      string               `json:"time_column_name"`
    TimeColumnType      string               `json:"time_column_type,omitempty"`
    PartitionInterval   string               `json:"partition_interval,omitempty"`
    ChunkTimeInterval   string               `json:"chunk_time_interval,omitempty"`
    SpacePartitions     int                  `json:"space_partitions,omitempty"`
    SpaceColumns        []string             `json:"space_columns,omitempty"`
    CompressionEnabled  bool                 `json:"compression_enabled,omitempty"`
    CompressionSettings *CompressionSettings `json:"compression_settings,omitempty"`
    RetentionPolicy     *RetentionPolicy     `json:"retention_policy,omitempty"`
    NumDimensions       int                  `json:"num_dimensions,omitempty"`
}

type CompressionSettings struct {
    SegmentByColumns  []string        `json:"segment_by_columns,omitempty"`
    OrderByColumns    []OrderByColumn `json:"order_by_columns,omitempty"`
    ChunkTimeInterval string          `json:"chunk_time_interval,omitempty"`
}

type OrderByColumn struct {
    Column string `json:"column"`
    Desc   bool   `json:"desc,omitempty"`
}

type RetentionPolicy struct {
    DropAfter string `json:"drop_after"`
}

Continuous Aggregate

type ContinuousAggregate struct {
    Schema           string         `json:"schema"`
    ViewName         string         `json:"view_name"`
    HypertableSchema string         `json:"hypertable_schema,omitempty"`
    HypertableName   string         `json:"hypertable_name,omitempty"`
    Query            string         `json:"query,omitempty"`
    RefreshPolicy    *RefreshPolicy `json:"refresh_policy,omitempty"`
    WithData         bool           `json:"with_data,omitempty"`
    Materialized     bool           `json:"materialized,omitempty"`
    Finalized        bool           `json:"finalized,omitempty"`
    Comment          string         `json:"comment,omitempty"`
}

type RefreshPolicy struct {
    StartOffset      string `json:"start_offset,omitempty"`
    EndOffset        string `json:"end_offset,omitempty"`
    ScheduleInterval string `json:"schedule_interval,omitempty"`
}

Other Types

Extension

type Extension struct {
    Name    string `json:"name"`
    Schema  string `json:"schema,omitempty"`
    Version string `json:"version,omitempty"`
}

Custom Type

type CustomType struct {
    Schema     string   `json:"schema"`
    Name       string   `json:"name"`
    Type       string   `json:"type"` // enum, composite, domain
    Values     []string `json:"values,omitempty"`     // For enums
    Attributes []Column `json:"attributes,omitempty"` // For composites
    BaseType   string   `json:"base_type,omitempty"`  // For domains
    Constraint string   `json:"constraint,omitempty"` // For domains
}

Sequence

type Sequence struct {
    Schema      string `json:"schema"`
    Name        string `json:"name"`
    DataType    string `json:"data_type,omitempty"`
    StartValue  int64  `json:"start_value,omitempty"`
    Increment   int64  `json:"increment,omitempty"`
    MinValue    int64  `json:"min_value,omitempty"`
    MaxValue    int64  `json:"max_value,omitempty"`
    Cache       int64  `json:"cache,omitempty"`
    Cycle       bool   `json:"cycle,omitempty"`
    OwnedBy     string `json:"owned_by,omitempty"`
}

Partition Strategy

type PartitionStrategy struct {
    Type       string      `json:"type"` // HASH, RANGE, LIST
    Columns    []string    `json:"columns"`
    Partitions []Partition `json:"partitions,omitempty"`
}

type Partition struct {
    Name       string `json:"name"`
    Expression string `json:"expression"` // FOR VALUES WITH/FROM/IN clause
}

See Also