-
Notifications
You must be signed in to change notification settings - Fork 968
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SQL Database adapter (#79)
Closes #69
- Loading branch information
Showing
60 changed files
with
2,471 additions
and
791 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 was deleted.
Oops, something went wrong.
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,104 @@ | ||
package client | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/viper" | ||
"github.com/ory/x/cmdx" | ||
"github.com/ory/x/flagx" | ||
"github.com/ory/x/logrusx" | ||
|
||
"github.com/ory/hive/driver" | ||
"github.com/ory/hive/driver/configuration" | ||
) | ||
|
||
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(), "", "", "") | ||
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(), "", "", "") | ||
} | ||
|
||
reg, ok := d.Registry().(*driver.RegistrySQL) | ||
if !ok { | ||
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 | ||
} | ||
|
||
u, err := url.Parse(d.Configuration().DSN()) | ||
if err != nil { | ||
fmt.Println(cmd.UsageString()) | ||
fmt.Println("") | ||
fmt.Println(err) | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
plan, err := reg.SchemaMigrationPlan(u.Scheme) | ||
cmdx.Must(err, "An error occurred planning migrations: %s", err) | ||
|
||
fmt.Println("The following migration is planned:") | ||
fmt.Println("") | ||
plan.Render() | ||
|
||
if !flagx.MustGetBool(cmd, "yes") { | ||
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 | ||
} | ||
} | ||
|
||
n, err := reg.CreateSchemas(u.Scheme) | ||
cmdx.Must(err, "An error occurred while connecting to SQL: %s", err) | ||
fmt.Printf("Successfully applied %d SQL migrations!\n", n) | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} |
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,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// migrateCmd represents the migrate command | ||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Various migration helpers", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(migrateCmd) | ||
} |
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,49 @@ | ||
/* | ||
Copyright © 2019 NAME HERE <EMAIL ADDRESS> | ||
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. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/hive/cmd/client" | ||
) | ||
|
||
// migrateSqlCmd represents the sql command | ||
var migrateSqlCmd = &cobra.Command{ | ||
Use: "sql <database-url>", | ||
Short: "Create SQL schemas and apply migration plans", | ||
Long: `Run this command on a fresh SQL installation and when you upgrade ORY Hive to a new minor version. | ||
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. | ||
You can read in the database URL using the -e flag, for example: | ||
export DSN=... | ||
hive migrate sql -e | ||
### WARNING ### | ||
Before running this command on an existing database, create a back up! | ||
`, | ||
Run: client.NewMigrateHandler().MigrateSQL, | ||
} | ||
|
||
func init() { | ||
migrateCmd.AddCommand(migrateSqlCmd) | ||
|
||
migrateSqlCmd.Flags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.") | ||
migrateSqlCmd.Flags().BoolP("yes", "y", false, "If set all confirmation requests are accepted without user interaction.") | ||
} |
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
Oops, something went wrong.