Skip to content

Commit

Permalink
Merge branch 'develop' into nasdf/feat/gql-mutation-input-arg
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf authored Jan 11, 2024
2 parents 245ceb5 + 858e6a6 commit 4bd619f
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 55 deletions.
53 changes: 42 additions & 11 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,7 @@ func (db *db) setDefaultSchemaVersion(
}
}

cols, err := db.getAllCollections(ctx, txn)
if err != nil {
return err
}

definitions := make([]client.CollectionDefinition, len(cols))
for i, col := range cols {
definitions[i] = col.Definition()
}

return db.parser.SetSchema(ctx, txn, definitions)
return db.loadSchema(ctx, txn)
}

func (db *db) setDefaultSchemaVersionExplicit(
Expand Down Expand Up @@ -633,6 +623,47 @@ func (db *db) getAllCollections(ctx context.Context, txn datastore.Txn) ([]clien
return collections, nil
}

// getAllActiveDefinitions returns all queryable collection/views and any embedded schema used by them.
func (db *db) getAllActiveDefinitions(ctx context.Context, txn datastore.Txn) ([]client.CollectionDefinition, error) {
cols, err := description.GetCollections(ctx, txn)
if err != nil {
return nil, err
}

definitions := make([]client.CollectionDefinition, len(cols))
for i, col := range cols {
schema, err := description.GetSchemaVersion(ctx, txn, col.SchemaVersionID)
if err != nil {
return nil, err
}

collection := db.newCollection(col, schema)

err = collection.loadIndexes(ctx, txn)
if err != nil {
return nil, err
}

definitions[i] = collection.Definition()
}

schemas, err := description.GetCollectionlessSchemas(ctx, txn)
if err != nil {
return nil, err
}

for _, schema := range schemas {
definitions = append(
definitions,
client.CollectionDefinition{
Schema: schema,
},
)
}

return definitions, nil
}

// GetAllDocIDs returns all the document IDs that exist in the collection.
//
// @todo: We probably need a lock on the collection for this kind of op since
Expand Down
42 changes: 42 additions & 0 deletions db/description/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,45 @@ func GetSchemaVersionIDs(

return schemaVersions, nil
}

// GetCollectionlessSchemas returns all schema that are not attached to a collection.
//
// Typically this means any schema embedded in a View.
//
// WARNING: This function does not currently account for multiple versions of collectionless schema,
// at the moment such a situation is impossible, but that is likely to change, at which point this
// function will need to account for that.
func GetCollectionlessSchemas(
ctx context.Context,
txn datastore.Txn,
) ([]client.SchemaDescription, error) {
cols, err := GetCollections(ctx, txn)
if err != nil {
return nil, err
}

allSchemas, err := GetAllSchemas(ctx, txn)
if err != nil {
return nil, err
}

schemaRootsByVersionID := map[string]string{}
for _, schema := range allSchemas {
schemaRootsByVersionID[schema.VersionID] = schema.Root
}

colSchemaRoots := map[string]struct{}{}
for _, col := range cols {
schemaRoot := schemaRootsByVersionID[col.SchemaVersionID]
colSchemaRoots[schemaRoot] = struct{}{}
}

collectionlessSchema := []client.SchemaDescription{}
for _, schema := range allSchemas {
if _, hasCollection := colSchemaRoots[schema.Root]; !hasCollection {
collectionlessSchema = append(collectionlessSchema, schema)
}
}

return collectionlessSchema, nil
}
29 changes: 7 additions & 22 deletions db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ func (db *db) addSchema(
return nil, err
}

err = db.parser.SetSchema(ctx, txn, append(existingDefinitions, newDefinitions...))
if err != nil {
return nil, err
}

returnDescriptions := make([]client.CollectionDescription, len(newDefinitions))
for i, definition := range newDefinitions {
col, err := db.createCollection(ctx, txn, definition)
Expand All @@ -68,20 +63,20 @@ func (db *db) addSchema(
returnDescriptions[i] = col.Description()
}

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

return returnDescriptions, nil
}

func (db *db) loadSchema(ctx context.Context, txn datastore.Txn) error {
collections, err := db.getAllCollections(ctx, txn)
definitions, err := db.getAllActiveDefinitions(ctx, txn)
if err != nil {
return err
}

definitions := make([]client.CollectionDefinition, len(collections))
for i := range collections {
definitions[i] = collections[i].Definition()
}

return db.parser.SetSchema(ctx, txn, definitions)
}

Expand Down Expand Up @@ -150,17 +145,7 @@ func (db *db) patchSchema(ctx context.Context, txn datastore.Txn, patchString st
}
}

