krab

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InfoName     = "krab"
	InfoFullName = "Oh Krab!"
	InfoWWW      = "https://ohkrab.dev"
)

Variables

View Source
var (
	InfoVersion   = "<generated>"
	InfoCommit    = "<generated>"
	InfoBuildDate = "<generated>"
)

Functions

func ErrorCoalesce

func ErrorCoalesce(errs ...error) error

ErrorCoalesce returns first non empty error.

func SchemaMigrationDelete

func SchemaMigrationDelete(ctx context.Context, db sqlx.ExecerContext, version string) error

SchemaMigrationDelete removes migration from a database.

func SchemaMigrationExists added in v0.3.1

func SchemaMigrationExists(ctx context.Context, db sqlx.QueryerContext, migration SchemaMigration) (bool, error)

SchemaMigrationExists checks if migration exists in database.

func SchemaMigrationInit

func SchemaMigrationInit(ctx context.Context, db sqlx.ExecerContext) error

SchemaMigrationInit creates a migrations table.

func SchemaMigrationInsert

func SchemaMigrationInsert(ctx context.Context, db sqlx.ExecerContext, version string) error

SchemaMigrationInsert saves migration to a database.

func SchemaMigrationTruncate

func SchemaMigrationTruncate(ctx context.Context, db sqlx.ExecerContext) error

SchemaMigrationTruncate truncates migrations table.

func ValidateRefName

func ValidateRefName(refName string) error

ValidateRefName checks if reference name matches allowed format.

func ValidateStringNonEmpty

func ValidateStringNonEmpty(what, s string) error

ValidateStringNonEmpty checks if string is not empty.

Types

type ActionMigrateDown

type ActionMigrateDown struct {
	Set           *MigrationSet
	DownMigration SchemaMigration
}

ActionMigrateDown keeps data needed to perform this action.

func (*ActionMigrateDown) Do

func (a *ActionMigrateDown) Do(ctx context.Context, db *sqlx.DB) error

Do performs the action. Schema migration must exist before running it.

func (*ActionMigrateDown) Help

func (a *ActionMigrateDown) Help() string

func (*ActionMigrateDown) Run

func (a *ActionMigrateDown) Run(args []string) int

Run in CLI.

func (*ActionMigrateDown) Synopsis

func (a *ActionMigrateDown) Synopsis() string

type ActionMigrateUp

type ActionMigrateUp struct {
	Set *MigrationSet
}

ActionMigrateUp keeps data needed to perform this action.

func (*ActionMigrateUp) Do

func (a *ActionMigrateUp) Do(ctx context.Context, db *sqlx.DB) error

Run performs the action. All pending migrations will be executed. Migration schema is created if does not exist.

func (*ActionMigrateUp) Help

func (a *ActionMigrateUp) Help() string

func (*ActionMigrateUp) Run

func (a *ActionMigrateUp) Run(args []string) int

Run in CLI.

func (*ActionMigrateUp) Synopsis

func (a *ActionMigrateUp) Synopsis() string

type ActionVersion

type ActionVersion struct{}

ActionVersion prints full version.

func (*ActionVersion) Help

func (a *ActionVersion) Help() string

func (*ActionVersion) Run

func (a *ActionVersion) Run(args []string) int

Run in CLI.

func (*ActionVersion) Synopsis

func (a *ActionVersion) Synopsis() string

type Addr

type Addr struct {
	Keyword string
	Labels  []string
}

Addr represents resource reference.

func (*Addr) OnlyRefNames

func (a *Addr) OnlyRefNames() string

OnlyRefNames returns reference name without the keyword.

func (*Addr) String

func (a *Addr) String() string

String returns full reference name including the keyword.

type Config

type Config struct {
	MigrationSets map[string]*MigrationSet
	Migrations    map[string]*Migration
}

Config represents all configuration loaded from directory.

func NewConfig

func NewConfig(files []*File) (*Config, error)

NewConfig returns new configuration that was read from Parser. Transient attributes are updated with parsed data.

type File

type File struct {
	Migrations    []*Migration    `hcl:"migration,block"`
	MigrationSets []*MigrationSet `hcl:"migration_set,block"`
}

File represents all resource definication within a single file.

type Migration

type Migration struct {
	RefName string `hcl:"ref_name,label"`

	Version     string        `hcl:"version"`
	Up          MigrationUp   `hcl:"up,block"`
	Down        MigrationDown `hcl:"down,block"`
	Transaction *bool         `hcl:"transaction,optional"` // wrap operaiton in transaction
}

Migration represents single up/down migration pair.

func SchemaMigrationFilterPending

func SchemaMigrationFilterPending(all []*Migration, refsInDb []SchemaMigration) []*Migration

SchemaMigrationFilterPending removes `refsInDb` migrations from `all` and return new slice with pending ones only.

func (*Migration) ShouldRunInTransaction added in v0.3.0

func (ms *Migration) ShouldRunInTransaction() bool

ShouldRunInTransaction returns whether migration should be wrapped into transaction or not.

func (*Migration) Validate

func (ms *Migration) Validate() error

type MigrationDown

type MigrationDown struct {
	SQL string `hcl:"sql,optional"`
}

MigrationDown contains info how to migrate down.

type MigrationSet

type MigrationSet struct {
	RefName string `hcl:"ref_name,label"`

	// SchemaMigrationsTable string         `hcl:"schema_migrations_table"`
	MigrationsExpr hcl.Expression `hcl:"migrations"`
	Migrations     []*Migration   // populated from refs in expression
}

MigrationSet represents collection of migrations.

func (*MigrationSet) FindMigrationByVersion

func (ms *MigrationSet) FindMigrationByVersion(version string) *Migration

FindMigrationByVersion looks up for the migration in current set.

func (*MigrationSet) Validate

func (ms *MigrationSet) Validate() error

type MigrationUp

type MigrationUp struct {
	SQL string `hcl:"sql,optional"`
}

MigrationUp contains info how to migrate up.

type Parser

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

Parser represents HCL simple parser.

func NewParser

func NewParser() *Parser

NewParser initializes HCL parser and default file system.

func (*Parser) LoadConfigDir

func (p *Parser) LoadConfigDir(path string) (*Config, error)

LoadConfigDir parses files in a dir and returns Config.

type SchemaMigration

type SchemaMigration struct {
	Version string `db:"version"`
}

SchemaMigration represents a single row from migrations table.

func SchemaMigrationSelectAll

func SchemaMigrationSelectAll(ctx context.Context, db sqlx.QueryerContext) ([]SchemaMigration, error)

SchemaMigrationSelectAll fetches all migrations from a database.

func SchemaMigrationSelectLastN added in v0.3.1

func SchemaMigrationSelectLastN(ctx context.Context, db sqlx.QueryerContext, limit int) ([]SchemaMigration, error)

SchemaMigrationSelectLastN fetches last N migrations in Z-A order.

type Validator

type Validator interface {
	Validate() error
}

Jump to

Keyboard shortcuts

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