schema

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortMigrationsByOperationPriority

func SortMigrationsByOperationPriority(migrations []*ColumnMigration)

This function sorts the given column migrations list by operation priority. The priority of the operation is determined by it's value, smaller value means higher priority

Types

type Column

type Column struct {
	TableName    string
	Name         string
	ColumnType   string
	Null         string
	PrimaryKey   bool
	ForeignKey   bool
	UniqueIndex  bool
	DefaultValue sql.NullString
	Extra        string
}

func (*Column) ComparePrimary

func (c *Column) ComparePrimary(col Column) int

0 -> same primary. 1 -> c is not primary, col is primary. -1 -> c is primary, col is not primary.

func (*Column) Equals

func (c *Column) Equals(col Column) bool

returns true if columns are same, false if not

func (*Column) PrettyPrint

func (c *Column) PrettyPrint()

type ColumnMigration

type ColumnMigration struct {
	ApplyOn   interface{}
	Old       interface{}
	Operation ColumnOperation
}

func NewColumnMigration

func NewColumnMigration(applyOn interface{}, old interface{}, operation ColumnOperation) *ColumnMigration

type ColumnOperation

type ColumnOperation int
const (
	DROP_FOREIGN_KEY ColumnOperation = iota
	DROP_UNIQUE_INDEX
	DROP_COLUMN
	RENAME_COLUMN
	MODIFY_COLUMN
	ADD_COLUMN
	UPDATE_FOREIGN_KEY
	ADD_FOREIGN_KEY
	ADD_UNIQUE_INDEX
)

type Key

type Key string
const (
	PRIMARY_KEY  Key = "PRI"
	FOREIGN_KEY  Key = "MUL"
	UNIQUE_INDEX Key = "UNI"
)

type Reference

type Reference struct {
	TableName            string
	ColumnName           string
	ReferencedTableName  string
	ReferencedColumnName string
	DeleteOption         ReferenceOption
	UpdateOption         ReferenceOption
	IsUnique             bool
}

func NewReference

func NewReference(tableName string, columnName string, referencedTableName string,
	referencedColumnName string, deleteOption ReferenceOption, updateOption ReferenceOption) *Reference

func (*Reference) PrettyPrint

func (r *Reference) PrettyPrint()

type ReferenceOption

type ReferenceOption string
const (
	RESTRICT_OPTION    ReferenceOption = "RESTRICT"
	CASCADE_OPTION     ReferenceOption = "CASCADE"
	SET_NULL_OPTION    ReferenceOption = "SET NULL"
	NO_ACTION_OPTION   ReferenceOption = "NO ACTION"
	SET_DEFAULT_OPTION ReferenceOption = "SET DEFAULT"
)

type Table

type Table struct {
	Name              string
	Columns           []*Column
	References        []Reference
	IndexToUniqueCols map[string][]string // index name maps to list of column names
	PrimaryCols       []string
}

func (*Table) CompareUniqueIndex

func (t *Table) CompareUniqueIndex(indexName string, colsCompare []string) int

0 -> same \r 1 -> t does not have the index or some columns are missing. Indexes are not same

func (*Table) CompareWith

func (t *Table) CompareWith(dst *Table) []*ColumnMigration

Compares the caller table with the given dst table and creates migrations to change caller table into given 'dst' table

func (*Table) GetPrimaryKeyColumn

func (t *Table) GetPrimaryKeyColumn() *Column

TODO: handle composite primary keys Returns primaryKey column. If not found returns nil

func (*Table) HasColumn

func (t *Table) HasColumn(col *Column) (contains bool, index int)

Checks columns existance by name, returns its index if exists. Returns -1 if column not found

func (*Table) PrettyPrint

func (t *Table) PrettyPrint()

type TablePair

type TablePair struct {
	First  *Table
	Second *Table
}

Jump to

Keyboard shortcuts

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