dbschema

package
v1.5.12 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dbschema computes a structured schema diff between two Postgres databases and renders a forward migration as text. It never runs the SQL.

Index

Constants

View Source
const (
	ChangeAdded   = "added"
	ChangeRemoved = "removed"
	ChangeChanged = "changed"

	ObjectSchema     = "schema"
	ObjectTable      = "table"
	ObjectColumn     = "column"
	ObjectIndex      = "index"
	ObjectConstraint = "constraint"
	ObjectView       = "view"
	ObjectSequence   = "sequence"
)

Kinds of change and of object, as they appear in the API.

View Source
const Header = "-- Generated schema migration. Review before running; Levelrail does not run it."

Header marks the script as text for a human; this package never executes it.

Variables

View Source
var ErrUnsupported = errors.New("schema diff supports Postgres only")

ErrUnsupported reports an engine the reader has no catalog queries for.

Functions

This section is empty.

Types

type Change

type Change struct {
	Object string `json:"object"`
	Kind   string `json:"kind"`
	Schema string `json:"schema"`
	Table  string `json:"table,omitempty"`
	Name   string `json:"name"`
	Before string `json:"before,omitempty"`
	After  string `json:"after,omitempty"`
	// BeforeKind and AfterKind name the view kind when it matters, so a kind change is visible.
	BeforeKind string `json:"before_kind,omitempty"`
	AfterKind  string `json:"after_kind,omitempty"`
	// Unsupported marks a change the generator cannot turn into a statement; the SQL carries a comment instead.
	Unsupported bool `json:"unsupported,omitempty"`
}

Change is one difference. Table is set for columns, indexes and constraints.

type Column

type Column struct {
	Name     string `json:"name"`
	Type     string `json:"type"`
	Nullable bool   `json:"nullable"`
	Default  string `json:"default,omitempty"`
	// Identity is "a" (ALWAYS) or "d" (BY DEFAULT) for an identity column; Generated is "s" for a stored
	// generated column, whose expression is carried in Default.
	Identity  string `json:"identity,omitempty"`
	Generated string `json:"generated,omitempty"`
}

Column is one table column as the catalog reports it.

type Constraint

type Constraint struct {
	Name       string `json:"name"`
	Type       string `json:"type"`
	Definition string `json:"definition"`
}

Constraint is a primary key, unique, check or foreign key constraint (types p, u, c, f).

type Index

type Index struct {
	Name       string `json:"name"`
	Definition string `json:"definition"`
}

Index is a standalone index; indexes that back a constraint are carried by the constraint.

type Result

type Result struct {
	Summary   Summary  `json:"summary"`
	Changes   []Change `json:"changes"`
	SQL       string   `json:"sql"`
	Truncated bool     `json:"truncated"`
}

Result is a diff with the forward migration that turns base into head.

func Diff

func Diff(base, head Snapshot) Result

Diff compares base with head: added means head has it and base does not.

type Sequence

type Sequence struct {
	Schema    string `json:"schema"`
	Name      string `json:"name"`
	Type      string `json:"type"`
	Start     int64  `json:"start"`
	Increment int64  `json:"increment"`
	Min       int64  `json:"min"`
	Max       int64  `json:"max"`
	Cycle     bool   `json:"cycle"`
}

Sequence is a standalone sequence; sequences owned by a column are left to the column.

type Snapshot

type Snapshot struct {
	Schemas   []string   `json:"schemas"`
	Tables    []Table    `json:"tables"`
	Views     []View     `json:"views"`
	Sequences []Sequence `json:"sequences"`
	// Truncated reports that the object cap cut the read short.
	Truncated bool `json:"truncated"`
}

Snapshot is the schema of one database at one moment.

func Read

func Read(ctx context.Context, t dbviewer.Target, maxObjects int) (Snapshot, error)

Read takes a snapshot of one Postgres database through the viewer's read-only exec path. At most maxObjects tables and views are read; the snapshot says when it was cut. Credentials stay inside the container.

type Summary

type Summary struct {
	Added   int `json:"added"`
	Removed int `json:"removed"`
	Changed int `json:"changed"`
}

Summary counts the changes by kind.

type Table

type Table struct {
	Schema      string       `json:"schema"`
	Name        string       `json:"name"`
	Columns     []Column     `json:"columns"`
	Indexes     []Index      `json:"indexes"`
	Constraints []Constraint `json:"constraints"`
}

Table is a base table with its columns, indexes and constraints.

type View

type View struct {
	Schema       string `json:"schema"`
	Name         string `json:"name"`
	Materialized bool   `json:"materialized"`
	Definition   string `json:"definition"`
}

View is a view or materialized view with its definition query.

Jump to

Keyboard shortcuts

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