Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add means to fetch schema #2006

Merged
merged 7 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewDefraCommand(cfg *config.Config) *cobra.Command {
MakeSchemaAddCommand(),
MakeSchemaPatchCommand(),
MakeSchemaSetDefaultCommand(),
MakeSchemaDescribeCommand(),
schema_migrate,
)

Expand Down
82 changes: 82 additions & 0 deletions cli/schema_describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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 cli

import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
)

func MakeSchemaDescribeCommand() *cobra.Command {
var name string
var root string
var versionID string

var cmd = &cobra.Command{
Use: "describe",
Short: "View schema descriptions.",
Long: `Introspect schema types.

Example: view all schemas
defradb client schema describe

Example: view schemas by name
defradb client schema describe --name User

Example: view schemas by root
defradb client schema describe --root bae123

Example: view a single schema by version id
defradb client schema describe --version bae123
`,
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetStoreContext(cmd)

var schemas []client.SchemaDescription
switch {
case versionID != "":
schema, err := store.GetSchemaByVersionID(cmd.Context(), versionID)
if err != nil {
return err
}
return writeJSON(cmd, schema)

case root != "":
s, err := store.GetSchemasByRoot(cmd.Context(), root)
if err != nil {
return err
}

Check warning on line 57 in cli/schema_describe.go

View check run for this annotation

Codecov / codecov/patch

cli/schema_describe.go#L56-L57

Added lines #L56 - L57 were not covered by tests
schemas = s

case name != "":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: It would be good to add a check after the switch to check if name and versionID were given that they are actually related like you did with collections.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was in two minds if this should behave like the collection stuff, this is different though, as they are not going to be acting on the collection (saving docs and the like). I'll revisit today and see what happens.

- [ ] Revisit multi param schema describe stuff

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if not acting on it, it would be weird to return a set of results event if the provided params make no sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to close this thread as it should match whatever we decide in #2006 (comment)

s, err := store.GetSchemasByName(cmd.Context(), name)
if err != nil {
return err
}

Check warning on line 64 in cli/schema_describe.go

View check run for this annotation

Codecov / codecov/patch

cli/schema_describe.go#L63-L64

Added lines #L63 - L64 were not covered by tests
schemas = s

default:
s, err := store.GetAllSchemas(cmd.Context())
if err != nil {
return err
}

Check warning on line 71 in cli/schema_describe.go

View check run for this annotation

Codecov / codecov/patch

cli/schema_describe.go#L70-L71

Added lines #L70 - L71 were not covered by tests
schemas = s
}

return writeJSON(cmd, schemas)
},
}
cmd.PersistentFlags().StringVar(&name, "name", "", "Schema name")
cmd.PersistentFlags().StringVar(&root, "root", "", "Schema root")
cmd.PersistentFlags().StringVar(&versionID, "version", "", "Schema Version ID")
return cmd
}
16 changes: 16 additions & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ type Store interface {
// this [Store].
GetAllCollections(context.Context) ([]Collection, error)

// GetSchemasByName returns the all schema versions with the given name.
GetSchemasByName(context.Context, string) ([]SchemaDescription, error)

// GetSchemaByVersionID returns the schema description for the schema version of the
// ID provided.
//
// Will return an error if it is not found.
GetSchemaByVersionID(context.Context, string) (SchemaDescription, error)

// GetSchemasByRoot returns the all schema versions for the given root.
GetSchemasByRoot(context.Context, string) ([]SchemaDescription, error)

// GetAllSchemas returns all schema versions that currently exist within
// this [Store].
GetAllSchemas(context.Context) ([]SchemaDescription, error)

// GetAllIndexes returns all the indexes that currently exist within this [Store].
GetAllIndexes(context.Context) (map[CollectionName][]IndexDescription, error)

Expand Down
217 changes: 217 additions & 0 deletions client/mocks/db.go

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

Loading