spirali

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2017 License: MIT Imports: 15 Imported by: 0

README

spirali

spirali is a go based database migration tool.

GoDoc wercker status

Overview

  • Have create, up, down, status command
  • Be able to use in golang code (this is flexible more than the CLI)
  • Customizable versioning
  • Taking bindata into consideration

Installation

$ go get github.com/kudohamu/spirali/cmd/spirali

You can check with the below command whether spirali install

$ spirali -h

Usage

config file

Prepare config file in accordance with below (file type is .toml ).

[dev]
dsn=""
driver="mysql" # only mysql now...
directory="" # relative path for directory of migration files from working directory.

[test]
dsn=""
driver="mysql"
directory=""
Create

Create new migration files.

$ spirali --path=path/to/your/config/file --env=dev create create_user_table
Status

Show current migration status.
format: applied time | migration file name

$ spirali --path=path/to/your/config/file --env=dev status
migration status of `dev` environment
==============================================================
 2017-04-02 17:47:14 | 20170402174358_create_foo_table
 2017-04-04 23:10:10 | 20170402174720_create_bar_table
 not applied         | 20170404230832_create_baz_table
==============================================================
Up

Apply remaining migrations.

$ spirali --path=path/to/your/config/file --env=dev up
Down

Roll back the latest migration.

$ spirali --path=path/to/your/config/file --env=dev down

Use in golang code

spirali.Create(vg VersionG, name string, config *Config, metadata *MetaData) (*MetaData, error)

spirali.Up(metadata *MetaData, config *Config, driver Driver, readable Readable) error

spirali.Down(metadata *MetaData, config *Config, driver Driver, readable Readable) error

spirali.Status(metadata *MetaData, config *Config, driver Driver, w io.Writer) error

(TODO: More friendly code sample)

Driver?

You can generate driver with below code.

driver, err := spirali.NewDriver(config)
VersionG?

You can customize versioning of migration files.

type VersionG interface {
  GenerateNextVersion() (uint64, error)                        // generates next version.
  IsSmall(targetVersion uint64, comparisonVersion uint64) bool // returns whether targetVersion is smaller than comparisonVersion.
}

Now spirali prepares two generators.

  • TimestampBasedVersionG (versioning with YYYYMMDDhhmmss format)
  • IncrementalVersionG (versioning with 1, 2, 3, ... format)

(Be able to use only TimestampBasedVersionG in CLI now.)

Readable?

Spirali abstracts directory to read migration files.

type Readable interface {
  Read(path string) ([]byte, error)
}

In CLI, spirali uses spirali.Dir. If you want to read migration files from bindata, you can use spirali.Bindata in your golang code.

readable := spirali.NewReadableFromBindata(bindata.Asset)

TODO

  • More drivers (postgresql, SQLite3)
  • More useful commands

More details

See godoc.

Contributing

Please feel free to submit issues, and send pull requests.

License

Released under the MIT License.

Documentation

Index

Constants

View Source
const MetaDataFileName = "metadata.json"

MetaDataFileName ...

Variables

View Source
var (
	ErrUnknownDriver         = errors.New("unknown driver")
	ErrEnvNotFound           = errors.New("env not found in config")
	ErrMigrationFileNotFound = errors.New("migration file not found")
	ErrMigrationsNotExist    = errors.New("migrations not exist")
	ErrSchemaVersionIsZero   = errors.New("schema version is 0")
)

Various errors the spirali might return.

Functions

func Down

func Down(metadata *MetaData, config *Config, driver Driver, readable Readable) error

Down rolls back the latest migration.

func Status added in v1.0.0

func Status(metadata *MetaData, config *Config, driver Driver, w io.Writer) error

Status writes current migration status to io.Writer.

func Up

func Up(metadata *MetaData, config *Config, driver Driver, readable Readable) error

Up applies migrations not applied.

Types

type Bindata

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

Bindata represents just `bindata`.

func NewReadableFromBindata

func NewReadableFromBindata(asset func(string) ([]byte, error)) *Bindata

NewReadableFromBindata ...

func (*Bindata) Read

func (b *Bindata) Read(path string) ([]byte, error)

Read ...

func (*Bindata) WithBasePath added in v1.0.0

func (b *Bindata) WithBasePath(basePath string) *Bindata

WithBasePath sets a base path when read data from bindata.

