-
Notifications
You must be signed in to change notification settings - Fork 0
/
up.go
81 lines (71 loc) · 1.91 KB
/
up.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
package cmd
import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vippsas/sqlcode"
"strings"
)
var (
upCmd = &cobra.Command{
Use: "up <dbname>:<schemasuffix>",
Short: "Uploads the SQL code to the SQL database configured in sqlcode.yaml",
Long: "Uploads the SQL code to an SQL database. The target is specified using a :-delimiter, with the database name first and schemasuffix last",
RunE: func(cmd *cobra.Command, args []string) error {
logger := logrus.StandardLogger()
ctx := context.Background()
if len(args) != 1 {
_ = cmd.Help()
return errors.New("Wrong number of arguments")
}
target := args[0]
targetParts := strings.Split(target, ":")
if len(targetParts) != 2 {
if len(args) != 1 {
_ = cmd.Help()
return errors.New("Illegal target, should contain exactly one ':'")
}
}
dbname := targetParts[0]
schemasuffix := targetParts[1]
config, err := LoadConfig()
if err != nil {
return err
}
dbconfig, ok := config.Databases[dbname]
if !ok {
return errors.New(fmt.Sprintf("database %s not present in configuration file", dbname))
}
dbc, err := dbconfig.Open(ctx, logger)
if err != nil {
return err
}
exists, err := sqlcode.Exists(ctx, dbc, schemasuffix)
if err != nil {
return err
}
if exists {
fmt.Println(fmt.Sprintf("Schema [%s] already exists, removing", sqlcode.SchemaName(schemasuffix)))
if err := sqlcode.Drop(ctx, dbc, schemasuffix); err != nil {
return err
}
}
deployable, err := dep(false)
if err != nil {
return err
}
deployable = deployable.WithSchemaSuffix(schemasuffix)
err = deployable.DropAndUpload(ctx, dbc)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Schema [%s] successfully uploaded", sqlcode.SchemaName(schemasuffix)))
return nil
},
}
)
func init() {
rootCmd.AddCommand(upCmd)
}