Skip to content

Commit

Permalink
refactor: persistence table structure
Browse files Browse the repository at this point in the history
Closes #613
  • Loading branch information
zepatrik committed Jun 29, 2021
1 parent 5bda957 commit 6ab8998
Show file tree
Hide file tree
Showing 58 changed files with 1,049 additions and 575 deletions.
15 changes: 13 additions & 2 deletions .bin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ 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-client-go => github.com/ory/kratos-client-go v0.6.3-alpha.1

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

replace github.com/ory/cli => github.com/ory/cli v0.0.57-0.20210629114108-ae1184abec67

replace github.com/gobuffalo/pop/v5 => github.com/gobuffalo/pop/v5 v5.3.4-0.20210608105745-bb07a373cc0e

replace github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.14.7-0.20210414154423-1157a4212dcb

replace github.com/ory/kratos => github.com/ory/kratos v0.6.3-alpha.1.0.20210608145203-b5c1658e01ca

require (
github.com/Azure/go-autorest/autorest/azure/auth v0.5.7 // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/bufbuild/buf v0.31.1
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/cli v0.0.54
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
625 changes: 497 additions & 128 deletions .bin/go.sum

Large diffs are not rendered by default.

14 changes: 12 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,15 @@ 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: .bin/go.mod .bin/go.sum Makefile
cd .bin; GOBIN=$(PWD)/.bin/ go install -tags sqlite github.com/ory/cli; mv cli ory

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

Expand Down Expand Up @@ -95,3 +97,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
37 changes: 18 additions & 19 deletions cmd/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package migrate
import (
"bytes"
"context"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -126,24 +125,24 @@ func TestMigrate(t *testing.T) {
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])
})
//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
31 changes: 0 additions & 31 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 Down Expand Up @@ -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 TODO",
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 TODO",
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
42 changes: 7 additions & 35 deletions cmd/namespace/migrate_up.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
package namespace

import (
"fmt"

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

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

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

func NewMigrateUpCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "up <namespace-name>",
Short: "Migrate a namespace up",
Long: "Migrate a namespace up to the most recent migration.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

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.BoxUp(cmd, mb, "[namespace="+n.Name+"] ")
Deprecated: "This step is not necessary anymore, see TODO",
Use: "up <namespace-name>",
Short: "Migrate a namespace up",
Long: "Migrate a namespace up to the most recent migration.",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ replace github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1

replace github.com/ory/keto/proto => ./proto

replace github.com/gobuffalo/pop/v5 => ../pop

require (
github.com/cenkalti/backoff/v3 v3.0.0
github.com/ghodss/yaml v1.0.0
Expand All @@ -19,6 +21,7 @@ require (
github.com/go-openapi/swag v0.19.14
github.com/go-openapi/validate v0.20.2
github.com/gobuffalo/pop/v5 v5.3.4
github.com/gofrs/uuid v3.2.0+incompatible
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/julienschmidt/httprouter v1.3.0
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,6 @@ github.com/gobuffalo/pop v4.8.3+incompatible/go.mod h1:DwBz3SD5SsHpTZiTubcsFWcVD
github.com/gobuffalo/pop v4.8.4+incompatible/go.mod h1:DwBz3SD5SsHpTZiTubcsFWcVDpJWGsxjVjMPnkiThWg=
github.com/gobuffalo/pop v4.13.1+incompatible h1:AhbqPxNOBN/DBb2DBaiBqzOXIBQXxEYzngHHJ+ytP4g=
github.com/gobuffalo/pop v4.13.1+incompatible/go.mod h1:DwBz3SD5SsHpTZiTubcsFWcVDpJWGsxjVjMPnkiThWg=
github.com/gobuffalo/pop/v5 v5.0.11/go.mod h1:mZJHJbA3cy2V18abXYuVop2ldEJ8UZ2DK6qOekC5u5g=
github.com/gobuffalo/pop/v5 v5.2.0/go.mod h1:Hj586Cr7FoTFNmvzyNdUcajv3r0A+W+bkil4RIX/zKo=
github.com/gobuffalo/pop/v5 v5.3.1/go.mod h1:vcEDhh6cJ3WVENqJDFt/6z7zNb7lLnlN8vj3n5G9rYA=
github.com/gobuffalo/pop/v5 v5.3.3/go.mod h1:Ey1hqzDLkWQKNEfsnafaz+3P1h/TrS++W9PmpGsNxvk=
github.com/gobuffalo/pop/v5 v5.3.4 h1:VWH74icA8khG8SyWDAq6Ch00jcPiC4n436JMFh0lLm0=
github.com/gobuffalo/pop/v5 v5.3.4/go.mod h1:UiVurv2aTKC7MuR27PnMrQjAazoLr8SoC/LuTKTS/tQ=
github.com/gobuffalo/release v1.0.35/go.mod h1:VtHFAKs61vO3wboCec5xr9JPTjYyWYcvaM3lclkc4x4=
github.com/gobuffalo/release v1.0.38/go.mod h1:VtHFAKs61vO3wboCec5xr9JPTjYyWYcvaM3lclkc4x4=
github.com/gobuffalo/release v1.0.42/go.mod h1:RPs7EtafH4oylgetOJpGP0yCZZUiO4vqHfTHJjSdpug=
Expand Down Expand Up @@ -1472,7 +1466,6 @@ golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200219183655-46282727080f/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
Expand Down
7 changes: 0 additions & 7 deletions internal/check/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ import (

func newDepsProvider(t *testing.T, namespaces []*namespace.Namespace, pageOpts ...x.PaginationOptionSetter) *relationtuple.ManagerWrapper {
reg := driver.NewMemoryTestRegistry(t, namespaces)
t.Cleanup(func() {
for _, n := range namespaces {
mb, err := reg.NamespaceMigrator().NamespaceMigrationBox(context.Background(), n)
require.NoError(t, err)
require.NoError(t, mb.Down(context.Background(), 0))
}
})
return relationtuple.NewManagerWrapper(t, reg, pageOpts...)
}

Expand Down
Loading

0 comments on commit 6ab8998

Please sign in to comment.