migo

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 13 Imported by: 0

README

Migo

migo is database migration tool. It supports .sql and .go type migrations. For now it have only gorm database connector.

Installation

migo has command line tool that creates new migrations. For installation use the next command: $ go get github.com/walkline/migo/cmd/migo

Usage

First of all you need to create a new migration.

cd path_with_migrations
migo new sql "create user table"

As result you will have two files:

1.0.0-create-user-table.up.sql # sql migration that will be used when we will want update database
1.0.0-create-user-table.down.sql # sql migration that will be used when we will want downgrade database

Than inside of your project you would need something simmilar to this:

package main

import (
	"os"

	"github.com/walkline/migo"
	"github.com/walkline/migo/connections/gormconnection"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
)

func Migrate(db *gorm.DB) {
	m := migo.NewMigrate(
		gormconnection.NewConnection(db),
		migo.NewSQLMigrationLoader(path),
		migo.DefaultGoMigrationLoader,
	)

	err := m.UpToLatest()
	if err != nil {
		panic(err)
	}

	migo.DefaultGoMigrationLoader.Clear()
}

func main() {
	db, _ := gorm.Open("postgres", "host=...")
	Migrate(db)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultGoMigrationLoader = &GoMigrationLoader{
	m: []Migration{},
}
View Source
var GoDefaultTemplateData = `` /* 1093-byte string literal not displayed */
View Source
var SQLDownDefaultTemplateData = `-- DOWN: {{.version.name}}
`
View Source
var SQLUpDefaultTemplateData = `-- UP: {{.version.name}}
`
View Source
var VersionDefaultTemplateData = `1.0.0.{{.timestamp}}`

Functions

This section is empty.

Types

type Connection

type Connection interface {
	Exec(sql string, values ...interface{}) error
	LoadVersions() ([]string, error)
	SetVersion(v string) error
	Tx() (Transaction, error)
}

type GoMigrationLoader

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

func (*GoMigrationLoader) Add

func (l *GoMigrationLoader) Add(m Migration)

func (*GoMigrationLoader) Clear

func (l *GoMigrationLoader) Clear()

func (*GoMigrationLoader) Load

func (l *GoMigrationLoader) Load() ([]Migration, error)

type Migrate

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

Migrate is entry point struct that can trigger database updating or downgrading

func NewMigrate

func NewMigrate(c Connection, loaders ...MigrationLoader) *Migrate

NewMigrate creates new struct that can start migration. `c` is a connection to database, you can use gorm connection. `loaders` is migrations loaders, it can be `sql` or `go` migrations loader

func (*Migrate) Add

func (m *Migrate) Add(migration Migration) error

Add adds migrations to the pool of pending migrations

func (*Migrate) ApplyLostAndPanic

func (m *Migrate) ApplyLostAndPanic() error

func (*Migrate) ApplyLostIfAny added in v1.0.1

func (m *Migrate) ApplyLostIfAny() error

func (*Migrate) DownWithSteps

func (m *Migrate) DownWithSteps(steps int) error

DownWithSteps runs migrations to downgrade database version. `steps` is number of latest migrations that needs to be unapplied.

func (*Migrate) SetConncetion

func (m *Migrate) SetConncetion(c Connection)

SetConncetion sets database connection that will be used for migration

func (*Migrate) UpToLatest

func (m *Migrate) UpToLatest() error

UpToLatest loads all needed migrations, filters migrations that already applied, and starts migration process.

type Migration

type Migration interface {
	SetConnection(c Connection)
	Version() Version
	SetVersion(v *Version)
	Up() error
	Down() error
}

type MigrationLoader

type MigrationLoader interface {
	Load() ([]Migration, error)
}

type MigrationsLoader

type MigrationsLoader interface {
	Load() ([]Migration, error)
}

type SQLMigration

type SQLMigration struct {
	UpFile   *os.File
	DownFile *os.File
	// contains filtered or unexported fields
}

func (*SQLMigration) Down

func (m *SQLMigration) Down() error

func (*SQLMigration) SetConnection

func (m *SQLMigration) SetConnection(c Connection)

func (*SQLMigration) SetVersion

func (m *SQLMigration) SetVersion(v *Version)

func (*SQLMigration) Up

func (m *SQLMigration) Up() error

func (*SQLMigration) Version

func (m *SQLMigration) Version() Version

type SQLMigrationsLoader

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

func NewSQLMigrationLoader

func NewSQLMigrationLoader(path string) *SQLMigrationsLoader

func (*SQLMigrationsLoader) Load

func (l *SQLMigrationsLoader) Load() ([]Migration, error)

type Template

type Template struct {
	Type TemplateType
	// contains filtered or unexported fields
}

func (*Template) Build

func (t *Template) Build(v *Version) ([]byte, error)

func (*Template) BuildVersion

func (t *Template) BuildVersion() (string, error)

type TemplateType

type TemplateType string
const (
	TemplateTypeSQLUp   TemplateType = "sql-up"
	TemplateTypeSQLDown TemplateType = "sql-down"
	TemplateTypeGo      TemplateType = "go"
	TemplateTypeVersion TemplateType = "version"
)

type Templater

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

func (*Templater) ContentForTemplateType

func (t *Templater) ContentForTemplateType(tmplType TemplateType, v *Version) ([]byte, error)

func (*Templater) LoadTemplates

func (t *Templater) LoadTemplates(path string) error

func (*Templater) TampleteWithType

func (t *Templater) TampleteWithType(tmplType TemplateType) *Template

type Transaction

type Transaction interface {
	Exec(sql string, values ...interface{}) error
	Commit() error
	Rollback() error
}

type Version

type Version struct {
	Name string
	// contains filtered or unexported fields
}

func GreatestVersion

func GreatestVersion(versions []Version) *Version

func StringsToVersions

func StringsToVersions(strs []string) ([]Version, error)

func VersionFromString

func VersionFromString(s string) (*Version, error)

func (Version) GreaterThan

func (v Version) GreaterThan(o *Version) bool

func (Version) GreaterThanOrEqual

func (v Version) GreaterThanOrEqual(o *Version) bool

func (Version) String

func (v Version) String() string

func (Version) StringWithoutName added in v1.0.2

func (v Version) StringWithoutName() string

Directories

Path Synopsis
cmd
connections

Jump to

Keyboard shortcuts

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