-
Notifications
You must be signed in to change notification settings - Fork 52
/
refresh_schema.go
59 lines (50 loc) · 1.6 KB
/
refresh_schema.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
package branch
import (
"fmt"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
"github.com/planetscale/planetscale-go/planetscale"
"github.com/spf13/cobra"
)
func RefreshSchemaCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "refresh-schema <database> <branch>",
Short: "Refresh the schema for a database branch",
Args: cmdutil.RequiredArgs("database", "branch"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
database, branch := args[0], args[1]
client, err := ch.Client()
if err != nil {
return err
}
end := ch.Printer.PrintProgress(fmt.Sprintf("Refreshing schema for %s in %s", printer.BoldBlue(branch), printer.BoldBlue(database)))
defer end()
err = client.DatabaseBranches.RefreshSchema(ctx, &planetscale.RefreshSchemaRequest{
Organization: ch.Config.Organization,
Database: database,
Branch: branch,
})
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("branch %s does not exist in database %s (organization: %s)",
printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}
end()
if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Successfully refreshed schema for %s in %s.\n", printer.BoldBlue(branch), printer.BoldBlue(database))
return nil
}
return ch.Printer.PrintResource(
map[string]string{
"result": "schema refreshed",
},
)
},
}
return cmd
}