Skip to content

Commit

Permalink
feat: Add PatchCollection (#2402)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2389

## Description

Adds the PatchCollection command.

Mutating anything but the collection name is currently disabled, we can
expand this as we see fit, but for now I'd prefer to keep the initial PR
small.

This change means that the Collection Name is no longer always going to
be the same as the Schema Name.
  • Loading branch information
AndrewSisley authored Mar 14, 2024
1 parent d524320 commit c67bc56
Show file tree
Hide file tree
Showing 36 changed files with 1,798 additions and 95 deletions.
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func NewDefraCommand() *cobra.Command {
MakeCollectionUpdateCommand(),
MakeCollectionCreateCommand(),
MakeCollectionDescribeCommand(),
MakeCollectionPatchCommand(),
)

client := MakeClientCommand()
Expand Down
69 changes: 69 additions & 0 deletions cli/collection_patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 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 (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
)

func MakeCollectionPatchCommand() *cobra.Command {
var patchFile string
var cmd = &cobra.Command{
Use: "patch [patch]",
Short: "Patch existing collection descriptions",
Long: `Patch existing collection descriptions.
Uses JSON Patch to modify collection descriptions.
Example: patch from an argument string:
defradb client collection patch '[{ "op": "add", "path": "...", "value": {...} }]'
Example: patch from file:
defradb client collection patch -p patch.json
Example: patch from stdin:
cat patch.json | defradb client collection patch -
To learn more about the DefraDB GraphQL Schema Language, refer to https://docs.source.network.`,
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

var patch string
switch {
case patchFile != "":
data, err := os.ReadFile(patchFile)
if err != nil {
return err
}
patch = string(data)
case len(args) > 0 && args[0] == "-":
data, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
patch = string(data)
case len(args) == 1:
patch = args[0]
default:
return fmt.Errorf("patch cannot be empty")
}

return store.PatchCollection(cmd.Context(), patch)
},
}
cmd.Flags().StringVarP(&patchFile, "patch-file", "p", "", "File to load a patch from")
return cmd
}
2 changes: 1 addition & 1 deletion cli/schema_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Example: patch from an argument string:
defradb client schema patch '[{ "op": "add", "path": "...", "value": {...} }]' '{"lenses": [...'
Example: patch from file:
defradb client schema patch -f patch.json
defradb client schema patch -p patch.json
Example: patch from stdin:
cat patch.json | defradb client schema patch -
Expand Down
11 changes: 11 additions & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ type Store interface {
// A lens configuration may also be provided, it will be added to all collections using the schema.
PatchSchema(context.Context, string, immutable.Option[model.Lens], bool) error

// PatchCollection takes the given JSON patch string and applies it to the set of CollectionDescriptions
// present in the database.
//
// It will also update the GQL types used by the query system. It will error and not apply any of the
// requested, valid updates should the net result of the patch result in an invalid state. The
// individual operations defined in the patch do not need to result in a valid state, only the net result
// of the full patch.
//
// Currently only the collection name can be modified.
PatchCollection(context.Context, string) error

// SetActiveSchemaVersion activates all collection versions with the given schema version, and deactivates all
// those without it (if they share the same schema root).
//
Expand Down
43 changes: 43 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

0 comments on commit c67bc56

Please sign in to comment.