migorm

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

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

Go to latest
Published: Sep 25, 2019 License: MIT Imports: 13 Imported by: 4

README

MIGORM

MIGORM is a helper tool for gorm framework allowing you to make changes in your database by creating migration files.

Quick start:

Install package

glide get github.com/carprice-tech/migorm

Then you must create a file for running migration commands (e.g. migrate.go)

package main

import (
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"github.com/jinzhu/gorm"
	"fmt"
	"github.com/carprice-tech/migorm"
)

func main() {

	db_user := ""
	db_pass := ""
	db_host := ""
	db_port := ""
	db_name := ""

	conStr := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8&parseTime=true&loc=Local", db_user, db_pass, db_host, db_port, db_name)
	dbConn, err := gorm.Open("mysql", conStr)

	if err != nil{
		panic(err)
	}

	migrater := migorm.NewMigrater(dbConn)
	migorm.Run(migrater)
}

For testing try create a new migration.

go run migrate.go make first_example

After that, a package with default name migrations will be created in the same directory. And the migration file (_first_example.go) will be created there.

.
├── cmd
│   ├── migrate.go
│   └── migrations
│       └── 1542223496_first_example.go
...

! You must import new created package with migrations in you migration run file (migrate.go)


import (
    _ "github.com/jinzhu/gorm/dialects/mysql"
    "github.com/jinzhu/gorm"
    "github.com/carprice-tech/migorm"
    _ "your/project/path/migrations"
)

Now you just need to insert your code into the up/down methods in the new created file (_first_example.go)

Run migrations for execution.

go run migrate.go

Successful migrations are inserted into the table by default: migrations. And in the future ones will be skipped.

Available commands:

go run migrate.go                                # run new migrations
go run migrate.go make my_new_migration          # create migration
go run migrate.go up 1542223496_first_example    # Up specific migration
go run migrate.go down 1542223496_first_example  # Down specific migration

Configuration

You can configure some parameters before performing migrations.


migrater := migorm.NewMigrater(db)

// don't forget import new package after create
_, file, _, _  := runtime.Caller(0)
curDir := path.Dir(file)
migrater.Conf().MigrationsDir = curDir + "/../migrations" // relative current file

migrater.Conf().TableName = "my_migrations"

migrater.Conf().Log = migorm.NewLogger() // or your implementation

migorm.Run(migrater)

Recomendations

// TODO

Authors

License

This project is licensed under the MIT License

Documentation

Index

Constants

View Source
const (
	InfoLevel  = "INFO"
	ErrorLevel = "ERROR"
)

Variables

This section is empty.

Functions

func RegisterMigration

func RegisterMigration(migration Migration)

Each migration file call this method in its init method

func Run

func Run(migrater Migrater)

Types

type Configurator

type Configurator struct {
	Log           Logger
	MigrationsDir string
	TableName     string
}

type Logger

type Logger interface {
	Infof(template string, args ...interface{})
	Errorf(template string, args ...interface{})
}

func NewLogger

func NewLogger() Logger

type Migrater

type Migrater interface {
	Conf() *Configurator
	UpMigrations() error
	UpConcreteMigration(name string) error
	DownConcreteMigration(name string) error
	MakeFileMigration(name string) error
}

func NewMigrater

func NewMigrater(db *gorm.DB) Migrater

type Migration

type Migration interface {
	Up(db *gorm.DB, log Logger) error
	Down(db *gorm.DB, log Logger) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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