Documentation
¶
Index ¶
- Constants
- Variables
- func EmptyArgs() map[string]interface{}
- func ErrorCoalesce(errs ...error) error
- func ValidateRefName(refName string) error
- func ValidateStringNonEmpty(what, s string) error
- type Action
- type ActionCustom
- type ActionMigrateDown
- type ActionMigrateUp
- type ActionVersion
- type Addr
- type Argument
- type Arguments
- type Config
- type DDLCheck
- type DDLColumn
- type DDLCreateIndex
- type DDLCreateTable
- type DDLDropIndex
- type DDLDropTable
- type DDLForeignKey
- type DDLGeneratedColumn
- type DDLIdentity
- type DDLPrimaryKey
- type DDLReferences
- type DDLUnique
- type Defaultable
- type File
- type HookRunner
- type Hooks
- type Migration
- type MigrationSet
- type MigrationUpOrDown
- type Parser
- type RawFile
- type RawMigration
- type RawMigrationUpOrDown
- type SQLStatement
- type SQLStatements
- type SQLStatementsSorter
- type SchemaMigration
- type SchemaMigrationTable
- func (s SchemaMigrationTable) Delete(ctx context.Context, db krabdb.ExecerContext, version string) error
- func (s SchemaMigrationTable) Exists(ctx context.Context, db krabdb.QueryerContext, migration SchemaMigration) (bool, error)
- func (s SchemaMigrationTable) FilterPending(all []*Migration, refsInDb []SchemaMigration) []*Migration
- func (s SchemaMigrationTable) Init(ctx context.Context, db krabdb.ExecerContext) error
- func (s SchemaMigrationTable) Insert(ctx context.Context, db krabdb.ExecerContext, version string) error
- func (s SchemaMigrationTable) SelectAll(ctx context.Context, db krabdb.QueryerContext) ([]SchemaMigration, error)
- func (s SchemaMigrationTable) SelectLastN(ctx context.Context, db krabdb.QueryerContext, limit int) ([]SchemaMigration, error)
- func (s SchemaMigrationTable) Truncate(ctx context.Context, db krabdb.ExecerContext) error
- type Templates
- type ToSQL
- type Validator
Constants ¶
const ( InfoName = "krab" InfoFullName = "Oh Krab!" InfoWWW = "https://ohkrab.dev" )
const DefaultSchemaMigrationTableName = "schema_migrations"
Variables ¶
var ( InfoVersion = "<generated>" InfoCommit = "<generated>" InfoBuildDate = "<generated>" )
var DDLCreateIndexSchema = hcl.BodySchema{ Blocks: []hcl.BlockHeaderSchema{ { Type: "create_index", LabelNames: []string{"table", "name"}, }, }, }
var DDLCreateTableSchema = hcl.BodySchema{ Blocks: []hcl.BlockHeaderSchema{ { Type: "create_table", LabelNames: []string{"name"}, }, }, }
var DDLDropIndexSchema = hcl.BodySchema{ Blocks: []hcl.BlockHeaderSchema{ { Type: "drop_index", LabelNames: []string{"name"}, }, }, }
var DDLDropTableSchema = hcl.BodySchema{ Blocks: []hcl.BlockHeaderSchema{ { Type: "drop_table", LabelNames: []string{"name"}, }, }, }
var MigrationUpOrDownSchema = hcl.BodySchema{ Attributes: []hcl.AttributeSchema{ { Name: "sql", Required: false, }, }, }
Functions ¶
func ErrorCoalesce ¶
ErrorCoalesce returns first non empty error.
func ValidateRefName ¶
ValidateRefName checks if reference name matches allowed format.
func ValidateStringNonEmpty ¶
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) 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)
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) 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) 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) Synopsis ¶
func (a *ActionMigrateUp) Synopsis() string
type ActionVersion ¶
ActionVersion prints full version.
func (*ActionVersion) Help ¶
func (a *ActionVersion) Help() string
func (*ActionVersion) Synopsis ¶
func (a *ActionVersion) Synopsis() string
type Addr ¶
Addr represents resource reference.
func (Addr) OnlyRefNames ¶
OnlyRefNames returns reference name without 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()
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) InitDefaults ¶ added in v0.4.0
func (a *Arguments) InitDefaults()
type Config ¶
type Config struct {
MigrationSets map[string]*MigrationSet
Migrations map[string]*Migration
Actions map[string]*Action
}
Config represents all configuration loaded from directory.
type DDLCheck ¶ added in v0.4.0
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
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 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
ShouldRunInTransaction returns whether migration should be wrapped into transaction or not.
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 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 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
func (s *SQLStatementsSorter) Sort() SQLStatements
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
func (s SchemaMigrationTable) Delete(ctx context.Context, db krabdb.ExecerContext, version string) error
Delete removes migration from a database.
func (SchemaMigrationTable) Exists ¶ added in v0.4.0
func (s SchemaMigrationTable) Exists(ctx context.Context, db krabdb.QueryerContext, migration SchemaMigration) (bool, error)
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
func (s SchemaMigrationTable) Init(ctx context.Context, db krabdb.ExecerContext) error
Init creates a migrations table.
func (SchemaMigrationTable) Insert ¶ added in v0.4.0
func (s SchemaMigrationTable) Insert(ctx context.Context, db krabdb.ExecerContext, version string) error
Insert saves migration to a database.
func (SchemaMigrationTable) SelectAll ¶ added in v0.4.0
func (s SchemaMigrationTable) SelectAll(ctx context.Context, db krabdb.QueryerContext) ([]SchemaMigration, error)
SelectAll fetches all migrations from a database.
func (SchemaMigrationTable) SelectLastN ¶ added in v0.4.0
func (s SchemaMigrationTable) SelectLastN(ctx context.Context, db krabdb.QueryerContext, limit int) ([]SchemaMigration, error)
SelectLastN fetches last N migrations in Z-A order.
func (SchemaMigrationTable) Truncate ¶ added in v0.4.0
func (s SchemaMigrationTable) Truncate(ctx context.Context, db krabdb.ExecerContext) error
Truncate truncates migrations table.
type ToSQL ¶ added in v0.4.0
type ToSQL interface {
ToSQL(w io.StringWriter)
}
ToSQL converts DSL struct to SQL.
Source Files
¶
- action_custom.go
- action_migrate_down.go
- action_migrate_up.go
- action_version.go
- addr.go
- config.go
- defaultable.go
- file.go
- hook_runner.go
- parser.go
- schema_migration_table.go
- sql_statement.go
- templates.go
- type_action.go
- type_arguments.go
- type_ddl_check.go
- type_ddl_column.go
- type_ddl_create_index.go
- type_ddl_create_table.go
- type_ddl_drop_index.go
- type_ddl_drop_table.go
- type_ddl_foreign_key.go
- type_ddl_generated_column.go
- type_ddl_identity.go
- type_ddl_primary_key.go
- type_ddl_references.go
- type_ddl_unique.go
- type_hooks.go
- type_migration.go
- type_migration_set.go
- validator.go
- version.go