krab

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2021 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

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

Variables

View Source
var (
	InfoVersion   = "<generated>"
	InfoCommit    = "<generated>"
	InfoBuildDate = "<generated>"
)
View Source
var DDLCreateIndexSchema = hcl.BodySchema{
	Blocks: []hcl.BlockHeaderSchema{
		{
			Type:       "create_index",
			LabelNames: []string{"table", "name"},
		},
	},
}
View Source
var DDLCreateTableSchema = hcl.BodySchema{
	Blocks: []hcl.BlockHeaderSchema{
		{
			Type:       "create_table",
			LabelNames: []string{"name"},
		},
	},
}
View Source
var DDLDropIndexSchema = hcl.BodySchema{
	Blocks: []hcl.BlockHeaderSchema{
		{
			Type:       "drop_index",
			LabelNames: []string{"name"},
		},
	},
}
View Source
var DDLDropTableSchema = hcl.BodySchema{
	Blocks: []hcl.BlockHeaderSchema{
		{
			Type:       "drop_table",
			LabelNames: []string{"name"},
		},
	},
}
View Source
var MigrationUpOrDownSchema = hcl.BodySchema{
	Attributes: []hcl.AttributeSchema{
		{
			Name:     "sql",
			Required: false,
		},
	},
}

Functions

func EmptyArgs added in v0.4.0

func EmptyArgs() map[string]interface{}

func ErrorCoalesce

func ErrorCoalesce(errs ...error) error

ErrorCoalesce returns first non empty error.

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 Action added in v0.4.0

type Action struct {
	Namespace string `hcl:"namespace,label"`
	RefName   string `hcl:"ref_name,label"`

	Arguments *Arguments `hcl:"arguments,block"`

	SQL string `hcl:"sql"`
}

Action represents custom action to execute.

func (*Action) Addr added in v0.4.0

func (a *Action) Addr() Addr

func (*Action) InitDefaults added in v0.4.0

func (a *Action) InitDefaults()

func (*Action) ToSQL added in v0.4.0

func (m *Action) ToSQL(w io.StringWriter)

func (*Action) Validate added in v0.4.0

func (a *Action) Validate() error

type ActionCustom added in v0.4.0

type ActionCustom struct {
	Ui         cli.UI
	Action     *Action
	Connection krabdb.Connection
}

ActionCustom keeps data needed to perform this action.

func (*ActionCustom) Do added in v0.4.0

func (a *ActionCustom) Do(ctx context.Context, db krabdb.DB, tpl *tpls.Templates) error

Do performs the action.

func (*ActionCustom) Help added in v0.4.0

func (a *ActionCustom) Help() string

func (*ActionCustom) Run added in v0.4.0

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

Run in CLI.

func (*ActionCustom) Synopsis added in v0.4.0

func (a *ActionCustom) Synopsis() string

type ActionMigrateDown

type ActionMigrateDown struct {
	Ui            cli.UI
	Set           *MigrationSet
	DownMigration SchemaMigration
	Arguments     Arguments
	Connection    krabdb.Connection
}

ActionMigrateDown keeps data needed to perform this action.

func (*ActionMigrateDown) Do

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 {
	Ui         cli.UI
	Set        *MigrationSet
	Connection krabdb.Connection
}

ActionMigrateUp keeps data needed to perform this action.

func (*ActionMigrateUp) Do

func (a *ActionMigrateUp) Do(ctx context.Context, db krabdb.DB, tpl *tpls.Templates, ui cli.UI) 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 {
	Ui cli.UI
}

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) Absolute added in v0.4.0

func (a Addr) Absolute() []string

Absolute returns keyword and labels as a single slice.

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 Argument added in v0.4.0

type Argument struct {
	Name        string `hcl:"name,label"`
	Type        string `hcl:"type,optional"`
	Description string `hcl:"description,optional"`
}

func (*Argument) InitDefaults added in v0.4.0

func (a *Argument) InitDefaults()

