/
make-migration.go
58 lines (48 loc) · 1.42 KB
/
make-migration.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
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"path"
"github.com/abibby/salusa/database/migrate"
"github.com/abibby/salusa/spice/pkg"
"github.com/abibby/salusa/spice/util"
"github.com/spf13/cobra"
)
// makeMigrationCmd represents the makeMigration command
var makeMigrationCmd = &cobra.Command{
Use: "make:migration [name]",
Short: "",
Long: ``,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := util.MigrationName(args)
c, err := util.LoadConfig(".")
if err != nil {
return err
}
err = os.MkdirAll(c.Migration.Dir, 0755)
if err != nil {
return err
}
run := "schema.Run(func(ctx context.Context, tx schema.DB) error {\n" +
"return nil\n" +
"})"
src, err := migrate.SrcFile(name, c.Migration.Pkg, pkg.Raw(run), pkg.Raw(run))
if err != nil {
return err
}
return os.WriteFile(path.Join(c.Migration.Dir, name+".go"), []byte(src), 0644)
},
}
func init() {
rootCmd.AddCommand(makeMigrationCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// makeMigrationCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// makeMigrationCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}