-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
migrate.go
127 lines (107 loc) · 2.78 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package cliclient
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"github.com/ory/x/servicelocatorx"
"github.com/pkg/errors"
"github.com/ory/x/contextx"
"github.com/ory/x/configx"
"github.com/spf13/cobra"
"github.com/ory/kratos/driver"
"github.com/ory/kratos/driver/config"
"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
)
type MigrateHandler struct{}
func NewMigrateHandler() *MigrateHandler {
return &MigrateHandler{}
}
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) error {
var d driver.Registry
var err error
if flagx.MustGetBool(cmd, "read-from-env") {
d, err = driver.NewWithoutInit(
cmd.Context(),
cmd.ErrOrStderr(),
servicelocatorx.NewOptions(),
nil,
[]configx.OptionModifier{
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
})
if err != nil {
return err
}
if len(d.Config().DSN(cmd.Context())) == 0 {
fmt.Println(cmd.UsageString())
fmt.Println("")
fmt.Println("When using flag -e, environment variable DSN must be set")
return cmdx.FailSilently(cmd)
}
if err != nil {
return err
}
} else {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return cmdx.FailSilently(cmd)
}
d, err = driver.NewWithoutInit(
cmd.Context(),
cmd.ErrOrStderr(),
servicelocatorx.NewOptions(),
nil,
[]configx.OptionModifier{
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
configx.WithValue(config.ViperKeyDSN, args[0]),
})
if err != nil {
return err
}
}
err = d.Init(cmd.Context(), &contextx.Default{}, driver.SkipNetworkInit)
if err != nil {
return errors.Wrap(err, "an error occurred initializing migrations")
}
var plan bytes.Buffer
_, err = d.Persister().MigrationStatus(cmd.Context())
if err != nil {
return errors.Wrap(err, "an error occurred planning migrations:")
}
if !flagx.MustGetBool(cmd, "yes") {
fmt.Println("The following migration is planned:")
fmt.Println("")
fmt.Printf("%s", plan.String())
fmt.Println("")
fmt.Println("To skip the next question use flag --yes (at your own risk).")
if !askForConfirmation("Do you wish to execute this migration plan?") {
fmt.Println("Migration aborted.")
return cmdx.FailSilently(cmd)
}
}
if err = d.Persister().MigrateUp(cmd.Context()); err != nil {
return err
}
fmt.Println("Successfully applied SQL migrations!")
return nil
}
func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n]: ", s)
response, err := reader.ReadString('\n')
cmdx.Must(err, "%s", err)
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}