type Config

type Config struct {
	Env  string
	Path string
	// contains filtered or unexported fields
}

Config represents the spirali configuration.

func ReadConfig

func ReadConfig(r io.Reader) (*Config, error)

ReadConfig reads the spirali configuration from io.Reader.

func (*Config) Dir

func (c *Config) Dir() (string, error)

Dir returns migration files directory.

func (*Config) Driver

func (c *Config) Driver() (string, error)

Driver returns driver of current env.

func (*Config) Dsn

func (c *Config) Dsn() (string, error)

Dsn returns dsn of current env.

func (*Config) WithEnv

func (c *Config) WithEnv(env string) *Config

WithEnv sets env to config.

func (*Config) WithPath

func (c *Config) WithPath(path string) *Config

WithPath sets path to config.

type Dir

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

Dir represents directory.

func NewReadableFromDir

func NewReadableFromDir(basePath string) *Dir

NewReadableFromDir ...

func (*Dir) Read

func (d *Dir) Read(path string) ([]byte, error)

Read ...

type Driver

type Driver interface {
	Close() error
	CreateVersionTableIfNotExists() error
	DeleteVersion(version uint64) error
	Exec(query string) error
	GetAppliedTimeList() (map[uint64]time.Time, error)
	GetCurrentVersion() (uint64, error)
	Open(dsn string) error
	SetVersion(version uint64) error
	Transaction(fn func() error) error
}

Driver is interface of database driver.

func NewDriver

func NewDriver(c *Config) (Driver, error)

NewDriver separates out actual sql driver.

type IncrementalVersionG

type IncrementalVersionG struct {
	CurrentVersion uint64
}

IncrementalVersionG is the incremental version generator.

func (*IncrementalVersionG) GenerateNextVersion

func (vg *IncrementalVersionG) GenerateNextVersion() (uint64, error)

GenerateNextVersion ...

func (*IncrementalVersionG) IsSmall

func (vg *IncrementalVersionG) IsSmall(targetVersion uint64, comparisonVersion uint64) bool

IsSmall ...

type MetaData

type MetaData struct {
	Migrations Migrations `json:"migrations"`
}

MetaData of migration.

func Create

func Create(vg VersionG, name string, config *Config, metadata *MetaData) (*MetaData, error)

Create generates new migration files.

func ReadMetaData

func ReadMetaData(r io.Reader) (*MetaData, error)

ReadMetaData reads the metadata of migration from io.Reader.

func (*MetaData) Save

func (m *MetaData) Save(w io.Writer) error

Save updates the metadata file.

type Migration

type Migration struct {
	Version uint64 `json:"version"`
	Name    string `json:"name"`
}

Migration is ...

func NewMigration

func NewMigration(vg VersionG, name string) (*Migration, error)

NewMigration initialize migration struct.

func (*Migration) GetDownFileName

func (m *Migration) GetDownFileName() string

GetDownFileName generate file name for rolled back migration.

func (*Migration) GetUpFileName

func (m *Migration) GetUpFileName() string

GetUpFileName generate file name for applied migration.

type Migrations

type Migrations []*Migration

Migrations is array of migration.

func (Migrations) Down

func (ms Migrations) Down(driver Driver, readable Readable) error

Down rolls back the latest migration.

func (Migrations) Up

func (ms Migrations) Up(driver Driver, currentVersion uint64, readable Readable) error

Up applies migrations not applied.

type Readable

type Readable interface {
	Read(path string) ([]byte, error)
}

Readable represents abstraction that readable binary from path.

type TimestampBasedVersionG

type TimestampBasedVersionG struct{}

TimestampBasedVersionG is the timestamp based version generator.

func (*TimestampBasedVersionG) GenerateNextVersion

func (vg *TimestampBasedVersionG) GenerateNextVersion() (uint64, error)

GenerateNextVersion ...

func (*TimestampBasedVersionG) IsSmall

func (vg *TimestampBasedVersionG) IsSmall(targetVersion uint64, comparisonVersion uint64) bool

IsSmall ...

type VersionG

type VersionG interface {
	GenerateNextVersion() (uint64, error)                        // generates next version.
	IsSmall(targetVersion uint64, comparisonVersion uint64) bool // returns whether targetVersion is smaller than comparisonVersion.
}

VersionG represents the version generator of migration.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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