gorm_migration

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2020 License: MIT Imports: 5 Imported by: 3

README

GORM-based migration workaround

GitHub Workflow Status GitHub tag (latest by date)

  1. Write migration functions with Golang
  2. Register functions on MigrationManger
  3. Execute the migrate or rollback command

WHY

If you've used rails, you'll be impressed by its migration management.

gorm-migration is a clumsy parody, I've implemented basic migration management, including Check, Migrate and Rollback.

Install

go get github.com/icbd/gorm-migration

How to use

For some types of databases, you need to create the database first.

Migrate

It will perform all the tasks that haven't been run yet.

db := newMemoryDB()
mm := NewMigrationManger(db, Migrate)
mm.RegisterFunctions(
    mm.createUsersTable,
    mm.addAvatarToUsers,
    mm.addEmailIndexToUsers)
mm.Migrate()
Rollback

It will roll it back step by step.

# ...
mm.Type = Rollback
mm.Migrate()
Check
# ...
mm.Type = Check
mm.Migrate()

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MigrateType

type MigrateType int8
const (
	Check MigrateType = iota
	Migrate
	Rollback
)

func (*MigrateType) Set

func (t *MigrateType) Set(value string) error

Set for flag parse implement: type Value interface

func (*MigrateType) String

func (t *MigrateType) String() string

String for flag parse implement: type Value interface

type MigrationManger added in v0.0.2

type MigrationManger struct {
	DB         *gorm.DB
	Type       MigrateType
	Columns    []*SchemaMigration // saved in database
	Migrations []*SchemaMigration // registered by code
}

func NewMigrationManger

func NewMigrationManger(db *gorm.DB, t MigrateType) *MigrationManger

func (*MigrationManger) Change added in v0.0.2

func (mm *MigrationManger) Change(up, down changeFunc) error

Change migrate up or migrate down

func (*MigrationManger) ChangeColumn added in v0.0.2

func (mm *MigrationManger) ChangeColumn(dst interface{}, column string) error

ChangeColumn create or drop table

func (*MigrationManger) ChangeFuncWrap added in v0.0.2

func (mm *MigrationManger) ChangeFuncWrap(sqlLines ...string) changeFunc

func (*MigrationManger) ChangeTable added in v0.0.2

func (mm *MigrationManger) ChangeTable(dst interface{}) error

ChangeTable create or drop table

func (*MigrationManger) IsCompleted added in v0.0.2

func (mm *MigrationManger) IsCompleted() bool

IsCompleted fill mm Columns, and check Columns match with Migrations

func (*MigrationManger) Migrate added in v0.0.2

func (mm *MigrationManger) Migrate()

Migrate trigger

func (*MigrationManger) RegisterFunctions added in v0.0.2

func (mm *MigrationManger) RegisterFunctions(changeFunctions ...changeFunc) *MigrationManger

RegisterFunctions fill it by user

type SchemaMigration

type SchemaMigration struct {
	gorm.Model
	FuncName string
	// contains filtered or unexported fields
}

save to database

Jump to

Keyboard shortcuts

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