-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
[ | ||
"v0.7", | ||
"v0.6", | ||
"v0.5" | ||
] | ||
["v0.7", "v0.6", "v0.5"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
internal/persistence/sql/migrations/migratest/migration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
internal/persistence/sql/migrations/migratest/testdata/20201110175414_testdata.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO keto_namespace (id, schema_version) VALUES (1, 1); |
9 changes: 9 additions & 0 deletions
9
internal/persistence/sql/migrations/migratest/testdata/20210623162417_testdata.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
1 change: 1 addition & 0 deletions
1
internal/persistence/sql/migrations/migratest/testdata/20210914134624_testdata.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- DROP TABLE statement only, nothing to insert |