/
command.go
49 lines (45 loc) · 1.02 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package migrate
import (
"database/sql"
"fmt"
"github.com/MigAru/poseidon/cmd/commands"
"github.com/MigAru/poseidon/cmd/commands/migrate/migrations"
_ "github.com/mattn/go-sqlite3"
goose "github.com/pressly/goose/v3"
"github.com/spf13/cobra"
)
func newCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "run migrate migrations to sql",
Long: "run migrate migrations to sql | need exec with flags in package goose",
Run: func(cmd *cobra.Command, args []string) {
cfg := newConfig()
if cfg == nil {
panic("in parse cfg has been error")
}
conn, err := sql.Open(cfg.Driver, cfg.DSN)
if err != nil {
panic(err)
}
fmt.Println(args[1:])
goose.SetBaseFS(migrations.Migrations)
if err := goose.SetDialect(cfg.Driver); err != nil {
panic(err)
}
if err := goose.Run(
args[0],
conn,
"schemas/"+cfg.Driver,
args[1:]...,
); err != nil {
panic(err)
}
},
}
cmd.DisableFlagParsing = true
return cmd
}
func init() {
commands.Add(newCommand())
}