diff --git a/docs/docs/cli/keto-migrate-up.md b/docs/docs/cli/keto-migrate-up.md index 8c253c2a0..8706ec394 100644 --- a/docs/docs/cli/keto-migrate-up.md +++ b/docs/docs/cli/keto-migrate-up.md @@ -17,7 +17,7 @@ Migrate the database up ### Synopsis Run this command on a fresh SQL installation and when you upgrade Ory Keto from -version v0.7.0-alpha.0.pre.0 and later. +version v0.7.0 and later. It is recommended to run this command close to the SQL instance (e.g. same subnet) instead of over the public internet. This decreases risk of failure and diff --git a/docs/versions.json b/docs/versions.json index d11f43066..1782764ca 100644 --- a/docs/versions.json +++ b/docs/versions.json @@ -1,5 +1 @@ -[ - "v0.7", - "v0.6", - "v0.5" -] +["v0.7", "v0.6", "v0.5"] diff --git a/go.mod b/go.mod index 78c81fa30..a74157c95 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/ory/herodot v0.9.6 github.com/ory/jsonschema/v3 v3.0.3 github.com/ory/keto/proto v0.0.0-00010101000000-000000000000 - github.com/ory/x v0.0.287 + github.com/ory/x v0.0.289 github.com/pelletier/go-toml v1.8.1 github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 8ab687cfb..0c9d94f6d 100644 --- a/go.sum +++ b/go.sum @@ -192,7 +192,6 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= -github.com/go-bindata/go-bindata v3.1.1+incompatible h1:tR4f0e4VTO7LK6B2YWyAoVEzG9ByG1wrXB4TL9+jiYg= github.com/go-bindata/go-bindata v3.1.1+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -428,7 +427,6 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= -github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g= github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -867,8 +865,8 @@ github.com/ory/x v0.0.93/go.mod h1:lfcTaGXpTZs7IEQAW00r9EtTCOxD//SiP5uWtNiz31g= github.com/ory/x v0.0.110/go.mod h1:DJfkE3GdakhshNhw4zlKoRaL/ozg/lcTahA9OCih2BE= github.com/ory/x v0.0.127/go.mod h1:FwUujfFuCj5d+xgLn4fGMYPnzriR5bdAIulFXMtnK0M= github.com/ory/x v0.0.205/go.mod h1:A1s4iwmFIppRXZLF3J9GGWeY/HpREVm0Dk5z/787iek= -github.com/ory/x v0.0.287 h1:ubXxLsEOCaIX37KJnr/VgyfHQOQleNO9fmlqhcXK7hg= -github.com/ory/x v0.0.287/go.mod h1:APpShLyJcVzKw1kTgrHI+j/L9YM+8BRjHlcYObc7C1U= +github.com/ory/x v0.0.289 h1:JuPILxVQS0NnHcbmt6HbFGYlrQwPHidqFDRXWfduQH4= +github.com/ory/x v0.0.289/go.mod h1:APpShLyJcVzKw1kTgrHI+j/L9YM+8BRjHlcYObc7C1U= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/parnurzeal/gorequest v0.2.15/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= diff --git a/internal/persistence/sql/migrations/migratest/migration_test.go b/internal/persistence/sql/migrations/migratest/migration_test.go new file mode 100644 index 000000000..ea0d2a47f --- /dev/null +++ b/internal/persistence/sql/migrations/migratest/migration_test.go @@ -0,0 +1,110 @@ +package migratest + +import ( + "context" + "os" + "testing" + "time" + + "github.com/gobuffalo/pop/v5" + + "github.com/ory/keto/internal/driver/config" + "github.com/ory/keto/internal/namespace" + + "github.com/gofrs/uuid" + "github.com/ory/x/fsx" + "github.com/ory/x/logrusx" + "github.com/ory/x/networkx" + "github.com/ory/x/popx" + "github.com/ory/x/sqlcon" + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/ory/keto/internal/driver" + "github.com/ory/keto/internal/persistence/sql" + "github.com/ory/keto/internal/relationtuple" + "github.com/ory/keto/internal/x/dbx" +) + +func TestMigrations(t *testing.T) { + const debugOnDisk = false + + for _, db := range dbx.GetDSNs(t, debugOnDisk) { + t.Run("dsn="+db.Name, func(t *testing.T) { + db.MigrateUp, db.MigrateDown = false, false + + ctx := context.Background() + l := logrusx.New("", "", logrusx.ForceLevel(logrus.DebugLevel)) + + var c *pop.Connection + var err error + for i := 0; i < 120; i++ { + c, err = pop.NewConnection(&pop.ConnectionDetails{URL: db.Conn}) + require.NoError(t, err) + require.NoError(t, c.Open()) + if err := c.Store.(interface{ Ping() error }).Ping(); err == nil { + break + } + time.Sleep(time.Second) + } + require.NoError(t, c.Store.(interface{ Ping() error }).Ping()) + + tm := popx.NewTestMigrator(t, c, fsx.Merge(networkx.Migrations, os.DirFS("../sql")), os.DirFS("./testdata"), l) + // cleanup first + require.NoError(t, tm.Down(ctx, -1)) + + t.Run("suite=up", func(t *testing.T) { + require.NoError(t, tm.Up(ctx)) + }) + + t.Run("suite=fixtures", func(t *testing.T) { + t.Run("table=legacy namespaces", func(t *testing.T) { + // as they are legacy, we expect them to be actually dropped + + assert.ErrorIs(t, sqlcon.HandleError(c.RawQuery("SELECT * FROM keto_namespace").Exec()), sqlcon.ErrNoSuchTable) + }) + + t.Run("table=relation tuples", func(t *testing.T) { + reg := driver.NewTestRegistry(t, db) + require.NoError(t, + reg.Config().Set(config.KeyNamespaces, []*namespace.Namespace{{ID: 1, Name: "foo"}})) + + p, err := sql.NewPersister(reg, uuid.Must(uuid.FromString("77fdc5e0-2260-49da-8aae-c36ba255d05b"))) + require.NoError(t, err) + rts, next, err := p.GetRelationTuples(context.Background(), &relationtuple.RelationQuery{Namespace: "foo"}) + require.NoError(t, err) + assert.Equal(t, "", next) + + for _, rt := range []*relationtuple.InternalRelationTuple{ + { + Namespace: "foo", + Object: "object", + Relation: "relation", + Subject: &relationtuple.SubjectID{ID: "user"}, + }, + { + Namespace: "foo", + Object: "object", + Relation: "relation", + Subject: &relationtuple.SubjectSet{ + Namespace: "foo", + Object: "s_object", + Relation: "s_relation", + }, + }, + } { + assert.Contains(t, rts, rt) + } + }) + }) + + t.Run("suite=down", func(t *testing.T) { + if debugOnDisk && db.Name == "sqlite" { + t.SkipNow() + } + require.NoError(t, tm.Down(ctx, -1)) + }) + }) + } +} diff --git a/internal/persistence/sql/migrations/migratest/testdata/20201110175414_testdata.sql b/internal/persistence/sql/migrations/migratest/testdata/20201110175414_testdata.sql new file mode 100644 index 000000000..2ad3896bf --- /dev/null +++ b/internal/persistence/sql/migrations/migratest/testdata/20201110175414_testdata.sql @@ -0,0 +1 @@ +INSERT INTO keto_namespace (id, schema_version) VALUES (1, 1); diff --git a/internal/persistence/sql/migrations/migratest/testdata/20210623162417_testdata.sql b/internal/persistence/sql/migrations/migratest/testdata/20210623162417_testdata.sql new file mode 100644 index 000000000..c948b264d --- /dev/null +++ b/internal/persistence/sql/migrations/migratest/testdata/20210623162417_testdata.sql @@ -0,0 +1,9 @@ +INSERT INTO keto_relation_tuples (shard_id, nid, namespace_id, object, relation, subject_id, subject_set_namespace_id, + subject_set_object, subject_set_relation, commit_time) +VALUES ('7a9d6df4-3b60-4cae-996e-d15d78b0fc36', '77fdc5e0-2260-49da-8aae-c36ba255d05b', 1, 'object', 'relation', 'user', + NULL, NULL, NULL, '2021-09-30 15:50:52'); + +INSERT INTO keto_relation_tuples (shard_id, nid, namespace_id, object, relation, subject_id, subject_set_namespace_id, + subject_set_object, subject_set_relation, commit_time) +VALUES ('1457d171-4473-4c77-bd88-0809529c599f', '77fdc5e0-2260-49da-8aae-c36ba255d05b', 1, 'object', 'relation', NULL, + 1, 's_object', 's_relation', '2021-09-30 15:50:52'); diff --git a/internal/persistence/sql/migrations/migratest/testdata/20210914134624_testdata.sql b/internal/persistence/sql/migrations/migratest/testdata/20210914134624_testdata.sql new file mode 100644 index 000000000..49c3a0cde --- /dev/null +++ b/internal/persistence/sql/migrations/migratest/testdata/20210914134624_testdata.sql @@ -0,0 +1 @@ +-- DROP TABLE statement only, nothing to insert