migrations

package
v1.0.1-0...-5fb5fd8 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FilePattern        = *regexp.MustCompile(`(?P<Version>^\d{20})_(?P<Name>[aA-zZ]+).yaml$`)
	CreateTablePattern = *regexp.MustCompile(`CREATE TABLE (?P<TableName>\w+)`)
	DropTablePattern   = *regexp.MustCompile(`(DROP TABLE (IF EXISTS )?)(?P<TableName>\w+)`)
)

Functions

func CreateMigrationEntry

func CreateMigrationEntry(table string) string

func CreateMigrationTable

func CreateMigrationTable(table string) string

func DeleteMigrationEntry

func DeleteMigrationEntry(table string) string

func DropMigrationTable

func DropMigrationTable(table string) string

func IsEmpty

func IsEmpty(store Store, schemaTable string) bool

MARK: - Return `true` if the schemaTable is found and has no rows.

func IsTracked

func IsTracked(store Store, schemaTable string) bool

MARK: - Returns `true` if the schemaTable is found in the database. Returns `false` in any other case.

func IsUpToDate

func IsUpToDate(store Store, schemaTable string, migrations MigrationList) bool

func LoadFiles

func LoadFiles(dir string, pattern *regexp.Regexp) []fs.FileInfo

func MatchingFiles

func MatchingFiles(dir string, pattern *regexp.Regexp) ([]fs.FileInfo, error)

MatchingFiles - Finds all files that statisfy a regex in the specified directory

func NumberOfAppliedMigrations

func NumberOfAppliedMigrations(table string) string

func SchemaTableExists

func SchemaTableExists(table string) string

func SelectMigrationEntry

func SelectMigrationEntry(table string) string

func SelectMigrations

func SelectMigrations(table string) string

func SelectMigrationsVersion

func SelectMigrationsVersion(table string) string

func StartTracking

func StartTracking(store Store, schemaTable string) bool

func StopTracking

func StopTracking(store Store, schemaTable string) bool

func Validate

func Validate(migrations MigrationList) (bool, string)

Validate - Runs validations on a list of migrations.

Types

type Changes

type Changes struct {
	Up   []string `yaml:"up"`
	Down []string `yaml:"down"`
}

type DatabaseConnector

type DatabaseConnector interface {
	// Connect - Acquire a connection to the database.
	Connect() error

	// Disconnect - Releases all existing database connections.
	Disconnect() error
}

type Engine

type Engine interface {
	// DatabaseConnector
	FileLoader
	MigrationRunner
	Tracker
	Validator
}

type EngineError

type EngineError struct{}

func (EngineError) Error

func (error EngineError) Error() string

type FileLoader

type FileLoader interface {
	// LoadFiles - Loads files from the given directory matching the provided regex
	LoadFiles(string, *regexp.Regexp) []fs.FileInfo
}

type Migration

type Migration struct {
	Id       int     `yaml:"-" json:"-"`
	FileName string  `yaml:"-"`
	Version  string  `yaml:"version"`
	Schema   int     `yaml:"schema,omitempty" json:"-"`
	Name     string  `yaml:"name"`
	Engine   string  `yaml:"engine" json:"-"`
	Changes  Changes `yaml:"changes,omitempty" json:"-"`
	// contains filtered or unexported fields
}

func (Migration) Description

func (M Migration) Description() string

func (*Migration) Load

func (instance *Migration) Load(file fs.FileInfo, parent string, pattern *regexp.Regexp) error

func (*Migration) Next

func (M *Migration) Next() *Migration

MARK: - Implements LinkedList behavior

type MigrationFile

type MigrationFile struct {
	// contains filtered or unexported fields
}

func (MigrationFile) IsDir

func (f MigrationFile) IsDir() bool

func (MigrationFile) ModTime

func (f MigrationFile) ModTime() time.Time

func (MigrationFile) Mode

func (f MigrationFile) Mode() fs.FileMode

func (MigrationFile) Name

func (f MigrationFile) Name() string

func (MigrationFile) Size

func (f MigrationFile) Size() int64

func (MigrationFile) Sys

func (f MigrationFile) Sys() any

type MigrationList

type MigrationList struct {
	// contains filtered or unexported fields
}

func BuildMigrations

func BuildMigrations(files []fs.FileInfo, dir string, pattern *regexp.Regexp) MigrationList

BuildMigrations - Instantiate a list of migrations from the contents of the provided files. Accesses the filesystem.

func (*MigrationList) Description

func (List *MigrationList) Description() string

func (*MigrationList) Find

func (List *MigrationList) Find(identifier string) (sequence MigrationList, isFound bool)

Find - Traverses the list in search for a given node.

func (*MigrationList) FromMap

func (List *MigrationList) FromMap(m map[string]Migration)

FromMap - Inserts map elements into the list.

func (*MigrationList) GetHead

func (List *MigrationList) GetHead() *Migration

GetHead - Returns the node at the start of the list.

func (*MigrationList) GetTail

func (List *MigrationList) GetTail() *Migration

GetTail - Returns the node at the end of the list.

