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: Multiple schema file support in schema add #3352

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion acp/acp_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package acp

import (
"context"
"errors"

protoTypes "github.com/cosmos/gogoproto/types"
"github.com/sourcenetwork/acp_core/pkg/auth"
Expand All @@ -21,6 +20,8 @@ import (
"github.com/sourcenetwork/acp_core/pkg/types"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/errors"

"github.com/sourcenetwork/defradb/acp/identity"
)

Expand Down
3 changes: 2 additions & 1 deletion cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package cli

import (
"errors"
"os"
"path/filepath"
"strings"
Expand All @@ -20,6 +19,8 @@ import (
"github.com/sourcenetwork/corelog"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/sourcenetwork/defradb/errors"
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Might be worth adding a linter rule for this check.

)

const (
Expand Down
42 changes: 29 additions & 13 deletions cli/schema_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

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 +36,56 @@ 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 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 fmt.Errorf("failed to read file %s: %w", 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 fmt.Errorf("failed to read schema from stdin: %w", 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")
}

cols, err := store.AddSchema(cmd.Context(), schema)
// Process the combined schema
cols, err := store.AddSchema(cmd.Context(), combinedSchema)
if err != nil {
return fmt.Errorf("failed to add schema: %w", 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(s) to load schema from")
return cmd
}
3 changes: 2 additions & 1 deletion crypto/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ package crypto

import (
"crypto/rand"
"errors"
"io"
"os"
"strings"

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

const AESNonceSize = 12
Expand Down
7 changes: 5 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,9 @@ 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 stdin:
cat schema.graphql | defradb client schema add -

Expand All @@ -29,8 +32,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(s) to load schema from
-h, --help help for add
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion internal/db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package db
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/errors"
"github.com/sourcenetwork/defradb/internal/db/base"
"github.com/sourcenetwork/defradb/internal/db/description"
"github.com/sourcenetwork/defradb/internal/db/fetcher"
Expand Down
Loading