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

refactor: persistence table structure #638

Merged
merged 30 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6ab8998
refactor: persistence table structure
zepatrik Jun 29, 2021
a989734
Merge branch 'master' into persistence-v2-2
zepatrik Jun 29, 2021
d846b71
fix: build issues
zepatrik Jun 29, 2021
aa41b47
autogen(docs): generate cli docs
aeneasr Jun 29, 2021
6204d9f
fix: make linter happy
zepatrik Jun 29, 2021
72f750d
Merge remote-tracking branch 'origin/persistence-v2-2' into persisten…
zepatrik Jun 29, 2021
5155479
refactor: internal tests
zepatrik Jul 2, 2021
be30b03
Merge branch 'master' into persistence-v2-2
zepatrik Jul 2, 2021
e45b3f2
autogen: build buf
zepatrik Jul 2, 2021
a974ca3
docs: add deprecation info link
zepatrik Jul 2, 2021
614d9b6
test: fix race
zepatrik Jul 3, 2021
a4f4a92
Merge remote-tracking branch 'origin/persistence-v2-2' into persisten…
zepatrik Jul 3, 2021
c9f2c87
refactor: a lot
zepatrik Jul 7, 2021
2552166
Merge branch 'master' into persistence-v2-2
zepatrik Jul 7, 2021
f4efb67
fix: minor things, nothing to see here, don't look into this commit
zepatrik Jul 8, 2021
cf6c67b
chore: format
zepatrik Jul 8, 2021
fcb70d6
autogen(docs): generate cli docs
aeneasr Jul 8, 2021
01907e1
fix: use stderr for error msg
zepatrik Jul 13, 2021
d1dac63
Merge remote-tracking branch 'origin/persistence-v2-2' into persisten…
zepatrik Jul 13, 2021
388c853
Merge branch 'master' into persistence-v2-2
zepatrik Jul 13, 2021
0ea3381
autogen: build buf
zepatrik Jul 13, 2021
3e851bb
fix: review suggestions
zepatrik Jul 27, 2021
e50009c
Merge remote-tracking branch 'origin/persistence-v2-2' into persisten…
zepatrik Jul 27, 2021
d2df2a7
Merge branch 'master' into persistence-v2-2
zepatrik Jul 27, 2021
d42c445
fix: review request
zepatrik Aug 9, 2021
75e7752
Merge branch 'master' into persistence-v2-2
zepatrik Aug 9, 2021
640cc82
chore: go mod tidy
zepatrik Aug 9, 2021
002f8de
chore: lint issues
zepatrik Aug 9, 2021
8773b3e
fix: align namespace ID int size (SQL & Go)
zepatrik Aug 10, 2021
f785fd3
Merge branch 'master' into persistence-v2-2
zepatrik Aug 10, 2021
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
3 changes: 0 additions & 3 deletions .bin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.16

replace github.com/goreleaser/nfpm => github.com/goreleaser/nfpm v1.10.2

replace github.com/ory/kratos-client-go => github.com/ory/kratos-client-go v0.5.4-alpha.1.0.20210210170256-960b093d8bf9

replace github.com/ory/kratos/corp => github.com/ory/kratos/corp v0.0.0-20210118092700-c2358be1e867

