database

package
v0.0.0-...-5b699e1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuoteConstraint

func QuoteConstraint(def string, contype string) string

func QuoteIdentifier

func QuoteIdentifier(name string) string

func QuoteIndex

func QuoteIndex(def string) string

Types

type Column

type Column struct {
	ColumnName    string  `db:"column_name"`
	IsNullable    string  `db:"is_nullable"`
	ColumnDefault *string `db:"column_default"`
	DataType      string  `db:"data_type"`
	Comment       string  `db:"column_comment"`
	IsLocal       bool    `db:"is_local"`
}

Column details using pg_catalog

type Database

type Database struct {
	Hostname       string
	Port           int
	Database       string
	Username       string
	Password       string
	URI            string
	SSLMode        string // default "disable"
	Timeout        int
	DataOnly       bool
	SchemaOnly     bool
	IfExists       bool
	File           string
	IncludeSchemas []string
	IncludeTables  []string
	NoOwner        bool
	NoPrivileges   bool
	*sqlx.DB
}

Database holds connection config and a live *sqlx.DB.

func NewDatabase

func NewDatabase() *Database

func (*Database) GenTable

func (db *Database) GenTable(ctx context.Context, schema, table string, cols []Column) (string, error)

GenTable generates a CREATE TABLE DDL statement.

func (*Database) GetColumnDetail

func (db *Database) GetColumnDetail(ctx context.Context, schema, table string) ([]Column, error)

func (*Database) GetInheritance

func (db *Database) GetInheritance(ctx context.Context, schema, table string) ([]Inheritance, error)

func (*Database) GetMatViewDetails

func (db *Database) GetMatViewDetails(ctx context.Context, schema, matview string) (string, error)

GetMatViewDetails returns the DDL for a materialized view.

func (*Database) GetOwnershipAndPrivileges

func (db *Database) GetOwnershipAndPrivileges(ctx context.Context, schema, name, kind string) (string, error)

func (*Database) GetRoutine

func (db *Database) GetRoutine(schema string, r Routine) string

GetRoutine returns the SQL DDL for a single routine overload. pg_get_functiondef already returns a complete CREATE OR REPLACE statement.

func (*Database) GetRoutineSchema

func (db *Database) GetRoutineSchema(ctx context.Context, schema, routine string) ([]Routine, error)

GetRoutineSchema returns all overloads of a routine (function or procedure). pg_get_functiondef returns a complete CREATE OR REPLACE statement for both functions and procedures in PG11+, so we use it for both kinds.

func (*Database) GetRoutines

func (db *Database) GetRoutines(ctx context.Context, schema string) ([]string, error)

GetRoutines returns the distinct routine names in a schema.

func (*Database) GetSchemas

func (db *Database) GetSchemas(ctx context.Context) ([]Schema, error)

GetSchemas returns schema list

func (*Database) GetSequenceDetails

func (db *Database) GetSequenceDetails(ctx context.Context, schema, name string) (string, error)

func (*Database) GetSequences

func (db *Database) GetSequences(ctx context.Context, schema string) ([]Sequence, error)

func (*Database) GetTableComment

func (db *Database) GetTableComment(ctx context.Context, schema, table string) (string, error)

func (*Database) GetTableConstraints

func (db *Database) GetTableConstraints(ctx context.Context, schema, table string) (string, error)

func (*Database) GetTableData

func (db *Database) GetTableData(ctx context.Context, schema, table string) (string, error)

GetTableData returns COPY-format table data as a string. Prefer WriteTableData for production use to avoid buffering large tables.

func (*Database) GetTableIndexes

func (db *Database) GetTableIndexes(ctx context.Context, schema, table string) (string, error)

func (*Database) GetTableSchema

func (db *Database) GetTableSchema(ctx context.Context, schema, table string) (string, error)

GetTableSchema gets the full DDL for a table.

func (*Database) GetTables

func (db *Database) GetTables(ctx context.Context, schema string, kind RelKind) ([]Table, error)

func (*Database) GetViewDetails

func (db *Database) GetViewDetails(ctx context.Context, schema, view string) (string, error)

func (*Database) OpenDatabase

func (db *Database) OpenDatabase() error

func (*Database) WriteTableData

func (db *Database) WriteTableData(ctx context.Context, w io.Writer, schema, table string) (retErr error)

WriteTableData streams COPY-format table data directly to w, avoiding buffering the entire table in memory. The caller is responsible for buffering w (e.g. via bufio.Writer) if needed.

type Index

type Index struct {
	Name       string `db:"indexname"`
	Definition string `db:"indexdef"`
}

type Inheritance

type Inheritance struct {
	ParentSchema string `db:"parent_schema"`
	ParentTable  string `db:"parent_table"`
}

type RelKind

type RelKind string

RelKind represents a pg_class.relkind value.

const (
	RelkindTable            RelKind = "r" // ordinary table
	RelkindView             RelKind = "v" // view
	RelkindMatView          RelKind = "m" // materialized view
	RelkindPartitionedTable RelKind = "p" // partitioned table  — TODO: dump PARTITION BY clauses
	RelkindForeignTable     RelKind = "f" // foreign table      — TODO: dump CREATE FOREIGN TABLE ... SERVER
)

relkind constants per pg_class.relkind

type Routine

type Routine struct {
	Name       string `db:"routine_name"`
	Type       string `db:"routine_type"`
	Definition string `db:"routine_definition"`
}

Routine holds the DDL for a single function or procedure overload.

type Schema

type Schema struct {
	Name string `db:"schema_name"`
}

Schema struct to hold schemas

type Sequence

type Sequence struct {
	Name string `db:"seq_name"`
}

type Table

type Table struct {
	Name string `db:"table_name"`
}

Table list of tables

type TableConstraint

type TableConstraint struct {
	Name       string `db:"constraint_name"`
	Definition string `db:"constraint_def"`
	Type       string `db:"constraint_type"`
}

type View

type View struct {
	Name       string `db:"view_name"`
	Definition string `db:"view_def"`
}

Jump to

Keyboard shortcuts

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