func (*Argument) Validate added in v0.4.0

func (a *Argument) Validate(value interface{}) error

type Arguments added in v0.4.0

type Arguments struct {
	Args []*Argument `hcl:"arg,block"`
}

Arguments represents command line arguments or params that you can pass to action.

func (*Arguments) Help added in v0.4.0

func (a *Arguments) Help() string

func (*Arguments) InitDefaults added in v0.4.0

func (a *Arguments) InitDefaults()

func (*Arguments) Validate added in v0.4.0

func (a *Arguments) Validate(values map[string]interface{}) error

type Config

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

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 DDLCheck added in v0.4.0

type DDLCheck struct {
	Name       string `hcl:"name,label"`
	Expression string `hcl:"expression"`
}

DDLCheck constraint DSL for table DDL.

func (*DDLCheck) ToSQL added in v0.4.0

func (d *DDLCheck) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLColumn added in v0.4.0

type DDLColumn struct {
	Name      string              `hcl:"name,label"`
	Type      string              `hcl:"type,label"`
	Null      *bool               `hcl:"null,optional"`
	Identity  *DDLIdentity        `hcl:"identity,block"`
	Default   hcl.Expression      `hcl:"default,optional"`
	Generated *DDLGeneratedColumn `hcl:"generated,block"`
}

DDLColumn DSL for table DDL.

func (*DDLColumn) ToSQL added in v0.4.0

func (d *DDLColumn) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLCreateIndex added in v0.4.0

type DDLCreateIndex struct {
	Table string `hcl:"table,label"`
	Name  string `hcl:"name,label"`

	Unique       bool     `hcl:"unique,optional"`
	Concurrently bool     `hcl:"concurrently,optional"`
	Columns      []string `hcl:"columns"`
	Include      []string `hcl:"include,optional"`
	Using        string   `hcl:"using,optional"`
	Where        string   `hcl:"where,optional"`

	DefRange hcl.Range
}

DDLCreateIndex contains DSL for creating indicies.

func (*DDLCreateIndex) ToSQL added in v0.4.0

func (d *DDLCreateIndex) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLCreateTable added in v0.4.0

type DDLCreateTable struct {
	Name        string           `hcl:"name,label"`
	Unlogged    bool             `hcl:"unlogged,optional"`
	Columns     []*DDLColumn     `hcl:"column,block"`
	PrimaryKeys []*DDLPrimaryKey `hcl:"primary_key,block"`
	ForeignKeys []*DDLForeignKey `hcl:"foreign_key,block"`
	Uniques     []*DDLUnique     `hcl:"unique,block"`
	Checks      []*DDLCheck      `hcl:"check,block"`

	DefRange hcl.Range
}

DDLCreateTable contains DSL for creating tables.

func (*DDLCreateTable) ToSQL added in v0.4.0

func (d *DDLCreateTable) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLDropIndex added in v0.4.0

type DDLDropIndex struct {
	Name string `hcl:"name,label"`

	Cascade      bool `hcl:"cascade,optional"`
	Concurrently bool `hcl:"concurrently,optional"`

	DefRange hcl.Range
}

DDLDropIndex contains DSL for dropping indicies.

func (*DDLDropIndex) ToSQL added in v0.4.0

func (d *DDLDropIndex) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLDropTable added in v0.4.0

type DDLDropTable struct {
	Name string `hcl:"name,label"`

	DefRange hcl.Range
}

DDLDropTable contains DSL for dropping tables.

func (*DDLDropTable) ToSQL added in v0.4.0

func (d *DDLDropTable) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLForeignKey added in v0.4.0

type DDLForeignKey struct {
	Columns    []string      `hcl:"columns"`
	References DDLReferences `hcl:"references,block"`
}

DDLForeignKey constraint DSL for table DDL.

func (*DDLForeignKey) ToSQL added in v0.4.0

func (d *DDLForeignKey) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLGeneratedColumn added in v0.4.0