replace github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1
Expand All @@ -15,7 +13,6 @@ require (
github.com/go-swagger/go-swagger v0.26.1
github.com/goreleaser/godownloader v0.1.1-0.20200426152203-fd8ad8f7dd78
github.com/mattn/goveralls v0.0.7
github.com/ory/cli v0.0.49
github.com/ory/go-acc v0.2.6
github.com/pseudomuto/protoc-gen-doc v1.4.1
golang.org/x/tools v0.1.0
Expand Down
1,153 changes: 21 additions & 1,132 deletions .bin/go.sum

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion .bin/indirect_pins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
_ "github.com/mattn/goveralls"
_ "golang.org/x/tools/cmd/goimports"

_ "github.com/ory/cli"
_ "github.com/ory/go-acc"

// Protobuf and gRPC related tools
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
.idea/
coverage.txt
dist/
**/*.sqlite
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export PWD := $(shell pwd)
GO_DEPENDENCIES = github.com/go-swagger/go-swagger/cmd/swagger \
golang.org/x/tools/cmd/goimports \
github.com/mattn/goveralls \
github.com/ory/cli \
github.com/ory/go-acc \
github.com/bufbuild/buf/cmd/buf \
google.golang.org/protobuf/cmd/protoc-gen-go \
Expand All @@ -16,12 +15,16 @@ GO_DEPENDENCIES = github.com/go-swagger/go-swagger/cmd/swagger \

define make-go-dependency
# go install is responsible for not re-building when the code hasn't changed
.bin/$(notdir $1): go.mod go.sum Makefile
.bin/$(notdir $1): .bin/go.mod .bin/go.sum Makefile
cd .bin; GOBIN=$(PWD)/.bin/ go install $1
endef
$(foreach dep, $(GO_DEPENDENCIES), $(eval $(call make-go-dependency, $(dep))))
$(call make-lint-dependency)

.bin/ory: Makefile
bash <(curl https://raw.githubusercontent.com/ory/cli/master/install.sh) -b .bin v0.0.57
touch -a -m .bin/ory

node_modules: package.json package-lock.json Makefile
npm ci

Expand Down Expand Up @@ -95,3 +98,11 @@ test-docs-samples:
npm i \
&& \
npm test

.PHONY: migrations-render
migrations-render: .bin/ory
ory dev pop migration render internal/persistence/sql/migrations/templates internal/persistence/sql/migrations/sql

.PHONY: migrations-render-replace
migrations-render-replace: .bin/ory
ory dev pop migration render -r internal/persistence/sql/migrations/templates internal/persistence/sql/migrations/sql
5 changes: 4 additions & 1 deletion cmd/client/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net"
"testing"

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

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -35,8 +37,9 @@ const (

func NewTestServer(t *testing.T, rw ServerType, nspaces []*namespace.Namespace, newCmd func() *cobra.Command) *TestServer {
ts := &TestServer{
Reg: driver.NewMemoryTestRegistry(t, nspaces),
Reg: driver.NewSqliteTestRegistry(t, false),
}
require.NoError(t, ts.Reg.Config().Set(config.KeyNamespaces, nspaces))

switch rw {
case ReadServer:
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func newDownCmd() *cobra.Command {
return fmt.Errorf("malformed argument %s for <steps>: %+v", args[0], err)
}

reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags())
reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags(), true)
if err != nil {
return err
}

mb, err := reg.Migrator().MigrationBox(cmd.Context())
mb, err := reg.MigrationBox()
if err != nil {
return err
}
Expand Down
33 changes: 8 additions & 25 deletions cmd/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package migrate
import (
"bytes"
"context"
"regexp"
"strings"
"testing"

"github.com/ory/x/dbal"

"github.com/ory/keto/internal/x/dbx"

"github.com/ory/x/cmdx"
"github.com/ory/x/configx"
"github.com/sirupsen/logrus/hooks/test"
Expand All @@ -17,7 +20,6 @@ import (
"github.com/ory/keto/internal/driver"
"github.com/ory/keto/internal/driver/config"
"github.com/ory/keto/internal/namespace"
"github.com/ory/keto/internal/x"
)

func assertAllApplied(t *testing.T, status string) {
Expand Down Expand Up @@ -54,14 +56,14 @@ func TestMigrate(t *testing.T) {
}
}

for _, dsn := range x.GetDSNs(t) {
if dsn.Name == "memory" {
for _, dsn := range dbx.GetDSNs(t, false) {
if dbal.IsMemorySQLite(dsn.Conn) {
t.Run("dsn=memory", func(t *testing.T) {
t.Run("case=auto migrates", func(t *testing.T) {
hook := &test.Hook{}
ctx := context.WithValue(context.Background(), driver.LogrusHookContextKey, hook)

cf := x.ConfigFile(t, map[string]interface{}{
cf := dbx.ConfigFile(t, map[string]interface{}{
config.KeyDSN: dsn.Conn,
config.KeyNamespaces: nspaces,
"log.level": "debug",
Expand All @@ -78,7 +80,7 @@ func TestMigrate(t *testing.T) {
hook := &test.Hook{}
ctx := context.WithValue(context.Background(), driver.LogrusHookContextKey, hook)

cf := x.ConfigFile(t, map[string]interface{}{
cf := dbx.ConfigFile(t, map[string]interface{}{
config.KeyDSN: dsn.Conn,
config.KeyNamespaces: nspaces,
"log.level": "debug",
Expand Down Expand Up @@ -125,25 +127,6 @@ func TestMigrate(t *testing.T) {
assertNoneApplied(t, parts[0])
assertAllApplied(t, parts[1])
})

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

t.Cleanup(func() {
// migrate all down
t.Log(cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
})

parts := regexp.MustCompile("(?s)Current status:(.*)Successfully applied all migrations(.*)Going to migrate namespaces(.*)Successfully applied all migrations(.*)Current status(.*)Successfully applied all migrations(.*)").FindStringSubmatch(out)
require.Len(t, parts, 7)

assertNoneApplied(t, parts[1])
assertAllApplied(t, parts[2])
assertNoneApplied(t, parts[3])
assertAllApplied(t, parts[4])
assertNoneApplied(t, parts[5])
assertAllApplied(t, parts[6])
})
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func newStatusCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags(), true)
if err != nil {
return err
}

mb, err := reg.Migrator().MigrationBox(ctx)
mb, err := reg.MigrationBox()
if err != nil {
return err
}
Expand Down
35 changes: 2 additions & 33 deletions cmd/migrate/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/spf13/pflag"

"github.com/pkg/errors"

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

Expand All @@ -34,12 +32,12 @@ func newUpCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags(), true)
if err != nil {
return err
}

mb, err := reg.Migrator().MigrationBox(ctx)
mb, err := reg.MigrationBox()
if err != nil {
return err
}
Expand All @@ -48,35 +46,6 @@ func newUpCmd() *cobra.Command {
return err
}

if !allNamespaces {
// everything is done already
return nil
}

_, _ = fmt.Fprintln(cmd.OutOrStdout(), "\nGoing to migrate namespaces.")

nm, err := reg.Config().NamespaceManager()
if err != nil {
return errors.Wrap(err, "could not get the namespace manager")
}

nspaces, err := nm.Namespaces(cmd.Context())
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get namespaces: %+v\n", err)
return cmdx.FailSilently(cmd)
}

for _, nspace := range nspaces {
mb, err := reg.NamespaceMigrator().NamespaceMigrationBox(ctx, nspace)
if err != nil {
return err
}

if err := BoxUp(cmd, mb, "[namespace="+nspace.Name+"] "); err != nil {
return err
}
}

return nil
},
}
Expand Down
45 changes: 5 additions & 40 deletions cmd/namespace/migrate_down.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,21 @@
package namespace

import (
"fmt"
"strconv"

"github.com/ory/keto/cmd/migrate"

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

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

func NewMigrateDownCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "down <namespace-name> <steps>",
Short: "Migrate a namespace down",
Deprecated: "This step is not necessary anymore, see https://github.com/ory/keto/pull/638",
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
Use: "down <namespace-name> <steps>",
Short: "Migrate a namespace down",
Long: "Migrate a namespace down.\n" +
"Pass 0 steps to fully migrate down.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

steps, err := strconv.ParseInt(args[1], 0, 0)
if err != nil {
// return this error so it gets printed along the usage
return fmt.Errorf("malformed argument %s for <steps>: %+v", args[0], err)
}

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
if err != nil {
return err
}

nm, err := reg.Config().NamespaceManager()
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not initialize the namespace manager: %+v\n", err)
return cmdx.FailSilently(cmd)
}

n, err := nm.GetNamespace(ctx, args[0])
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not find the namespace with name \"%s\": %+v\n", args[0], err)
return cmdx.FailSilently(cmd)
}

mb, err := reg.NamespaceMigrator().NamespaceMigrationBox(ctx, n)
if err != nil {
return err
}

return migrate.BoxDown(cmd, mb, int(steps), "[namespace="+n.Name+"] ")
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
}

Expand Down
40 changes: 7 additions & 33 deletions cmd/namespace/migrate_status.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
package namespace

import (
"fmt"

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

"github.com/ory/keto/cmd/migrate"
"github.com/ory/keto/internal/driver"
)

func NewMigrateStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status <namespace-name>",
Short: "Get the current namespace migration status",
Long: "Get the current migration status of one specific namespace.\nDoes not apply any changes.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags())
if err != nil {
return err
}

nm, err := reg.Config().NamespaceManager()
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not initialize the namespace manager: %+v\n", err)
return cmdx.FailSilently(cmd)
}

n, err := nm.GetNamespace(cmd.Context(), args[0])
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not find the namespace with name \"%s\": %+v\n", args[0], err)
return cmdx.FailSilently(cmd)
}

mb, err := reg.NamespaceMigrator().NamespaceMigrationBox(cmd.Context(), n)
if err != nil {
return err
}

return migrate.BoxStatus(cmd, mb, "[namespace="+n.Name+"] ")
Deprecated: "This step is not necessary anymore, see https://github.com/ory/keto/pull/638",
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
Use: "status <namespace-name>",
Short: "Get the current namespace migration status",
Long: "Get the current migration status of one specific namespace.\nDoes not apply any changes.",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
}

Expand Down
Loading