Skip to content

Commit

Permalink
Merge d77ea36 into 425c977
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Jul 24, 2023
2 parents 425c977 + d77ea36 commit 17089bf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
65 changes: 59 additions & 6 deletions cmd/cli/handler_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/ory/x/popx"
"github.com/ory/x/servicelocatorx"

"github.com/pkg/errors"
Expand All @@ -28,6 +30,7 @@ import (

"github.com/ory/hydra/v2/driver"
"github.com/ory/hydra/v2/driver/config"
"github.com/ory/hydra/v2/persistence"
"github.com/ory/x/flagx"
)

Expand Down Expand Up @@ -259,7 +262,7 @@ func (h *MigrateHandler) MigrateGen(cmd *cobra.Command, args []string) {
os.Exit(0)
}

func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err error) {
func makePersister(cmd *cobra.Command, args []string) (p persistence.Persister, err error) {

Check warning on line 265 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L265

Added line #L265 was not covered by tests
var d driver.Registry

if flagx.MustGetBool(cmd, "read-from-env") {
Expand All @@ -275,16 +278,16 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
driver.SkipNetworkInit(),
})
if err != nil {
return err
return nil, err

Check warning on line 281 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L281

Added line #L281 was not covered by tests
}
if len(d.Config().DSN()) == 0 {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "When using flag -e, environment variable DSN must be set.")
return cmdx.FailSilently(cmd)
return nil, cmdx.FailSilently(cmd)

Check warning on line 285 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L285

Added line #L285 was not covered by tests
}
} else {
if len(args) != 1 {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Please provide the database URL.")
return cmdx.FailSilently(cmd)
return nil, cmdx.FailSilently(cmd)

Check warning on line 290 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L290

Added line #L290 was not covered by tests
}
d, err = driver.New(
cmd.Context(),
Expand All @@ -300,11 +303,17 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
driver.SkipNetworkInit(),
})
if err != nil {
return err
return nil, err

Check warning on line 306 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L306

Added line #L306 was not covered by tests
}
}
return d.Persister(), nil

Check warning on line 309 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L309

Added line #L309 was not covered by tests
}

p := d.Persister()
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err error) {
p, err := makePersister(cmd, args)
if err != nil {
return err

Check warning on line 315 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L312-L315

Added lines #L312 - L315 were not covered by tests
}
conn := p.Connection(context.Background())
if conn == nil {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Migrations can only be executed against a SQL-compatible driver but DSN is not a SQL source.")
Expand Down Expand Up @@ -349,3 +358,47 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Successfully applied migrations!")
return nil
}

func (h *MigrateHandler) MigrateStatus(cmd *cobra.Command, args []string) error {
p, err := makePersister(cmd, args)
if err != nil {
return err

Check warning on line 365 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L362-L365

Added lines #L362 - L365 were not covered by tests
}
conn := p.Connection(context.Background())
if conn == nil {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Migrations can only be checked against a SQL-compatible driver but DSN is not a SQL source.")
return cmdx.FailSilently(cmd)

Check warning on line 370 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L367-L370

Added lines #L367 - L370 were not covered by tests
}

if err := conn.Open(); err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not open the database connection:\n%+v\n", err)
return cmdx.FailSilently(cmd)

Check warning on line 375 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L373-L375

Added lines #L373 - L375 were not covered by tests
}

block := flagx.MustGetBool(cmd, "block")
ctx := cmd.Context()
s, err := p.MigrationStatus(ctx)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)

Check warning on line 383 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L378-L383

Added lines #L378 - L383 were not covered by tests
}

for block && s.HasPending() {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Waiting for migrations to finish...\n")
for _, m := range s {
if m.State == popx.Pending {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " - %s\n", m.Name)

Check warning on line 390 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L386-L390

Added lines #L386 - L390 were not covered by tests
}
}
time.Sleep(time.Second)
s, err = p.MigrationStatus(ctx)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)

Check warning on line 397 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L393-L397

Added lines #L393 - L397 were not covered by tests
}
}

cmdx.PrintTable(cmd, s)
return nil

Check warning on line 402 in cmd/cli/handler_migrate.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/handler_migrate.go#L401-L402

Added lines #L401 - L402 were not covered by tests

}
27 changes: 27 additions & 0 deletions cmd/migrate_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/ory/x/configx"
"github.com/ory/x/servicelocatorx"

"github.com/spf13/cobra"

"github.com/ory/hydra/v2/cmd/cli"
"github.com/ory/hydra/v2/driver"
)

func NewMigrateStatusCmd(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModifier, cOpts []configx.OptionModifier) *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Get the current migration status",
RunE: cli.NewHandler(slOpts, dOpts, cOpts).Migration.MigrateStatus,
}

cmd.Flags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.")
cmd.Flags().Bool("block", false, "Block until all migrations have been applied")

return cmd
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func RegisterCommandRecursive(parent *cobra.Command, slOpts []servicelocatorx.Op
migrateCmd := NewMigrateCmd()
migrateCmd.AddCommand(NewMigrateGenCmd())
migrateCmd.AddCommand(NewMigrateSqlCmd(slOpts, dOpts, cOpts))
migrateCmd.AddCommand(NewMigrateStatusCmd(slOpts, dOpts, cOpts))

serveCmd := NewServeCmd()
serveCmd.AddCommand(NewServeAdminCmd(slOpts, dOpts, cOpts))
Expand Down

0 comments on commit 17089bf

Please sign in to comment.