type DDLGeneratedColumn struct {
	As string `hcl:"as"`
}

DDLGeneratedColumn DSL.

func (*DDLGeneratedColumn) ToSQL added in v0.4.0

func (d *DDLGeneratedColumn) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLIdentity added in v0.4.0

type DDLIdentity struct {
}

DDLIdentity DSL.

func (*DDLIdentity) ToSQL added in v0.4.0

func (d *DDLIdentity) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLPrimaryKey added in v0.4.0

type DDLPrimaryKey struct {
	Columns []string `hcl:"columns"`
	Include []string `hcl:"include,optional"`
}

DDLPrimaryKey constraint DSL for table DDL.

func (*DDLPrimaryKey) ToSQL added in v0.4.0

func (d *DDLPrimaryKey) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLReferences added in v0.4.0

type DDLReferences struct {
	Table    string   `hcl:"table,label"`
	Columns  []string `hcl:"columns"`
	OnDelete string   `hcl:"on_delete,optional"`
	OnUpdate string   `hcl:"on_update,optional"`
}

DDLReferences DSL for ForeignKey.

func (*DDLReferences) ToSQL added in v0.4.0

func (d *DDLReferences) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type DDLUnique added in v0.4.0

type DDLUnique struct {
	Columns []string `hcl:"columns"`
	Include []string `hcl:"include,optional"`
}

DDLUnique constraint DSL for table DDL.

func (*DDLUnique) ToSQL added in v0.4.0

func (d *DDLUnique) ToSQL(w io.StringWriter)

ToSQL converts migration definition to SQL.

type Defaultable added in v0.4.0

type Defaultable interface {
	InitDefaults()
}

type File

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

	Raw *RawFile
}

File represents all resource definitions within a single file.

type HookRunner added in v0.4.0

type HookRunner struct {
	Hooks *Hooks
}

func (HookRunner) SetSearchPath added in v0.4.0

func (h HookRunner) SetSearchPath(ctx context.Context, db sqlx.ExecerContext, schema string) error

SetSearchPath sets Postgres search_path.

type Hooks added in v0.4.0

type Hooks struct {
	Before string `hcl:"before"`
}

type Migration

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

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

Migration represents single up/down migration pair.

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 MigrationSet

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

	Arguments *Arguments `hcl:"arguments,block"`
	Hooks     *Hooks     `hcl:"hooks,block"`

	// 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) InitDefaults added in v0.4.0

func (ms *MigrationSet) InitDefaults()

func (*MigrationSet) Validate

func (ms *MigrationSet) Validate() error

type MigrationUpOrDown added in v0.4.0

type MigrationUpOrDown struct {
	SQL           string            `hcl:"sql,optional"`
	CreateTables  []*DDLCreateTable `hcl:"create_table,block"`
	CreateIndices []*DDLCreateIndex `hcl:"create_index,block"`
	DropTables    []*DDLDropTable   `hcl:"drop_table,block"`
	DropIndices   []*DDLDropIndex   `hcl:"drop_index,block"`

	AttrDefRanges map[string]hcl.Range
}

Migration contains info how to migrate up or down.

func (*MigrationUpOrDown) ToSQL added in v0.4.0

func (m *MigrationUpOrDown) ToSQL(w io.StringWriter)

func (*MigrationUpOrDown) ToSQLStatements added in v0.4.0

func (m *MigrationUpOrDown) ToSQLStatements() SQLStatements

ToSQLStatements returns list of SQL statements to executre during the migration.

func (*MigrationUpOrDown) Validate added in v0.4.0

func (m *MigrationUpOrDown) Validate() error

type Parser

