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: Change GetCollectionBySchemaFoo funcs to return many #1984

Merged
38 changes: 36 additions & 2 deletions cli/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@
store := mustGetStoreContext(cmd)

var col client.Collection
var cols []client.Collection
switch {
case versionID != "":
col, err = store.GetCollectionByVersionID(cmd.Context(), versionID)
cols, err = store.GetCollectionsByVersionID(cmd.Context(), versionID)

Check warning on line 49 in cli/collection.go

View check run for this annotation

Codecov / codecov/patch

cli/collection.go#L49

Added line #L49 was not covered by tests

case schemaID != "":
col, err = store.GetCollectionBySchemaID(cmd.Context(), schemaID)
cols, err = store.GetCollectionsBySchemaID(cmd.Context(), schemaID)

Check warning on line 52 in cli/collection.go

View check run for this annotation

Codecov / codecov/patch

cli/collection.go#L52

Added line #L52 was not covered by tests

case name != "":
col, err = store.GetCollectionByName(cmd.Context(), name)
cols = []client.Collection{col}

default:
return nil
Expand All @@ -60,6 +62,38 @@
if err != nil {
return err
}

if schemaID != "" && versionID != "" && len(cols) > 0 {
if cols[0].SchemaID() != schemaID {
// If the a versionID has been provided that does not pair up with the given schemaID
// we should error and let the user know they have provided impossible params.
// We only need to check the first item - they will all be the same.
return NewErrSchemaVersionNotOfSchema(schemaID, versionID)
}

Check warning on line 72 in cli/collection.go

View check run for this annotation

Codecov / codecov/patch

cli/collection.go#L67-L72

Added lines #L67 - L72 were not covered by tests
}

if name != "" {
// Multiple params may have been specified, and in some cases both are needed.
// For example if a schema version and a collection name have been provided,
// we need to ensure that a collection at the requested version is returned.
// Likewise we need to ensure that if a collection name and schema id are provided,
// but there are none matching both, that nothing is returned.
fetchedCols := cols
cols = nil
for _, c := range fetchedCols {
if c.Name() == name {
cols = append(cols, c)
break
}
}
}

if len(cols) != 1 {
// If more than one collection matches the given criteria we cannot set the context collection
return nil
}

Check warning on line 94 in cli/collection.go

View check run for this annotation

Codecov / codecov/patch

cli/collection.go#L92-L94

Added lines #L92 - L94 were not covered by tests
col = cols[0]

if tx, ok := cmd.Context().Value(txContextKey).(datastore.Txn); ok {
col = col.WithTxn(tx)
}
Expand Down
26 changes: 19 additions & 7 deletions cli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@
"github.com/sourcenetwork/defradb/errors"
)

const errInvalidLensConfig = "invalid lens configuration"
const (
errInvalidLensConfig string = "invalid lens configuration"
errSchemaVersionNotOfSchema string = "the given schema version is from a different schema"
)

var (
ErrNoDocOrFile = errors.New("document or file must be defined")
ErrInvalidDocument = errors.New("invalid document")
ErrNoDocKeyOrFilter = errors.New("document key or filter must be defined")
ErrInvalidExportFormat = errors.New("invalid export format")
ErrNoLensConfig = errors.New("lens config cannot be empty")
ErrInvalidLensConfig = errors.New("invalid lens configuration")
ErrNoDocOrFile = errors.New("document or file must be defined")
ErrInvalidDocument = errors.New("invalid document")
ErrNoDocKeyOrFilter = errors.New("document key or filter must be defined")
ErrInvalidExportFormat = errors.New("invalid export format")
ErrNoLensConfig = errors.New("lens config cannot be empty")
ErrInvalidLensConfig = errors.New("invalid lens configuration")
ErrSchemaVersionNotOfSchema = errors.New(errSchemaVersionNotOfSchema)
)

func NewErrInvalidLensConfig(inner error) error {
return errors.Wrap(errInvalidLensConfig, inner)
}

func NewErrSchemaVersionNotOfSchema(schemaID string, schemaVersionID string) error {
return errors.New(
errSchemaVersionNotOfSchema,
errors.NewKV("SchemaID", schemaID),
errors.NewKV("SchemaVersionID", schemaVersionID),
)

Check warning on line 41 in cli/errors.go

View check run for this annotation

Codecov / codecov/patch

cli/errors.go#L36-L41

Added lines #L36 - L41 were not covered by tests
}
12 changes: 6 additions & 6 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ type Store interface {
// If no matching collection is found an error will be returned.
GetCollectionByName(context.Context, CollectionName) (Collection, error)

// GetCollectionBySchemaID attempts to retrieve a collection matching the given schema ID.
// GetCollectionsBySchemaID attempts to retrieve all collections using the given schema ID.
//
// If no matching collection is found an error will be returned.
GetCollectionBySchemaID(context.Context, string) (Collection, error)
// If no matching collection is found an empty set will be returned.
GetCollectionsBySchemaID(context.Context, string) ([]Collection, error)

// GetCollectionBySchemaID attempts to retrieve a collection matching the given schema version ID.
// GetCollectionsByVersionID attempts to retrieve all collections using the given schema version ID.
//
// If no matching collection is found an error will be returned.
GetCollectionByVersionID(context.Context, string) (Collection, error)
// If no matching collections are found an empty set will be returned.
GetCollectionsByVersionID(context.Context, string) ([]Collection, error)

// GetAllCollections returns all the collections and their descriptions that currently exist within
// this [Store].
Expand Down
16 changes: 16 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
errUninitializeProperty string = "invalid state, required property is uninitialized"
errMaxTxnRetries string = "reached maximum transaction reties"
errRelationOneSided string = "relation must be defined on both schemas"
errCollectionNotFound string = "collection not found"
)

// Errors returnable from this package.
Expand All @@ -45,6 +46,7 @@
ErrInvalidDocKeyVersion = errors.New("invalid DocKey version")
ErrMaxTxnRetries = errors.New(errMaxTxnRetries)
ErrRelationOneSided = errors.New(errRelationOneSided)
ErrCollectionNotFound = errors.New(errCollectionNotFound)
)

// NewErrFieldNotExist returns an error indicating that the given field does not exist.
Expand Down Expand Up @@ -107,3 +109,17 @@
errors.NewKV("Type", typeName),
)
}

func NewErrCollectionNotFoundForSchemaVersion(schemaVersionID string) error {
return errors.New(
errCollectionNotFound,
errors.NewKV("SchemaVersionID", schemaVersionID),
)

Check warning on line 117 in client/errors.go

View check run for this annotation

Codecov / codecov/patch

client/errors.go#L113-L117

Added lines #L113 - L117 were not covered by tests
}

func NewErrCollectionNotFoundForSchema(schemaID string) error {
return errors.New(
errCollectionNotFound,
errors.NewKV("SchemaID", schemaID),
)

Check warning on line 124 in client/errors.go

View check run for this annotation

Codecov / codecov/patch

client/errors.go#L120-L124

Added lines #L120 - L124 were not covered by tests
}
56 changes: 28 additions & 28 deletions client/mocks/db.go

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

Loading