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

Merged
merged 12 commits into from
Jan 17, 2025

Conversation

ChrisBQu
Copy link
Collaborator

@ChrisBQu ChrisBQu commented Jan 2, 2025

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

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • 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).
  • 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

@ChrisBQu ChrisBQu added feature New feature or request area/cli Related to the CLI binary labels Jan 2, 2025
@ChrisBQu ChrisBQu added this to the DefraDB v0.16 milestone Jan 2, 2025
@ChrisBQu ChrisBQu self-assigned this Jan 2, 2025
Copy link

codecov bot commented Jan 2, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 17 lines in your changes missing coverage. Please review.

Project coverage is 78.45%. Comparing base (10e0f38) to head (25f533d).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
cli/schema_add.go 53.57% 12 Missing and 1 partial ⚠️
cli/errors.go 33.33% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3352      +/-   ##
===========================================
- Coverage    78.46%   78.45%   -0.01%     
===========================================
  Files          392      392              
  Lines        35688    35707      +19     
===========================================
+ Hits         28001    28013      +12     
- Misses        6054     6059       +5     
- Partials      1633     1635       +2     
Flag Coverage Δ
all-tests 78.45% <50.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
cli/errors.go 9.09% <33.33%> (+9.09%) ⬆️
cli/schema_add.go 68.25% <53.57%> (-3.75%) ⬇️

... and 13 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 10e0f38...25f533d. Read the comment docs.

@ChrisBQu ChrisBQu requested a review from a team January 6, 2025 16:00
cli/config.go Outdated Show resolved Hide resolved
@ChrisBQu ChrisBQu marked this pull request as ready for review January 13, 2025 16:47
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

question: Why is this a draft PR?

todo: Please add some tests.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

thought: IMO it would be nice to standardise this, so that all file-based inputs can be expected to work the same way.

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
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: This is a nice and simple way of ensure the whole operation takes place in a single txn.

thought: This does introduce a difference between the 3 clients - it might be nicer for users if instead we changed the other 2 to accept multiple strings. You would then be able to test this using integration tests instead of CLI specific stuff.

I think there is also an argument to be had that without changing the other 2 clients, the CLI should not have this feature, and instead users can just use cat/type in their terminal.

for _, schemaFile := range schemaFiles {
data, err := os.ReadFile(schemaFile)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", schemaFile, err)
Copy link
Member

Choose a reason for hiding this comment

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

todo: please move the errors to the cli/errors.go file.

data, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
return fmt.Errorf("failed to read schema from stdin: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

todo: please move the errors to the cli/errors.go file.

if err != nil {
return fmt.Errorf("failed to add schema: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

todo: please move the errors to the cli/errors.go file.

@@ -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
Copy link
Member

@shahzadlone shahzadlone Jan 16, 2025

Choose a reason for hiding this comment

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

thought: saying strings over string kind of signals to me that this is valid:

  1. defradb client schema add -f "schema1.graphql" "schema2.graphql"

rather than:

  1. defradb client schema add -f "schema1.graphql" -f "schema2.graphql"

non-blocking: because maybe it might just be me

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer your suggestion, but I have a vague memory of the team having this discussion and deciding on the multiple -fs. I hope I am wrong though :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have an opinion on this, but not a strong one. Happy to go with the team consensus (or majority opinion.)

Copy link
Member

@shahzadlone shahzadlone Jan 16, 2025

Choose a reason for hiding this comment

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

I don't have a strong prefer at all on the approach we take. My thought was only on the changed documentation in this PR, I think it should say string instead of strings if we are going with the approach introduced in this PR (2. above).

Happy with strings terminology if we are switching to 1. above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I see where you are coming from now. Hmm... I think I agree. I will be changing that to the singular as you suggest.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry commented on the main PR thread, can continue here if you like: #3352 (comment)

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Chris!

Approving now, on condition that:

  1. Shahzad is happy with the f flag docs
  2. You have manually tested this with multiple SDLs

Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this, don't forget to change to string as I still see it as strings still.

Also please do make an issue to do a similar change for other file arguments in other cli commands (like Andy suggested) if they won't be done in this PR.

Comment on lines +71 to +81
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)
}
Copy link
Member

Choose a reason for hiding this comment

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

praise: Thanks for doing this suggestion :)

@shahzadlone
Copy link
Member

shahzadlone commented Jan 16, 2025

thought/question:

@ChrisBQu

Looking at the wayStringSliceVarP is used, and finding out that the change from string to strings was an autogenerated thing.

I have a hunch that both the -f with list of strings and multiple -fs will both work?

I haven't tested, but I suspect that:

defradb client schema add -f="schema1.graphql,schema2.graphql" -f="schema3.graphql"

Will result in all 3 files being loaded?

Meaning that this:

defradb client schema add -f="schema1.graphql,schema2.graphql"

Also works? Have you tested this? (so 1 and 2 both work in a way from my previous comment?)

Sorry for the bother if this assumption of mine is incorrect.

Got the hunch after reading this: https://github.com/spf13/pflag/blob/v1.0.5/string_slice.go#L120

@ChrisBQu
Copy link
Collaborator Author

thought/question:

@ChrisBQu

Looking at the wayStringSliceVarP is used, and finding out that the change from string to strings was an autogenerated thing.

I have a hunch that both the -f with list of strings and multiple -fs will both work?

I haven't tested, but I suspect that:

defradb client schema add -f="schema1.graphql,schema2.graphql" -f="schema3.graphql"

Will result in all 3 files being loaded?

Meaning that this:

defradb client schema add -f="schema1.graphql,schema2.graphql"

Also works? Have you tested this? (so 1 and 2 both work in a way from my previous comment?)

Sorry for the bother if this assumption of mine is incorrect.

Got the hunch after reading this: https://github.com/spf13/pflag/blob/v1.0.5/string_slice.go#L120

This is correct. I did test it. In other words: separating by a comma on a single -f flag does work.

And if both work, maybe the plural of strings is desired(?)

@shahzadlone
Copy link
Member

This is correct. I did test it. In other words: separating by a comma on a single -f flag does work.

And if both work, maybe the plural of strings is desired(?)

Ok perfect. Then in that case please add an example of this case in the cli command documentation demonstrating both these cases. And strings makes sense then.

@ChrisBQu ChrisBQu merged commit 3779cbb into sourcenetwork:develop Jan 17, 2025
42 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cli Related to the CLI binary feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

schema add command to support multiple files
4 participants