morph

package module
v0.0.0-...-f9a9926 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 16 Imported by: 0

README

GitHub Workflow Status (branch) GoDoc

Morph

Morph is a database migration tool that helps you to apply your migrations. It is written with Go so you can use it from your Go application as well.

Usage

It can be used as a library or a CLI tool.

Library
import (
    "context"

    "github.com/flywave/morph"
    "github.com/flywave/morph/drivers/mysql"
    "github.com/flywave/morph/sources/embedded"
)

src, err := embedded.WithInstance(&embedded.AssetSource{
    Names: []string{}, // add migration file names
    AssetFunc: func(name string) ([]byte, error) {
        return []byte{}, nil // should return the file contents
    },
})
if err != nil {
    return err
}
defer src.Close()

driver, err := mysql.WithInstance(db, &mysql.Config{})
if err != nil {
    return err
}

engine, err := morph.New(context.Background(), driver, src)
if err != nil {
    return err
}
defer engine.Close()

engine.ApplyAll()

CLI

To install morph you can use:

go install github.com/flywave/morph/cmd/morph@latest

Then you can apply your migrations like below:

morph apply up --driver postgres --dsn "postgres://user:pass@localhost:5432/mydb?sslmode=disable" --path ./db/migrations/postgres --number 1

Migration Files

The migrations files should have an up and down versions. The program requires each migration to be reversible, and the naming of the migration should be in the following form:

0000000001_create_user.up.sql
0000000001_create_user.down.sql

The first part will be used to determine the order in which the migrations should be applied and the next part until the up|down.sql suffix will be the migration name.

The program requires this naming convention to be followed as it saves the order and names of the migrations. Also, it can rollback migrations with the down files.

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorLogger      = color.New(color.FgRed, color.Bold)
	ErrorLoggerLight = color.New(color.FgRed)
	InfoLogger       = color.New(color.FgCyan, color.Bold)
	InfoLoggerLight  = color.New(color.FgCyan)
	SuccessLogger    = color.New(color.FgGreen, color.Bold)
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Logger      Logger
	LockTimeout time.Duration
	LockKey     string
}

type EngineOption

type EngineOption func(*Morph)

func SetMigrationTableName

func SetMigrationTableName(name string) EngineOption

func SetSatementTimeoutInSeconds

func SetSatementTimeoutInSeconds(n int) EngineOption

func WithLock

func WithLock(key string) EngineOption

WithLock creates a lock table in the database so that the migrations are guaranteed to be executed from a single instance. The key is used for naming the mutex.

func WithLockTimeout

func WithLockTimeout(lockTimeout time.Duration) EngineOption

func WithLogger

func WithLogger(logger Logger) EngineOption

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

type Morph

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

func New

func New(ctx context.Context, driver drivers.Driver, source sources.Source, options ...EngineOption) (*Morph, error)

New creates a new instance of the migrations engine from an existing db instance and a migrations source. If the driver implements the Lockable interface, it will also wait until it has acquired a lock.

func (*Morph) Apply

func (m *Morph) Apply(limit int) (int, error)

Applies limited number of migrations upwards.

func (*Morph) ApplyAll

func (m *Morph) ApplyAll() error

ApplyAll applies all pending migrations.

func (*Morph) ApplyDown

func (m *Morph) ApplyDown(limit int) (int, error)

ApplyDown rollbacks a limited number of migrations if limit is given below zero, all down scripts are going to be applied.

func (*Morph) Close

func (m *Morph) Close() error

Close closes the underlying database connection of the engine.

Directories

Path Synopsis
cmd
mysql
Initial code generated by generator.
Initial code generated by generator.

Jump to

Keyboard shortcuts

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