migrate

package
v0.3.21 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2021 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	SchemaName string
	TableName  string
}

func EffectiveConfig

func EffectiveConfig(d Dialect, c *Config) *Config

type ContentGetter

type ContentGetter func() (string, error)

type Dialect

type Dialect interface {
	DefaultConfig() *Config
	Verify(db *sqlx.DB) error
	EnsureMigrationsTable(context.Context, *sqlx.DB, *Config) error
	SelectStates(*Config) string
	InsertMigration(*Config) string
	DeleteMigration(*Config) string
	BeforeMigration(
		ctx context.Context,
		db *sqlx.DB,
		tx *sqlx.Tx,
		name string,
		direction Direction,
	) error
}

type Direction

type Direction bool
const (
	DirectionUp   Direction = true
	DirectionDown Direction = false
)

func (Direction) String

func (d Direction) String() string

type Migration

type Migration interface {
	// Name is used to track whether the migration has been run, by recording the
	// names of migrations that have been executed in a table.
	Name() string
	// Up runs the migration in the up migration, e.g. creating new tables, to
	// upgrade the schema to the new state. The migration MUST perform all changes
	// within the given transaction, and MUST NOT terminate the transaction.
	Up(context.Context, *sqlx.DB, *sqlx.Tx) error
	// Down runs the migration in the down migration, e.g. dropping tables, to
	// downgrade the schema to the old state. The migration MUST perform all
	// changes within the given transaction, and MUST NOT terminate the
	// transaction.
	Down(context.Context, *sqlx.DB, *sqlx.Tx) error
}

Migration represents a single migration that can be run to do a single atomic step of changing database schemas. Each migration will be run in an independent transaction.

func LoadFS

func LoadFS(
	migrationsFS fs.FS,
	filter func(*SQLMigration) bool,
) ([]Migration, error)

func WithPrefix

func WithPrefix(prefix string, migrations ...Migration) []Migration

WithPrefix returns either the input slice if prefix is the empty string or migrations is an empty slice, or else a copy of it with prefix applied to the names.

type Migrator

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

func New

func New(config *Config, dialect Dialect) *Migrator

New creates a new empty Migrator for the given config & dialect. You must add migrations to it before it can be run.

func NewFromFS

func NewFromFS(
	config *Config,
	dialect Dialect,
	migrationsFS fs.FS,
	filter func(*SQLMigration) bool,
) (*Migrator, error)

NewFromFS is roughly equivalent to New(...).Add("", LoadFS(...)...). If you want to apply a prefix, use the individual method calls.

func (*Migrator) AddAndSort

func (m *Migrator) AddAndSort(prefix string, migrations ...Migration) *Migrator

AddAndSort adds the listed migrations, applying the given prefix, to the migrator and sorts the resulting list

func (*Migrator) Down

func (m *Migrator) Down(
	ctx context.Context,
	db *sqlx.DB,
) error

func (*Migrator) HasDialect

func (m *Migrator) HasDialect() bool

func (*Migrator) HasMigrations

func (m *Migrator) HasMigrations() bool

func (*Migrator) SortAndAppend

func (m *Migrator) SortAndAppend(prefix string, migrations ...Migration) *Migrator

SortandAppend sorts the listed migrations and then appends them to the migrator without further sorting.

func (*Migrator) Up

func (m *Migrator) Up(
	ctx context.Context,
	db *sqlx.DB,
) error

func (*Migrator) WithConfig

func (m *Migrator) WithConfig(config *Config) *Migrator

func (*Migrator) WithDialect

func (m *Migrator) WithDialect(dialect Dialect) *Migrator

type PgxDialect

type PgxDialect struct{}

func (*PgxDialect) BeforeMigration

func (p *PgxDialect) BeforeMigration(
	ctx context.Context,
	db *sqlx.DB,
	tx *sqlx.Tx,
	name string,
	direction Direction,
) error

func (*PgxDialect) DefaultConfig

func (p *PgxDialect) DefaultConfig() *Config

func (*PgxDialect) DeleteMigration

func (p *PgxDialect) DeleteMigration(cfg *Config) string

func (*PgxDialect) EnsureMigrationsTable

func (p *PgxDialect) EnsureMigrationsTable(ctx context.Context, db *sqlx.DB, cfg *Config) error

func (*PgxDialect) InsertMigration

func (p *PgxDialect) InsertMigration(cfg *Config) string

func (*PgxDialect) SelectStates

func (p *PgxDialect) SelectStates(cfg *Config) string

func (*PgxDialect) Verify

func (p *PgxDialect) Verify(db *sqlx.DB) error

type SQLMigration

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

func FromContent

func FromContent(name string, up, down ContentGetter) *SQLMigration

FromContent generates a SQL Migration from dynamic SQL script getters.

func FromSQL

func FromSQL(name, up, down string) *SQLMigration

FromSQL generates a SQL Migration based on fixed SQL scripts.

func (*SQLMigration) Contents

func (m *SQLMigration) Contents(up Direction) (string, error)

func (*SQLMigration) Down

func (m *SQLMigration) Down(ctx context.Context, _ *sqlx.DB, tx *sqlx.Tx) error

func (*SQLMigration) Name

func (m *SQLMigration) Name() string

func (*SQLMigration) Up

func (m *SQLMigration) Up(ctx context.Context, _ *sqlx.DB, tx *sqlx.Tx) error

type State

type State struct {
	ID    int64     `db:"id"`
	Name  string    `db:"name"`
	RunOn time.Time `db:"run_on"`
}

struct State represents an entry in the migrations table, structured to be compatible with nodejs db-migrate

Jump to

Keyboard shortcuts

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