func (*MigrationList) Insert

func (List *MigrationList) Insert(node *Migration)

Insert - Adds a new node to the end of the list.

func (*MigrationList) IsEmpty

func (List *MigrationList) IsEmpty() bool

func (*MigrationList) Remove

func (List *MigrationList) Remove(identifier string)

Remove - Excludes a node from the list.

func (*MigrationList) Reverse

func (List *MigrationList) Reverse()

Reverse - Traverses list and swaps the direction of the list.

func (*MigrationList) Size

func (List *MigrationList) Size() int

func (*MigrationList) ToMap

func (List *MigrationList) ToMap() map[string]Migration

ToMap - Transforms the list into a map.

func (*MigrationList) ToSlice

func (List *MigrationList) ToSlice() Migrations

ToSlice - Transforms the list into a slice.

type MigrationRunner

type MigrationRunner interface {
	// Up - Runs migrations
	Up(changes MigrationList) error

	// Down - Reverts migrations
	Down(changes MigrationList) error
}

type Migrations

type Migrations []Migration

func (Migrations) Description

func (m Migrations) Description() string

MARK: - Implements Formattable

func (Migrations) Len

func (m Migrations) Len() int

func (Migrations) Less

func (m Migrations) Less(left, right int) bool

func (Migrations) Swap

func (m Migrations) Swap(left, right int)

func (Migrations) ToHash

func (m Migrations) ToHash() map[string]Migration

type MigratorVersion

type MigratorVersion struct {
	Id        int       `json:"id"`
	Name      string    `json:"name"`
	Version   string    `json:"version"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
}

func Version

func Version(store Store, schemaTable string) (MigratorVersion, bool)

func (*MigratorVersion) Description

func (V *MigratorVersion) Description() string

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner:

Responsible for running and reverting migrations.
The migration and rollback algorithm is self-contained within this type.

For more flexibility, creations, reads, and deletions are responsibilities of
the underlying store type. You can inject a type that conforms to the `Store` interface,
and the `Runner` will call the store's appropriate methods when needed.

func (*Runner) AppliedMigrations

func (runner *Runner) AppliedMigrations(directory string, filePattern *regexp.Regexp, loadFromDir bool) MigrationList

func (*Runner) Down

func (runner *Runner) Down(migrations MigrationList) error

func (*Runner) Generate

func (runner *Runner) Generate(name string, directory string) Migration

func (*Runner) GetSchemaTable

func (runner *Runner) GetSchemaTable() string

func (*Runner) GetStore

func (runner *Runner) GetStore() Store

func (*Runner) LogError

func (runner *Runner) LogError(err string)

func (*Runner) LogInfo

func (runner *Runner) LogInfo(info string)

func (*Runner) PendingMigrations

func (runner *Runner) PendingMigrations(directory string, filePattern *regexp.Regexp) MigrationList

func (*Runner) SetLogger

func (runner *Runner) SetLogger(format, template string)

func (*Runner) SetSchemaTable

func (runner *Runner) SetSchemaTable(table string)

func (*Runner) SetStore

func (runner *Runner) SetStore(store Store)

func (*Runner) Up

func (runner *Runner) Up(migrations MigrationList) error

func (*Runner) Version

func (runner *Runner) Version() (MigratorVersion, bool)

type Store

type Store interface {
	// Create - Adds record(s) to the store.
	Create(string, ...interface{}) error

	// Read - Reads record from the store.
	Read(string, interface{}, ...interface{}) error

	// Delete - Removes record(s) from the store.
	Delete(string, ...interface{}) error

	// Name - A string that identifies this store.
	Name() string

	DatabaseURL() string
}

type TableSchema

type TableSchema struct {
	TableSchema string `json:"table_schema" db:"table_schema"`
	TableName   string `json:"table_name" db:"table_name"`
	TableType   string `json:"table_type" db:"table_type"`
}

type Tracker

type Tracker interface {
	// ... -
	BuildMigrations([]fs.FileInfo) MigrationList

	// StartTracking - Prepares database for migration tracking
	StartTracking() error

	// StopTracking - Stops tracking database migrations
	StopTracking() error

	// Version - Return the version of the last applied migration. The returned boolean should indicate if the database is being tracked
	Version() (string, bool)

	// IsUpToDate - Indicator of whether migrations are current or up-to-date
	IsUpToDate(changes MigrationList) bool

	// IsTracked - Indicator of whether the database is being managed by this tool
	IsTracked() bool

	// IsEmpty - Indicator of whether the database has any migrations
	IsEmpty() bool

	// AppliedMigrations - Returns all applied/saved migrations
	AppliedMigrations() MigrationList

	// PendingMigrations - Returns all non-applied/saved migrations.
	PendingMigrations() MigrationList
}

type ValidationError

type ValidationError struct{}

func (ValidationError) Error

func (error ValidationError) Error() string

type Validator

type Validator interface {
	// Validate - Given a set of migrations, this method should return whether or not the migrations are valid.
	Validate(changes MigrationList) (bool, string)
}

Jump to

Keyboard shortcuts

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