-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_migrate.go
174 lines (151 loc) · 4.62 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package cli
import (
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/ory/hydra/client"
"github.com/ory/hydra/config"
"github.com/ory/hydra/jwk"
"github.com/ory/hydra/oauth2"
"github.com/ory/hydra/pkg"
"github.com/ory/hydra/warden/group"
ladon "github.com/ory/ladon/manager/sql"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type MigrateHandler struct {
c *config.Config
}
func newMigrateHandler(c *config.Config) *MigrateHandler {
return &MigrateHandler{c: c}
}
type schemaCreator interface {
CreateSchemas() (int, error)
}
func (h *MigrateHandler) connectToSql(dsn string) (*sqlx.DB, error) {
var db *sqlx.DB
u, err := url.Parse(dsn)
if err != nil {
return nil, errors.Errorf("Could not parse DATABASE_URL: %s", err)
}
if err := pkg.Retry(h.c.GetLogger(), time.Second*15, time.Minute*2, func() error {
if u.Scheme == "mysql" {
dsn = strings.Replace(dsn, "mysql://", "", -1)
}
if db, err = sqlx.Open(u.Scheme, dsn); err != nil {
return errors.Errorf("Could not connect to SQL: %s", err)
} else if err := db.Ping(); err != nil {
return errors.Errorf("Could not connect to SQL: %s", err)
}
return nil
}); err != nil {
return nil, err
}
return db, nil
}
func (h *MigrateHandler) MigrateLadon050To060(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println(cmd.UsageString())
return
} else if args[0] != "0.6.0" {
fmt.Println(cmd.UsageString())
return
}
db, err := h.connectToSql(args[1])
if err != nil {
fmt.Printf("An error occurred while connecting to SQL: %s", err)
os.Exit(1)
return
}
if err := h.runMigrateLadon050To060(db); err != nil {
fmt.Printf("An error occurred while running the migrations: %s", err)
os.Exit(1)
return
}
}
func (h *MigrateHandler) runMigrateLadon050To060(db *sqlx.DB) error {
m := ladon.NewSQLManager(db, nil)
fmt.Printf("Applying `%s` SQL migrations.\n", "ladon")
if num, err := m.CreateSchemas("", "hydra_policy_migration"); err != nil {
return errors.Wrap(err, "Could not apply `ladon` SQL migrations")
} else {
fmt.Printf("Applied %d `%s` SQL migrations.\n", num, "ladon")
}
fmt.Println("Moving policies to new schema")
mm := ladon.SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7{
DB: db,
SQLManager: m,
}
if err := mm.Migrate(); err != nil {
return errors.Wrap(err, "Could not move policies to new schema")
}
fmt.Println("Migration successful!")
return nil
}
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println(cmd.UsageString())
return
}
db, err := h.connectToSql(args[0])
if err != nil {
fmt.Printf("An error occurred while connecting to SQL: %s", err)
os.Exit(1)
return
}
if err := h.runMigrateSQL(db); err != nil {
fmt.Printf("An error occurred while running the migrations: %s", err)
os.Exit(1)
return
}
fmt.Println("Migration successful!")
}
func (h *MigrateHandler) runMigrateSQL(db *sqlx.DB) error {
var total int
fmt.Printf("Applying `%s` SQL migrations...\n", "ladon")
if num, err := ladon.NewSQLManager(db, nil).CreateSchemas("", "hydra_policy_migration"); err != nil {
return errors.Wrap(err, "Could not apply `ladon` SQL migrations")
} else {
fmt.Printf("Applied %d `%s` SQL migrations.\n", num, "ladon")
total += num
}
for k, m := range map[string]schemaCreator{
"client": &client.SQLManager{DB: db},
"oauth2": &oauth2.FositeSQLStore{DB: db},
"jwk": &jwk.SQLManager{DB: db},
"group": &group.SQLManager{DB: db},
"consent": oauth2.NewConsentRequestSQLManager(db),
} {
fmt.Printf("Applying `%s` SQL migrations...\n", k)
if num, err := m.CreateSchemas(); err != nil {
return errors.Wrapf(err, "Could not apply `%s` SQL migrations", k)
} else {
fmt.Printf("Applied %d `%s` SQL migrations.\n", num, k)
total += num
}
}
fmt.Printf("Migration successful! Applied a total of %d SQL migrations.\n", total)
return nil
}