Skip to content

Commit

Permalink
WIP - Add SetDefaultSchemaVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Sep 15, 2023
1 parent b21e0a9 commit d9c2e55
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 1 deletion.
2 changes: 2 additions & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type Store interface {
// [FieldKindStringToEnumMapping].
PatchSchema(context.Context, string) error

SetDefaultSchemaVersion(context.Context, string) error

// SetMigration sets the migration for the given source-destination schema version IDs. Is equivilent to
// calling `LensRegistry().SetMigration(ctx, cfg)`.
//
Expand Down
43 changes: 43 additions & 0 deletions client/mocks/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (db *db) updateCollection(
return nil, err
}

err = db.setDefaultSchemaVersion(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
err = db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -585,6 +585,20 @@ func validateUpdateCollectionIndexes(
}

func (db *db) setDefaultSchemaVersion(
ctx context.Context,
txn datastore.Txn,
schemaVersionID string,
) error {
col, err := db.getCollectionByVersionID(ctx, txn, schemaVersionID)
if err != nil {
return err
}

desc := col.Description()
return db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
}

func (db *db) setDefaultSchemaVersionExplicit(
ctx context.Context,
txn datastore.Txn,
collectionName string,
Expand Down
19 changes: 19 additions & 0 deletions db/txn_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ func (db *explicitTxnDB) PatchSchema(ctx context.Context, patchString string) er
return db.patchSchema(ctx, db.txn, patchString)
}

func (db *implicitTxnDB) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
txn, err := db.NewTxn(ctx, false)
if err != nil {
return err
}
defer txn.Discard(ctx)

err = db.setDefaultSchemaVersion(ctx, txn, schemaVersionID)
if err != nil {
return err
}

return txn.Commit(ctx)
}

func (db *explicitTxnDB) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
return db.setDefaultSchemaVersion(ctx, db.txn, schemaVersionID)
}

func (db *implicitTxnDB) SetMigration(ctx context.Context, cfg client.LensConfig) error {
txn, err := db.NewTxn(ctx, false)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ func (c *Client) PatchSchema(ctx context.Context, patch string) error {
return err
}

func (c *Client) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
methodURL := c.http.baseURL.JoinPath("schema")

Check failure on line 227 in http/client.go

View workflow job for this annotation

GitHub Actions / Lint job

SA4006: this value of `methodURL` is never used (staticcheck)
panic("todo")

req, err := http.NewRequestWithContext(ctx, http.MethodPatch, methodURL.String(), strings.NewReader(schemaVersionID))

Check failure on line 230 in http/client.go

View workflow job for this annotation

GitHub Actions / Lint job

unreachable: unreachable code (govet)
if err != nil {
return err
}
_, err = c.http.request(req)
return err
}

func (c *Client) SetMigration(ctx context.Context, config client.LensConfig) error {
return c.LensRegistry().SetMigration(ctx, config)
}
Expand Down
4 changes: 4 additions & 0 deletions http/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (w *Wrapper) PatchSchema(ctx context.Context, patch string) error {
return w.client.PatchSchema(ctx, patch)
}

func (w *Wrapper) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error {
return w.client.SetDefaultSchemaVersion(ctx, schemaVersionID)
}

func (w *Wrapper) SetMigration(ctx context.Context, config client.LensConfig) error {
return w.client.SetMigration(ctx, config)
}
Expand Down
106 changes: 106 additions & 0 deletions tests/integration/schema/with_update_set_default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2023 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package schema

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestSchema_WithUpdateAndSetDefaultVersionToEmptyString_Errors(t *testing.T) {
test := testUtils.TestCase{
Description: "Test schema update, set default version to empty string",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.SchemaPatch{
Patch: `
[
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {"Name": "email", "Kind": 11} }
]
`,
},
testUtils.SetDefaultSchemaVersion{
SchemaVersionID: "",
ExpectedError: "dfhsgaf",
},
},
}
testUtils.ExecuteTestCase(t, test)
}

func TestSchema_WithUpdateAndSetDefaultVersionToUnknownVersion_Errors(t *testing.T) {
test := testUtils.TestCase{
Description: "Test schema update, set default version to invalid string",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.SchemaPatch{
Patch: `
[
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {"Name": "email", "Kind": 11} }
]
`,
},
testUtils.SetDefaultSchemaVersion{
SchemaVersionID: "does not exist",
ExpectedError: "dfhsgaf",
},
},
}
testUtils.ExecuteTestCase(t, test)
}

func TestSchema_WithUpdateAndSetDefaultVersionToOriginal_NewFieldIsNotQueriable(t *testing.T) {
test := testUtils.TestCase{
Description: "Test schema update, add field",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.SchemaPatch{
Patch: `
[
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {"Name": "email", "Kind": 11} }
]
`,
},
testUtils.SetDefaultSchemaVersion{
SchemaVersionID: "bafkreihn4qameldz3j7rfundmd4ldhxnaircuulk6h2vcwnpcgxl4oqffq",
},
testUtils.Request{
Request: `query {
Users {
name
email
}
}`,
ExpectedError: "dfhsgaf",
},
},
}
testUtils.ExecuteTestCase(t, test)
}
10 changes: 10 additions & 0 deletions tests/integration/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ type SchemaPatch struct {
ExpectedError string
}

type SetDefaultSchemaVersion struct {
// NodeID may hold the ID (index) of a node to set the default schema version on.
//
// If a value is not provided the default will be set on all nodes.
NodeID immutable.Option[int]

SchemaVersionID string
ExpectedError string
}

// CreateDoc will attempt to create the given document in the given collection
// using the set [MutationType].
type CreateDoc struct {
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ func executeTestCase(
case SchemaPatch:
patchSchema(s, action)

case SetDefaultSchemaVersion:
setDefaultSchemaVersion(s, action)

case ConfigureMigration:
configureMigration(s, action)

Expand Down Expand Up @@ -1070,6 +1073,21 @@ func patchSchema(
refreshIndexes(s)
}

func setDefaultSchemaVersion(
s *state,
action SetDefaultSchemaVersion,
) {
for _, node := range getNodes(action.NodeID, s.nodes) {
err := node.DB.SetDefaultSchemaVersion(s.ctx, action.SchemaVersionID)
expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)

assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)
}

refreshCollections(s)
refreshIndexes(s)
}

// createDoc creates a document using the chosen [mutationType] and caches it in the
// test state object.
func createDoc(
Expand Down

0 comments on commit d9c2e55

Please sign in to comment.