This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
83 lines (68 loc) · 1.59 KB
/
migrate.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package db
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"goyave.dev/gyv/internal/command"
"goyave.dev/gyv/internal/inject"
)
// Migrate command for running auto migrations.
type Migrate struct {
command.ProjectPathCommand
}
// BuildCobraCommand builds the cobra command for this action
func (c *Migrate) BuildCobraCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "Run migrations",
Long: `Command to run database migrations.
If project-path is not specified, the nearest directory containing a go.mod file importing Goyave will be used.
`,
RunE: command.GenerateRunFunc(c),
}
c.setFlags(cmd.Flags())
return cmd
}
// BuildSurvey builds a survey for this action
func (c *Migrate) BuildSurvey() ([]*survey.Question, error) {
return []*survey.Question{}, nil
}
// Execute the command's behavior
func (c *Migrate) Execute() error {
seed, err := inject.Migrate(c.ProjectPath)
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
if err := os.Chdir(c.ProjectPath); err != nil {
return err
}
fmt.Println("💾 Running migrations...")
err = seed()
if err1 := os.Chdir(wd); err1 != nil && err == nil {
err = err1
}
if err != nil {
return err
}
fmt.Println("✅ Database migrated!")
return nil
}
// Validate checks if required flags are definded
func (c *Migrate) Validate() error {
return nil
}
func (c *Migrate) setFlags(flags *pflag.FlagSet) {
flags.StringVarP(
&c.ProjectPath,
"project-path",
"p",
"",
"The path to the Goyave project root",
)
}