Skip to content

Commit 17089bf

Browse files
authored
Merge d77ea36 into 425c977
2 parents 425c977 + d77ea36 commit 17089bf

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

cmd/cli/handler_migrate.go

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
"path/filepath"
1414
"regexp"
1515
"strings"
16+
"time"
1617

18+
"github.com/ory/x/popx"
1719
"github.com/ory/x/servicelocatorx"
1820

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

2931
"github.com/ory/hydra/v2/driver"
3032
"github.com/ory/hydra/v2/driver/config"
33+
"github.com/ory/hydra/v2/persistence"
3134
"github.com/ory/x/flagx"
3235
)
3336

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

262-
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err error) {
265+
func makePersister(cmd *cobra.Command, args []string) (p persistence.Persister, err error) {
263266
var d driver.Registry
264267

265268
if flagx.MustGetBool(cmd, "read-from-env") {
@@ -275,16 +278,16 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
275278
driver.SkipNetworkInit(),
276279
})
277280
if err != nil {
278-
return err
281+
return nil, err
279282
}
280283
if len(d.Config().DSN()) == 0 {
281284
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "When using flag -e, environment variable DSN must be set.")
282-
return cmdx.FailSilently(cmd)
285+
return nil, cmdx.FailSilently(cmd)
283286
}
284287
} else {
285288
if len(args) != 1 {
286289
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Please provide the database URL.")
287-
return cmdx.FailSilently(cmd)
290+
return nil, cmdx.FailSilently(cmd)
288291
}
289292
d, err = driver.New(
290293
cmd.Context(),
@@ -300,11 +303,17 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
300303
driver.SkipNetworkInit(),
301304
})
302305
if err != nil {
303-
return err
306+
return nil, err
304307
}
305308
}
309+
return d.Persister(), nil
310+
}
306311

307-
p := d.Persister()
312+
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err error) {
313+
p, err := makePersister(cmd, args)
314+
if err != nil {
315+
return err
316+
}
308317
conn := p.Connection(context.Background())
309318
if conn == nil {
310319
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Migrations can only be executed against a SQL-compatible driver but DSN is not a SQL source.")
@@ -349,3 +358,47 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
349358
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Successfully applied migrations!")
350359
return nil
351360
}
361+
362+
func (h *MigrateHandler) MigrateStatus(cmd *cobra.Command, args []string) error {
363+
p, err := makePersister(cmd, args)
364+
if err != nil {
365+
return err
366+
}
367+
conn := p.Connection(context.Background())
368+
if conn == nil {
369+
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Migrations can only be checked against a SQL-compatible driver but DSN is not a SQL source.")
370+
return cmdx.FailSilently(cmd)
371+
}
372+
373+
if err := conn.Open(); err != nil {
374+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not open the database connection:\n%+v\n", err)
375+
return cmdx.FailSilently(cmd)
376+
}
377+
378+
block := flagx.MustGetBool(cmd, "block")
379+
ctx := cmd.Context()
380+
s, err := p.MigrationStatus(ctx)
381+
if err != nil {
382+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get migration status: %+v\n", err)
383+
return cmdx.FailSilently(cmd)
384+
}
385+
386+
for block && s.HasPending() {
387+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Waiting for migrations to finish...\n")
388+
for _, m := range s {
389+
if m.State == popx.Pending {
390+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " - %s\n", m.Name)
391+
}
392+
}
393+
time.Sleep(time.Second)
394+
s, err = p.MigrationStatus(ctx)
395+
if err != nil {
396+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get migration status: %+v\n", err)
397+
return cmdx.FailSilently(cmd)
398+
}
399+
}
400+
401+
cmdx.PrintTable(cmd, s)
402+
return nil
403+
404+
}

cmd/migrate_status.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright © 2023 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"github.com/ory/x/configx"
8+
"github.com/ory/x/servicelocatorx"
9+
10+
"github.com/spf13/cobra"
11+
12+
"github.com/ory/hydra/v2/cmd/cli"
13+
"github.com/ory/hydra/v2/driver"
14+
)
15+
16+
func NewMigrateStatusCmd(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModifier, cOpts []configx.OptionModifier) *cobra.Command {
17+
cmd := &cobra.Command{
18+
Use: "status",
19+
Short: "Get the current migration status",
20+
RunE: cli.NewHandler(slOpts, dOpts, cOpts).Migration.MigrateStatus,
21+
}
22+
23+
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.")
24+
cmd.Flags().Bool("block", false, "Block until all migrations have been applied")
25+
26+
return cmd
27+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func RegisterCommandRecursive(parent *cobra.Command, slOpts []servicelocatorx.Op
7373
migrateCmd := NewMigrateCmd()
7474
migrateCmd.AddCommand(NewMigrateGenCmd())
7575
migrateCmd.AddCommand(NewMigrateSqlCmd(slOpts, dOpts, cOpts))
76+
migrateCmd.AddCommand(NewMigrateStatusCmd(slOpts, dOpts, cOpts))
7677

7778
serveCmd := NewServeCmd()
7879
serveCmd.AddCommand(NewServeAdminCmd(slOpts, dOpts, cOpts))

0 commit comments

Comments
 (0)