-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_migrate.go
95 lines (80 loc) · 2.34 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
package cli
import (
"context"
"fmt"
"os"
"github.com/ory/x/cmdx"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/ory/hydra/driver"
"github.com/ory/hydra/driver/configuration"
"github.com/ory/viper"
"github.com/ory/x/flagx"
"github.com/ory/x/logrusx"
)
type MigrateHandler struct{}
func newMigrateHandler() *MigrateHandler {
return &MigrateHandler{}
}
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
var d driver.Driver
if flagx.MustGetBool(cmd, "read-from-env") {
d = driver.NewDefaultDriver(logrusx.New("", ""), false, nil, "", "", "", false)
if len(d.Configuration().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
}
viper.Set(configuration.ViperKeyDSN, args[0])
d = driver.NewDefaultDriver(logrusx.New("", ""), false, nil, "", "", "", false)
}
p := d.Registry().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("")
if err := p.MigrationStatus(context.Background(), os.Stdout); err != nil {
fmt.Printf("Could not get the migration status:\n%+v\n", errors.WithStack(err))
os.Exit(1)
return
}
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", errors.WithStack(err))
}
fmt.Println("Successfully applied migrations!")
}