mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-25 06:36:03 +03:00
[feature] migration generator
This commit is contained in:
parent
2a114dee69
commit
c3a11afa3b
@ -84,4 +84,10 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
### Migration generator
|
||||
This library contains helper tool for transports. You can install it via go:
|
||||
```sh
|
||||
$ go get -u github.com/retailcrm/mg-transport-core/cmd/transport-core-tool
|
||||
```
|
||||
Currently, it only can generate new migrations for your transport.
|
37
cmd/transport-core-tool/main.go
Normal file
37
cmd/transport-core-tool/main.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/retailcrm/mg-transport-core/core"
|
||||
)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
var (
|
||||
options = Options{}
|
||||
parser = flags.NewParser(&options, flags.Default)
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, err := parser.AddCommand("migration",
|
||||
"Create new empty migration in specified directory.",
|
||||
"Create new empty migration in specified directory.",
|
||||
&core.NewMigrationCommand{},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if _, err := parser.Parse(); err != nil {
|
||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
} else {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
84
core/migration_generator.go
Normal file
84
core/migration_generator.go
Normal file
@ -0,0 +1,84 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var migrationTemplate = `package $package
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/retailcrm/mg-transport-core/core"
|
||||
"gopkg.in/gormigrate.v1"
|
||||
)
|
||||
|
||||
func init() {
|
||||
core.Migrations().Add(&gormigrate.Migration{
|
||||
ID: "$version",
|
||||
Migrate: func(db *gorm.DB) error {
|
||||
// Write your migration code here...
|
||||
},
|
||||
Rollback: func(db *gorm.DB) error {
|
||||
// Write your migration rollback code here...
|
||||
},
|
||||
})
|
||||
}
|
||||
`
|
||||
|
||||
// NewMigrationCommand struct
|
||||
type NewMigrationCommand struct {
|
||||
Directory string `short:"d" long:"directory" default:"./migrations" description:"Directory where migration will be created"`
|
||||
}
|
||||
|
||||
func (x *NewMigrationCommand) FileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func (x *NewMigrationCommand) Execute(args []string) error {
|
||||
version := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
directory := path.Clean(x.Directory)
|
||||
packageName := "migrations"
|
||||
|
||||
if _, err := os.Stat(directory); os.IsNotExist(err) {
|
||||
return fmt.Errorf("err: specified directory doesn't exist")
|
||||
}
|
||||
|
||||
if base := path.Base(directory); base != "/" && base != "." {
|
||||
packageName = base
|
||||
}
|
||||
|
||||
filePath := path.Join(directory, version+"_app.go")
|
||||
if x.FileExists(filePath) {
|
||||
return fmt.Errorf("\"%s\" already exists or it's a directory", filePath)
|
||||
}
|
||||
|
||||
migrationData := strings.Replace(
|
||||
strings.Replace(migrationTemplate, "$version", version, 1),
|
||||
"$package",
|
||||
packageName,
|
||||
1,
|
||||
)
|
||||
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
if _, err := file.WriteString(migrationData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Created new migration: " + filePath)
|
||||
return nil
|
||||
}
|
1
go.mod
1
go.mod
@ -16,6 +16,7 @@ require (
|
||||
github.com/golang/protobuf v1.3.2 // indirect
|
||||
github.com/google/go-querystring v1.0.0 // indirect
|
||||
github.com/h2non/gock v1.0.10
|
||||
github.com/jessevdk/go-flags v1.4.0 // indirect
|
||||
github.com/jinzhu/gorm v1.9.10
|
||||
github.com/json-iterator/go v1.1.7 // indirect
|
||||
github.com/lib/pq v1.2.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -87,6 +87,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jinzhu/gorm v1.9.2 h1:lCvgEaqe/HVE+tjAR2mt4HbbHAZsQOv3XAZiEZV37iw=
|
||||
github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo=
|
||||
github.com/jinzhu/gorm v1.9.10 h1:HvrsqdhCW78xpJF67g1hMxS6eCToo9PZH4LDB8WKPac=
|
||||
|
Loading…
Reference in New Issue
Block a user