migrate

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2021 License: MIT Imports: 13 Imported by: 0

README

gorm migrate tool

how to use

gf by example

create a cmd.go file

package main

import (
	"gf-cms/app/command/migrate"

	"github.com/gookit/color"

	"github.com/gogf/gf/os/gcmd"
)

func main() {
	command := gcmd.GetArg(1)

	switch command {
	case "migrate":
		sub := gcmd.GetArg(2)
		switch sub {
		case "new":
			fileName := gcmd.GetArg(3)
			if len(fileName) == 0 {
				color.Error.Prompt("Doesn't have fileName")
			} else {
				migrate.New(fileName)
			}
		case "up":
			migrate.Up()
		case "down":
			migrate.Down()
		default:
			color.Warn.Prompt("Wrong Process method")
		}

	default:
		color.Warn.Prompt("Not correct command")
	}
}

in app/command/migrate/new.go

package migrate

import (
	migrate "github.com/Insua/gorm-migrate"
	"github.com/gookit/color"
)

func New(fileName string) {
	err := migrate.Create("database/migrations", "migrations", fileName)
	if err != nil {
		color.Red.Println(err)
	} else {
		color.Green.Println(fileName + " has be created")
	}
}

in app/command/migrate/migrate.go

package migrate

import (
	_ "gf-cms/boot"
	ms "gf-cms/database/migrations"
	"gf-cms/global"

	migrate "github.com/Insua/gorm-migrate"
	"github.com/gookit/color"
)

func Up() {
	migrations := ms.Migrations
	err := migrate.Up(global.DB, migrations)
	if err != nil {
		color.Red.Println(err)
	}
}

func Down() {
	migrations := ms.Migrations
	err := migrate.Down(global.DB, migrations)
	if err != nil {
		color.Red.Println(err)
	}
}
command

now you can use go run cmd.go migrate new file_name to create a migrate file, and go run cmd.go migrate up to migrate go rum cmd.go migrate down to rollback

for project example, a go frame cms called gf-cms which will specify the detail.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(baseDir, packageName, fileName string) error

func Down

func Down(db *gorm.DB, migrations []interface{}) error

func Up

func Up(db *gorm.DB, migrations []interface{}) error

Types

type Migration

type Migration struct {
	Id        uint   `gorm:"primaryKey;column:ID"`
	Migration string `gorm:"size:255;not null;uniqueIndex;column:MIGRATION"`
	Batch     uint   `gorm:"not null;column:BATCH"`
}

func (Migration) TableName added in v0.0.8

func (Migration) TableName() string

Jump to

Keyboard shortcuts

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