Skip to content

Commit

Permalink
feat: Multiple schema file support in schema add (#3352)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #1248 

## Description

Previously, a single schema file could be added by calling `defradb
schema add -f filename`. Now we can add multiple new schemas from
different files in the following way: `defradb schema add -f file_one -f
file_two -f file_three`

This PR also adjusts some import statements that were using the
incorrect `error` import.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

Specify the platform(s) on which this was tested:
- Windows
  • Loading branch information
ChrisBQu authored Jan 17, 2025
1 parent 10e0f38 commit 3779cbb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
14 changes: 14 additions & 0 deletions cli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
errRequiredFlag string = "the required flag [--%s|-%s] is %s"
errInvalidAscensionOrder string = "invalid order: expected ASC or DESC"
errInvalidInxedFieldDescription string = "invalid or malformed field description"
errEmptySchemaString string = "schema cannot be empty"
)

var (
Expand All @@ -36,6 +37,7 @@ var (
ErrPolicyFileArgCanNotBeEmpty = errors.New("policy file argument can not be empty")
ErrPurgeForceFlagRequired = errors.New("run this command again with --force if you really want to purge all data")
ErrMissingKeyringSecret = errors.New("missing keyring secret")
ErrEmptySchemaString = errors.New(errEmptySchemaString)
)

func NewErrRequiredFlagEmpty(longName string, shortName string) error {
Expand Down Expand Up @@ -65,3 +67,15 @@ func NewErrInvalidAscensionOrder(fieldName string) error {
func NewErrInvalidInxedFieldDescription(fieldName string) error {
return errors.New(errInvalidInxedFieldDescription, errors.NewKV("Field", fieldName))
}

func NewErrFailedToReadSchemaFile(schemaFile string, inner error) error {
return errors.Wrap(fmt.Sprintf("failed to read file %s", schemaFile), inner)
}

func NewErrFailedToReadSchemaFromStdin(inner error) error {
return errors.Wrap("failed to read schema from stdin", inner)
}

func NewErrFailedToAddSchema(inner error) error {
return errors.Wrap("failed to add schema", inner)
}
48 changes: 33 additions & 15 deletions cli/schema_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
package cli

import (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
)

func MakeSchemaAddCommand() *cobra.Command {
var schemaFile string
var schemaFiles []string
var cmd = &cobra.Command{
Use: "add [schema]",
Short: "Add new schema",
Expand All @@ -36,40 +35,59 @@ Example: add from an argument string:
Example: add from file:
defradb client schema add -f schema.graphql
Example: add from multiple files:
defradb client schema add -f schema1.graphql -f schema2.graphql
Example: add from multiple files:
defradb client schema add -f schema1.graphql,schema2.graphql
Example: add from stdin:
cat schema.graphql | defradb client schema add -
Learn more about the DefraDB GraphQL Schema Language on https://docs.source.network.`,
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

var schema string
var combinedSchema string
switch {
case schemaFile != "":
data, err := os.ReadFile(schemaFile)
if err != nil {
return err
case len(schemaFiles) > 0:
// Read schemas from files and concatenate them
for _, schemaFile := range schemaFiles {
data, err := os.ReadFile(schemaFile)
if err != nil {
return NewErrFailedToReadSchemaFile(schemaFile, err)
}
combinedSchema += string(data) + "\n"
}
schema = string(data)

case len(args) > 0 && args[0] == "-":
// Read schema from stdin
data, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
return NewErrFailedToReadSchemaFromStdin(err)
}
schema = string(data)
combinedSchema += string(data) + "\n"

case len(args) > 0:
schema = args[0]
// Read schema from argument string
combinedSchema += args[0] + "\n"

default:
return fmt.Errorf("schema cannot be empty")
return ErrEmptySchemaString
}

cols, err := store.AddSchema(cmd.Context(), schema)
// Process the combined schema
cols, err := store.AddSchema(cmd.Context(), combinedSchema)
if err != nil {
return NewErrFailedToAddSchema(err)
}
if err := writeJSON(cmd, cols); err != nil {
return err
}
return writeJSON(cmd, cols)

return nil
},
}
cmd.Flags().StringVarP(&schemaFile, "file", "f", "", "File to load a schema from")
cmd.Flags().StringSliceVarP(&schemaFiles, "file", "f", []string{}, "File to load schema from")
return cmd
}
10 changes: 8 additions & 2 deletions docs/website/references/cli/defradb_client_schema_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Example: add from an argument string:
Example: add from file:
defradb client schema add -f schema.graphql

Example: add from multiple files:
defradb client schema add -f schema1.graphql -f schema2.graphql

Example: add from multiple files:
defradb client schema add -f schema1.graphql,schema2.graphql

Example: add from stdin:
cat schema.graphql | defradb client schema add -

Expand All @@ -29,8 +35,8 @@ defradb client schema add [schema] [flags]
### Options

```
-f, --file string File to load a schema from
-h, --help help for add
-f, --file strings File to load schema from
-h, --help help for add
```

### Options inherited from parent commands
Expand Down

0 comments on commit 3779cbb

Please sign in to comment.