type Parser struct {
	FS afero.Afero
	// 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 RawFile added in v0.4.0

type RawFile struct {
	Migrations []*RawMigration `hcl:"migration,block"`
	Remain     hcl.Body        `hcl:",remain"`
}

RawFile represents all resource definitions within a single file before mapping to structs.

type RawMigration added in v0.4.0

type RawMigration struct {
	RefName string `hcl:",label"`

	Up   RawMigrationUpOrDown `hcl:"up,block"`
	Down RawMigrationUpOrDown `hcl:"down,block"`

	Remain hcl.Body `hcl:",remain"`
}

type RawMigrationUpOrDown added in v0.4.0

type RawMigrationUpOrDown struct {
	Remain hcl.Body `hcl:",remain"`
}

type SQLStatement added in v0.4.0

type SQLStatement string

SQLStatement represents raw SQL statement.

type SQLStatements added in v0.4.0

type SQLStatements []SQLStatement

SQLStatements represents list of raw SQL statements.

func (*SQLStatements) Append added in v0.4.0

func (s *SQLStatements) Append(sql ToSQL)

Append adds new SQL statement to the list from object that satisfies ToSQL interface.

type SQLStatementsSorter added in v0.4.0

type SQLStatementsSorter struct {
	Statements SQLStatements
	Bytes      []int
}

SQLStatementsSorter sorts SQLStatement by the order how they are defined in a file.

func (*SQLStatementsSorter) Insert added in v0.4.0

func (s *SQLStatementsSorter) Insert(r hcl.Range, sql ToSQL)

Insert ToSQL at given range.

func (*SQLStatementsSorter) Len added in v0.4.0

func (s *SQLStatementsSorter) Len() int

Len is the number of elements in the collection.

func (*SQLStatementsSorter) Less added in v0.4.0

func (s *SQLStatementsSorter) Less(i int, j int) bool

Less reports whether the element with index i must sort before the element with index j.

If both Less(i, j) and Less(j, i) are false, then the elements at index i and j are considered equal. Sort may place equal elements in any order in the final result, while Stable preserves the original input order of equal elements.

Less must describe a transitive ordering:

  • if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
  • if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.

Note that floating-point comparison (the < operator on float32 or float64 values) is not a transitive ordering when not-a-number (NaN) values are involved. See Float64Slice.Less for a correct implementation for floating-point values.

func (*SQLStatementsSorter) Sort added in v0.4.0

Sort sorts statements by byte range.

func (*SQLStatementsSorter) Swap added in v0.4.0

func (s *SQLStatementsSorter) Swap(i int, j int)

Swap swaps the elements with indexes i and j.

type SchemaMigration

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

SchemaMigration represents a single row from migrations table.

type SchemaMigrationTable added in v0.4.0

type SchemaMigrationTable struct {
	Name string
}

func NewSchemaMigrationTable added in v0.4.0

func NewSchemaMigrationTable(schema string) SchemaMigrationTable

NewSchemaMigrationTable creates SchemaMigrationTable with default table name and specified schema.

func (SchemaMigrationTable) Delete added in v0.4.0

Delete removes migration from a database.

func (SchemaMigrationTable) Exists added in v0.4.0

Exists checks if migration exists in database.

func (SchemaMigrationTable) FilterPending added in v0.4.0

func (s SchemaMigrationTable) FilterPending(all []*Migration, refsInDb []SchemaMigration) []*Migration

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

func (SchemaMigrationTable) Init added in v0.4.0

Init creates a migrations table.

func (SchemaMigrationTable) Insert added in v0.4.0

Insert saves migration to a database.

func (SchemaMigrationTable) SelectAll added in v0.4.0

SelectAll fetches all migrations from a database.

func (SchemaMigrationTable) SelectLastN added in v0.4.0

SelectLastN fetches last N migrations in Z-A order.

func (SchemaMigrationTable) Truncate added in v0.4.0

Truncate truncates migrations table.

type Templates added in v0.4.0

type Templates struct {
}

func (Templates) ProcessArguments added in v0.4.0

func (t Templates) ProcessArguments(tpl string, args map[string]interface{}) (string, error)

type ToSQL added in v0.4.0

type ToSQL interface {
	ToSQL(w io.StringWriter)
}

ToSQL converts DSL struct to SQL.

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