schema

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

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const UnknownColumn = "§"

UnknownColumn is the character that represents a column that cannot be introspected from the database.

Variables

This section is empty.

Functions

func SQLCombine

func SQLCombine(sqls []string) string

SQLCombine combines multiple SQL statements into one string, separating SQL statements with the ";" separator.

Types

type Column

type Column struct {
	Name    string
	SQLType string
	Default string
	Alias   string
	DiffStruct
}

A Column represents a single column in a table

func (*Column) DiffSchema

func (c *Column) DiffSchema(old *Column) *Column

DiffSchema compares c and old and returns a diff column.

func (*Column) PrintDelta

func (c *Column) PrintDelta(out io.Writer)

PrintDelta writes the column delta to out.

func (*Column) SQL

func (c *Column) SQL() string

SQL returns the SQL expression for the column c

type Constraint

type Constraint interface {
	Name() string
	SQL() string
	DependsOnTable() string
	Differ
}

A Constraint is a table constraint such as a unique column or foreign-key.

type Diff

type Diff int

Diff specifies the nature of a schema change

const (
	NoDiff Diff = iota
	Added
	Removed
	Changed
)

Diff types

func (Diff) Sigil

func (d Diff) Sigil() string

Sigil returns a short description of this diff type.

type DiffStruct

type DiffStruct struct {
	Diff Diff
}

A DiffStruct represents a diff.

func (*DiffStruct) DiffMode

func (d *DiffStruct) DiffMode() Diff

DiffMode returns the current diff mode.

func (*DiffStruct) SetDiffMode

func (d *DiffStruct) SetDiffMode(diff Diff)

SetDiffMode sets the diff mode to diff.

type Differ

type Differ interface {
	DiffMode() Diff
	SetDiffMode(diff Diff)
}

A Differ controls how a schema diff is performed. (TODO: rename)

type ForeignKeyConstraint

type ForeignKeyConstraint struct {
	ConstraintName   string
	SourceTableField string
	TargetTable      string
	TargetTableField string
	*DiffStruct
}

A ForeignKeyConstraint links a foreign key column in a table to a (primary) key in a target table.

func (ForeignKeyConstraint) DependsOnTable

func (f ForeignKeyConstraint) DependsOnTable() string

DependsOnTable gets the name of the table this constraint depends on.

func (ForeignKeyConstraint) Name

func (f ForeignKeyConstraint) Name() string

Name gets the name of the foreign key constraint f

func (ForeignKeyConstraint) SQL

func (f ForeignKeyConstraint) SQL() string

SQL gets the SQL clause for the constraint f.

type Index

type Index struct {
	Name      string
	TableName string
	Columns   []string
	Unique    bool
	Force     bool // Always created, even if other indexes are omitted
	DiffStruct
}

An Index represents a table index.

func (*Index) DropSQL

func (i *Index) DropSQL() string

DropSQL returns the DDL statement to drop the index i.

func (*Index) PrintDelta

func (c *Index) PrintDelta(out io.Writer)

PrintDelta writes the index delta to out.

func (*Index) SQL

func (i *Index) SQL() string

SQL returns the DDL to create the index i.

type PrimaryKeyConstraint

type PrimaryKeyConstraint struct {
	ConstraintName string
	Column         string
	*DiffStruct
}

A PrimaryKeyConstraint represents the primary key constraint for a table.

func (PrimaryKeyConstraint) DependsOnTable

func (c PrimaryKeyConstraint) DependsOnTable() string

DependsOnTable returns "" always (primary-key constraints can have no dependencies on other tables).

func (PrimaryKeyConstraint) Name

func (c PrimaryKeyConstraint) Name() string

Name returns the constraint name for c.

func (PrimaryKeyConstraint) SQL

func (c PrimaryKeyConstraint) SQL() string

SQL returns the SQL for c.

type Schema

type Schema struct {
	Tables []*Table
}

A Schema is a set of tables

func (*Schema) DiffSchema

func (s *Schema) DiffSchema(old *Schema) *Schema

DiffSchema compares s to old and returns a diff schema.

func (*Schema) DropIndexConstraintSQL

func (s *Schema) DropIndexConstraintSQL() []string

DropIndexConstraintSQL returns the DDL to drop indexes and constraints, usually to prepare for a bulk load.

func (*Schema) IndexConstraintSQL

func (s *Schema) IndexConstraintSQL() []string

IndexConstraintSQL returns the DDL for table indexes and constraints.

