Skip to content

Commit

Permalink
Implement SQL Database adapter (#79)
Browse files Browse the repository at this point in the history
Closes #69
  • Loading branch information
aeneasr authored Aug 16, 2019
1 parent ddd19f6 commit 79c1d97
Show file tree
Hide file tree
Showing 60 changed files with 2,471 additions and 791 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ jobs:
- TEST_SELFSERVICE_OIDC_HYDRA_ADMIN=http://127.0.0.1:4445
- TEST_SELFSERVICE_OIDC_HYDRA_PUBLIC=http://127.0.0.1:4444
- TEST_SELFSERVICE_OIDC_HYDRA_INTEGRATION_ADDR=127.0.0.1:4499
- TEST_DATABASE_POSTGRESQL=postgres://test:test@localhost:5432/hydra?sslmode=disable
- image: oryd/hydra:v1.0.0
environment:
- DSN=memory
- URLS_SELF_ISSUER=http://127.0.0.1:4444/
- URLS_LOGIN=http://127.0.0.1:4499/login
- URLS_CONSENT=http://127.0.0.1:4499/consent
command: serve all --dangerous-force-http
- image: postgres:9.6
environment:
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=hydra
working_directory: /go/src/github.com/ory/hive
steps:
- checkout
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ install:
packr2 || (GO111MODULE=on go install github.com/gobuffalo/packr/v2/packr2 && packr2)
GO111MODULE=on go install .
packr2 clean

# Adds sql files to the binary using go-bindata
.PHONY: sqlbin
sqlbin:
cd driver; go-bindata -o sql_migration_files.go -pkg driver ../contrib/sql/...


# Resets the test databases
.PHONY: resetdb
resetdb:
docker kill hydra_test_database_postgres || true
docker rm -f hydra_test_database_postgres || true
docker run --rm --name hydra_test_database_postgres -p 3445:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=hydra -d postgres:9.6
67 changes: 0 additions & 67 deletions cmd/all.go

This file was deleted.

104 changes: 104 additions & 0 deletions cmd/client/migrate.go
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
}
}
}
6 changes: 3 additions & 3 deletions cmd/daemon/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func telemetry(cmd *cobra.Command, n *negroni.Negroni, d driver.Driver) {
selfservice.BrowserRegistrationPath,
selfservice.BrowserRegistrationRequestsPath,
},
BuildVersion: d.BuildInfo().Version,
BuildHash: d.BuildInfo().Hash,
BuildTime: d.BuildInfo().Time,
BuildVersion: d.Registry().BuildVersion(),
BuildHash: d.Registry().BuildHash(),
BuildTime: d.Registry().BuildDate(),
},
)
n.Use(m)
Expand Down
14 changes: 0 additions & 14 deletions cmd/identities.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// 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 (
Expand Down
14 changes: 0 additions & 14 deletions cmd/identities_import.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// 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 (
Expand Down
15 changes: 15 additions & 0 deletions cmd/migrate.go
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)
}
49 changes: 49 additions & 0 deletions cmd/migrate_sql.go
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.")
}
14 changes: 0 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2018 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 (
Expand Down
6 changes: 6 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ import (
"strconv"

"github.com/spf13/cobra"

"github.com/ory/hive/cmd/daemon"
"github.com/ory/hive/driver"
)

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Run: func(cmd *cobra.Command, args []string) {
daemon.ServeAll(driver.NewDefaultDriver(logger, BuildVersion, BuildTime, BuildGitHash))(cmd, args)
},
}

func init() {
Expand Down
14 changes: 0 additions & 14 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// 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 (
Expand Down
Loading

0 comments on commit 79c1d97

Please sign in to comment.