forked from pepa58/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: implement better migration handling
- Loading branch information
Showing
27 changed files
with
343 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"strings" | ||
"github.com/jmoiron/sqlx" | ||
ladon "github.com/ory/ladon/manager/sql" | ||
"net/url" | ||
"github.com/ory/hydra/pkg" | ||
"time" | ||
"github.com/pkg/errors" | ||
"github.com/ory/hydra/config" | ||
"github.com/ory/hydra/jwk" | ||
"github.com/ory/hydra/warden/group" | ||
"github.com/ory/hydra/client" | ||
"github.com/ory/hydra/oauth2" | ||
"os" | ||
) | ||
|
||
type MigrateHandler struct { | ||
c *config.Config | ||
M *jwk.HTTPManager | ||
} | ||
|
||
func newMigrateHandler(c *config.Config) *MigrateHandler { | ||
return &MigrateHandler{ | ||
c: c, | ||
M: &jwk.HTTPManager{}, | ||
} | ||
} | ||
|
||
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}, | ||
} { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// migrateLadonCmd represents the ladon command | ||
var migrateLadonCmd = &cobra.Command{ | ||
Use: "ladon 0.6.0 <database-url>", | ||
Short: "Migrates Ladon SQL schema to version 0.6.0", | ||
Long: `Hydra version 0.8.0 includes a breaking schema change from Ladon which was introduced | ||
with Ladon version 0.6.0. This script applies the neccessary migrations by copying data from the old tables | ||
to the new ones. This command might take some time, depending on how many policies are in your store. | ||
Do not run this command on a fresh installation. | ||
It is recommended to run this command close to the SQL instance (e.g. same subnet) instead of over the public internet. | ||
This decreases risk of failure and decreases time required. | ||
### WARNING ### | ||
Before running this command on an existing database, create a back up! | ||
`, | ||
Run:cmdHandler.Migration.MigrateLadon050To060, | ||
} | ||
|
||
func init() { | ||
migrateCmd.AddCommand(migrateLadonCmd) | ||
} |
Oops, something went wrong.