func (*Schema) PrintDelta

func (s *Schema) PrintDelta(out io.Writer)

PrintDelta writes the schema delta to out.

func (*Schema) SQL

func (s *Schema) SQL() []string

SQL returns the list of DDL statements for this schema.

func (*Schema) SQLNoIndexesConstraints

func (s *Schema) SQLNoIndexesConstraints() []string

SQLNoIndexesConstraints returns the schema SQL without index and constraints.

func (*Schema) SQLSel

func (s *Schema) SQLSel(sel Select) []string

SQLSel returns the SQL DDL statements selected by sel

func (*Schema) Sort

func (s *Schema) Sort() *Schema

Sort orders the objects in the schema by their dependencies and names.

func (*Schema) Table

func (s *Schema) Table(name string) *Table

Table gets the table object given a table name.

func (*Schema) Write

func (s *Schema) Write(sel Select, writer io.Writer) (int, error)

Write writes the SQL statements for schema, selected by sel to the writer.

func (*Schema) WriteFile

func (s *Schema) WriteFile(sel Select, filename string) (int, error)

WriteFile writes the SQL statements for schema, selected to the writer to the file named by filename, which will be created if possible.

type Select

type Select int

Select specifies what database DDL statements are selected.

const (
	// SelTables requests only table created statements
	SelTables Select = iota

	// SelIndexesConstraints requests indexes and constraints on tables
	SelIndexesConstraints

	// SelTablesIndexesConstraints requests tables, indexes and constraints
	SelTablesIndexesConstraints

	// SelDropIndexesConstraints requests drop statements for indexes and
	// constraints. This is usually used to drop indexes and constraints for a
	// bulk data load.
	SelDropIndexesConstraints
)

type Table

type Table struct {
	Name        string
	Columns     []*Column
	Indexes     []*Index
	Constraints []Constraint

	*DiffStruct
	// contains filtered or unexported fields
}

A Table represents a single table's columns, indexes, constaints and dependencies.

func (*Table) Column

func (t *Table) Column(name string) *Column

Column gets the column object given the column name

func (*Table) CreateColumnClauses

func (t *Table) CreateColumnClauses() []string

CreateColumnClauses returns the list of column SQL expressions.

func (*Table) CreateForceIndexSQL

func (t *Table) CreateForceIndexSQL() []string

CreateForceIndexSQL returns SQL statements for force-created indexes. Forced indexes are necessary for fast bulk-loads (usually indexes on lookup tables), and must be returned even when if the caller requested no regular indexes.

func (*Table) CreateIndexConstraintSQLs

func (t *Table) CreateIndexConstraintSQLs() []string

CreateIndexConstraintSQLs returns the DDL statements to create the table's indexes and constraints.

func (*Table) CreateTableSQL

func (t *Table) CreateTableSQL() string

CreateTableSQL returns the DDL SQL to create this table.

func (*Table) DependsOn

func (t *Table) DependsOn(other *Table) bool

DependsOn checks if t depends on other (viz. has a foreign key into other).

func (*Table) DiffConstraints

func (t *Table) DiffConstraints(old *Table) []Constraint

DiffConstraints compares the constraints on t and old and returns the list of constraint diffs.

func (*Table) DiffSchema

func (t *Table) DiffSchema(old *Table) *Table

DiffSchema compares t to old and returns a diff table.

func (*Table) DropIndexConstraintSQL

func (t *Table) DropIndexConstraintSQL() []string

DropIndexConstraintSQL returns the DDL to drop this table's indexes and constraints.

func (*Table) DropSQL

func (t *Table) DropSQL() []string

DropSQL returns the SQL to drop t

func (*Table) Index

func (t *Table) Index(name string) *Index

Index finds the index on this table with the given name

func (*Table) IndexConstraintSQL

func (t *Table) IndexConstraintSQL() []string

IndexConstraintSQL returns SQL statements to create indexes and constraints

func (*Table) PrintDelta

func (t *Table) PrintDelta(out io.Writer)

PrintDelta writes the table delta to out.

func (*Table) SQL

func (t *Table) SQL() []string

SQL returns the DDL to create the table t, along with indexes and constraints.

func (*Table) SQLNoIndexesConstraints

func (t *Table) SQLNoIndexesConstraints() []string

SQLNoIndexesConstraints returns the table's DDL SQL, ignoring indexes and constraints.

Jump to

Keyboard shortcuts

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