Skip to content

Commit

Permalink
refactor: move to common Ory migration helpers
Browse files Browse the repository at this point in the history
This patch brings Ory Keto SQL migrations in line with CLI patterns from Kraots and Hydra.

BREAKING CHANGE: Please update your migration scripts:

```patch
- keto migrate up
+ keto migrate sql up

- keto migrate down 2
+ keto migrate sql down --steps 2

- keto migrate status
+ keto migrate sql status
```
  • Loading branch information
aeneasr committed Dec 2, 2024
1 parent 6013a96 commit 8c286e6
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 351 deletions.
78 changes: 0 additions & 78 deletions cmd/migrate/down.go

This file was deleted.

12 changes: 6 additions & 6 deletions cmd/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestMigrate(t *testing.T) {

cmd := newCmd(ctx, "-c", cf)

out := cmd.ExecNoErr(t, "up", "--"+FlagYes)
out := cmd.ExecNoErr(t, "up", "-y")
assert.Contains(t, out, "All migrations are already applied, there is nothing to do.")
})
})
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestMigrate(t *testing.T) {

t.Cleanup(func() {
// migrate all down
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "--steps", "10000", "-y"))
})

parts := strings.Split(stdOut, "Are you sure that you want to apply this migration?")
Expand All @@ -143,11 +143,11 @@ func TestMigrate(t *testing.T) {
})

t.Run("case=applies on yes flag", func(t *testing.T) {
out := cmd.ExecNoErr(t, "up", "--"+FlagYes)
out := cmd.ExecNoErr(t, "up", "-y")

t.Cleanup(func() {
// migrate all down
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "--steps", "10000", "-y"))
})

parts := strings.Split(out, "Applying migrations...")
Expand Down Expand Up @@ -185,8 +185,8 @@ func TestUpAndDown(t *testing.T) {
config.KeyNamespaces: []*namespace.Namespace{},
})

t.Log(cmd.ExecNoErr(t, "up", "-c", cf, "--"+FlagYes))
t.Log(cmd.ExecNoErr(t, "down", "0", "-c", cf, "--"+FlagYes))
t.Log(cmd.ExecNoErr(t, "up", "-c", cf, "-y"))
t.Log(cmd.ExecNoErr(t, "down", "--steps", "10000", "-c", cf, "-y"))
})
}
}
84 changes: 72 additions & 12 deletions cmd/migrate/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,86 @@
package migrate

import (
"github.com/ory/x/configx"
"github.com/ory/x/popx"
"github.com/spf13/cobra"

"github.com/ory/keto/internal/driver"

"github.com/ory/keto/ketoctx"
)

func RegisterCommandsRecursive(parent *cobra.Command, opts []ketoctx.Option) {
parent.AddCommand(newMigrateCmd(opts))
}

// migrateSqlCmd represents the sql command
func newMigrateCmd(opts []ketoctx.Option) *cobra.Command {
cmd := &cobra.Command{
c := &cobra.Command{
Use: "migrate",
Short: "Commands to migrate the database",
Long: "Commands to migrate the database.\n" +
"This does not affect namespaces. Use `keto namespace migrate` for migrating namespaces.",
Short: "Create SQL schemas and apply migration plans",
}
cmd.AddCommand(
newStatusCmd(opts),
newUpCmd(opts),
newDownCmd(opts),
)
return cmd

configx.RegisterFlags(c.PersistentFlags())
c.PersistentFlags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.")
c.Flags().BoolP("yes", "y", false, "If set all confirmation requests are accepted without user interaction.")

c.AddCommand(NewMigrateSQLStatusCmd(opts))
c.AddCommand(NewMigrateSQLUpCmd(opts))
c.AddCommand(NewMigrateSQLDownCmd(opts))

c.AddCommand(newMigrateSqlCmd(opts))

return c
}

func RegisterCommandsRecursive(parent *cobra.Command, opts []ketoctx.Option) {
parent.AddCommand(newMigrateCmd(opts))
// migrateSqlCmd represents the sql command
func newMigrateSqlCmd(opts []ketoctx.Option) *cobra.Command {
c := &cobra.Command{
Use: "sql",
Short: "Create SQL schemas and apply migration plans",
}

configx.RegisterFlags(c.PersistentFlags())
c.PersistentFlags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.")
c.Flags().BoolP("yes", "y", false, "If set all confirmation requests are accepted without user interaction.")

c.AddCommand(NewMigrateSQLStatusCmd(opts))
c.AddCommand(NewMigrateSQLUpCmd(opts))
c.AddCommand(NewMigrateSQLDownCmd(opts))

return c
}

func NewMigrateSQLDownCmd(opts []ketoctx.Option) *cobra.Command {
return popx.NewMigrateSQLDownCmd("keto", func(cmd *cobra.Command, args []string) error {
reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags(), true, opts)
if err != nil {
return err
}

return popx.MigrateSQLDown(cmd, reg.Persister())
})
}

func NewMigrateSQLUpCmd(opts []ketoctx.Option) *cobra.Command {
return popx.NewMigrateSQLUpCmd("keto", func(cmd *cobra.Command, args []string) error {
reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags(), true, opts)
if err != nil {
return err
}

return popx.MigrateSQLUp(cmd, reg.Persister())
})
}

func NewMigrateSQLStatusCmd(opts []ketoctx.Option) *cobra.Command {
return popx.NewMigrateSQLStatusCmd("keto", func(cmd *cobra.Command, args []string) error {
reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags(), true, opts)
if err != nil {
return err
}

return popx.MigrateStatus(cmd, reg.Persister())
})
}
74 changes: 0 additions & 74 deletions cmd/migrate/status.go

This file was deleted.

Loading

0 comments on commit 8c286e6

Please sign in to comment.