Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Allows reading database from env in migrate sql #898

Merged
merged 1 commit into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions cmd/cli/handler_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/ory/hydra/pkg"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type MigrateHandler struct {
Expand Down Expand Up @@ -78,12 +79,24 @@ func (h *MigrateHandler) connectToSql(dsn string) (*sqlx.DB, error) {
}

func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println(cmd.UsageString())
return
var dburl string
if readFromEnv, _ := cmd.Flags().GetBool("read-from-env"); readFromEnv {
if len(viper.GetString("DATABASE_URL")) == 0 {
fmt.Println(cmd.UsageString())
fmt.Println("")
fmt.Println("When using flag -e, environment variable DATABASE_URL must be set")
return
}
dburl = viper.GetString("DATABASE_URL")
} else {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return
}
dburl = args[0]
}

db, err := h.connectToSql(args[0])
db, err := h.connectToSql(dburl)
if err != nil {
fmt.Printf("An error occurred while connecting to SQL: %s", err)
os.Exit(1)
Expand Down
6 changes: 6 additions & 0 deletions cmd/migrate_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ upgrading Hydra 0.7.0 to 0.8.0 requires running this command.
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 DATABASE_URL=...
hydra migrate sql -e

### WARNING ###

Before running this command on an existing database, create a back up!
Expand All @@ -41,4 +45,6 @@ Before running this command on an existing database, create a back up!

func init() {
migrateCmd.AddCommand(migrateSqlCmd)

migrateSqlCmd.Flags().BoolP("read-from-env", "e", false, "If set, reads the database URL from the environment variable DATABASE_URL.")
}
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ services:
dockerfile: Dockerfile
links:
- postgresd:postgresd
environment:
# - LOG_LEVEL=debug
- DATABASE_URL=postgres://hydra:secret@postgresd:5432/hydra?sslmode=disable
command:
migrate sql postgres://hydra:secret@postgresd:5432/hydra?sslmode=disable
migrate sql -e
restart: on-failure

hydra:
Expand Down