-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
handler_migrate.go
112 lines (94 loc) · 2.54 KB
/
handler_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
package cli
import (
"context"
"fmt"
"os"
"github.com/ory/x/configx"
"github.com/ory/x/errorsx"
"github.com/ory/x/cmdx"
"github.com/spf13/cobra"
"github.com/ory/hydra/driver"
"github.com/ory/hydra/driver/config"
"github.com/ory/x/flagx"
)
type MigrateHandler struct{}
func newMigrateHandler() *MigrateHandler {
return &MigrateHandler{}
}
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
var d driver.Registry
if flagx.MustGetBool(cmd, "read-from-env") {
d = driver.New(
cmd.Context(),
driver.WithOptions(
configx.SkipValidation(),
configx.WithFlags(cmd.Flags())),
driver.DisableValidation(),
driver.DisablePreloading())
if len(d.Config().DSN()) == 0 {
fmt.Println(cmd.UsageString())
fmt.Println("")
fmt.Println("When using flag -e, environment variable DSN must be set")
os.Exit(1)
return
}
} else {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
os.Exit(1)
return
}
d = driver.New(
cmd.Context(),
driver.WithOptions(
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
configx.WithValue(config.KeyDSN, args[0]),
),
driver.DisableValidation(),
driver.DisablePreloading())
}
p := d.Persister()
conn := p.Connection(context.Background())
if conn == nil {
fmt.Println(cmd.UsageString())
fmt.Println("")
fmt.Printf("Migrations can only be executed against a SQL-compatible driver but DSN is not a SQL source.\n")
os.Exit(1)
return
}
if err := conn.Open(); err != nil {
fmt.Printf("Could not open the database connection:\n%+v\n", err)
os.Exit(1)
return
}
// convert migration tables
if err := p.PrepareMigration(context.Background()); err != nil {
fmt.Printf("Could not convert the migration table:\n%+v\n", err)
os.Exit(1)
return
}
// print migration status
fmt.Println("The following migration is planned:")
fmt.Println("")
status, err := p.MigrationStatus(context.Background())
if err != nil {
fmt.Printf("Could not get the migration status:\n%+v\n", errorsx.WithStack(err))
os.Exit(1)
return
}
_ = status.Write(os.Stdout)
if !flagx.MustGetBool(cmd, "yes") {
fmt.Println("")
fmt.Println("To skip the next question use flag --yes (at your own risk).")
if !cmdx.AskForConfirmation("Do you wish to execute this migration plan?", nil, nil) {
fmt.Println("Migration aborted.")
return
}
}
// apply migrations
if err := p.MigrateUp(context.Background()); err != nil {
fmt.Printf("Could not apply migrations:\n%+v\n", errorsx.WithStack(err))
}
fmt.Println("Successfully applied migrations!")
}