newCollections, err := db.getAllCollections(ctx, txn)
if err != nil {
return err
}

definitions := make([]client.CollectionDefinition, len(newCollections))
for i, col := range newCollections {
definitions[i] = col.Definition()
}

return db.parser.SetSchema(ctx, txn, definitions)
return db.loadSchema(ctx, txn)
}

// substituteSchemaPatch handles any substitution of values that may be required before
Expand Down
20 changes: 5 additions & 15 deletions db/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,6 @@ func (db *db) addView(
newDefinitions[i].Description.BaseQuery = baseQuery
}

existingCollections, err := db.getAllCollections(ctx, txn)
if err != nil {
return nil, err
}

existingDefinitions := make([]client.CollectionDefinition, len(existingCollections))
for i := range existingCollections {
existingDefinitions[i] = existingCollections[i].Definition()
}

err = db.parser.SetSchema(ctx, txn, append(existingDefinitions, newDefinitions...))
if err != nil {
return nil, err
}

returnDescriptions := make([]client.CollectionDefinition, len(newDefinitions))
for i, definition := range newDefinitions {
if definition.Description.Name == "" {
Expand All @@ -95,5 +80,10 @@ func (db *db) addView(
}
}

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

return returnDescriptions, nil
}
4 changes: 2 additions & 2 deletions tests/integration/schema/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestSchemaSimpleErrorsGivenDuplicateSchema(t *testing.T) {
Schema: `
type Users {}
`,
ExpectedError: "schema type already exists",
ExpectedError: "collection already exists",
},
},
}
Expand All @@ -94,7 +94,7 @@ func TestSchemaSimpleErrorsGivenDuplicateSchemaInSameSDL(t *testing.T) {
type Users {}
type Users {}
`,
ExpectedError: "schema type already exists",
ExpectedError: "collection already exists",
},
},
}
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func executeTestCase(
logging.NewKV("changeDetector.Repository", changeDetector.Repository),
)

startActionIndex, endActionIndex := getActionRange(testCase)
startActionIndex, endActionIndex := getActionRange(t, testCase)

s := newState(ctx, t, testCase, dbt, clientType, collectionNames)
setStartingNodes(s)
Expand Down Expand Up @@ -554,7 +554,7 @@ func flattenActions(testCase *TestCase) {
//
// If a SetupComplete action is provided, the actions will be split there, if not
// they will be split at the first non SchemaUpdate/CreateDoc/UpdateDoc action.
func getActionRange(testCase TestCase) (int, int) {
func getActionRange(t *testing.T, testCase TestCase) (int, int) {
startIndex := 0
endIndex := len(testCase.Actions) - 1

Expand Down Expand Up @@ -597,8 +597,10 @@ ActionLoop:
// We must not set this to -1 :)
startIndex = firstNonSetupIndex
} else {
// if we don't have any non-mutation actions, just use the last action
startIndex = endIndex
// if we don't have any non-mutation actions and the change detector is enabled
// skip this test as we will not gain anything from running (change detector would
// run an idential profile to a normal test run)
t.Skipf("no actions to execute")
}
}

Expand Down
54 changes: 53 additions & 1 deletion tests/integration/view/one_to_one/identical_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package one_to_many
package one_to_one

import (
"testing"
Expand Down Expand Up @@ -92,3 +92,55 @@ func TestView_OneToOneSameSchema(t *testing.T) {

testUtils.ExecuteTestCase(t, test)
}

func TestView_OneToOneEmbeddedSchemaIsNotLostOnNextUpdate(t *testing.T) {
test := testUtils.TestCase{
Description: "One to one view followed by GQL type update",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Author {
name: String
books: [Book]
}
type Book {
name: String
author: Author
}
`,
},
testUtils.CreateView{
Query: `
Author {
name
books {
name
}
}
`,
SDL: `
type AuthorView {
name: String
books: [BookView]
}
interface BookView {
name: String
}
`,
},
// After creating the view, update the system's types again and ensure
// that `BookView` is not forgotten. A GQL error would appear if this
// was broken as `AuthorView.books` would reference a type that does
// not exist.
testUtils.SchemaUpdate{
Schema: `
type User {
name: String
}
`,
},
},
}

testUtils.ExecuteTestCase(t, test)
}
Loading

0 comments on commit 4bd619f

Please sign in to comment.