fix(orm): add additional checks on invalid tables (#11387)

## Description

Adds checks for two types of errors in table definitions
* defining a key on an `optional` field (haven't found a way to test unfortunately because a test case will break the code generator)
* defining a trivial unique key, essentially a unique key which contains all the fields in the primary key and is redundant



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Aaron Craelius
2022-04-05 19:48:58 +00:00
committed by GitHub
parent b9f5283769
commit 1486a669b7
15 changed files with 221 additions and 180 deletions
+7
View File
@@ -64,6 +64,13 @@ func NewUniqueKeyCodec(prefix []byte, messageType protoreflect.MessageType, inde
}
}
// if there is nothing in the value we have a trivial unique index
// which shouldn't actually be a unique index at all
if len(valueFields) == 0 {
return nil, ormerrors.InvalidTableDefinition.Wrapf("unique index %s on table %s introduces no new uniqueness constraint not already in the primary key and should not be marked as unique",
indexFields, messageType.Descriptor().FullName())
}
valueCodec, err := NewKeyCodec(nil, messageType, valueFields)
if err != nil {
return nil, err
+32 -1
View File
@@ -5,18 +5,36 @@ import (
"fmt"
"testing"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"gotest.tools/v3/assert"
"pgregory.net/rapid"
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
"github.com/cosmos/cosmos-sdk/orm/internal/testutil"
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
)
func TestUniqueKeyCodec(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
keyCodec := testutil.TestKeyCodecGen(1, 5).Draw(t, "keyCodec").(testutil.TestKeyCodec)
pkCodec := testutil.TestKeyCodecGen(1, 5).Draw(t, "primaryKeyCodec").(testutil.TestKeyCodec)
// check if we have a trivial unique index where all of the fields
// in the primary key are in the unique key, we should expect an
// error in this case
isInPk := map[protoreflect.Name]bool{}
for _, spec := range pkCodec.KeySpecs {
isInPk[spec.FieldName] = true
}
numPkFields := 0
for _, spec := range keyCodec.KeySpecs {
if isInPk[spec.FieldName] {
numPkFields++
}
}
isTrivialUniqueKey := numPkFields == len(pkCodec.KeySpecs)
messageType := (&testpb.ExampleTable{}).ProtoReflect().Type()
uniqueKeyCdc, err := ormkv.NewUniqueKeyCodec(
keyCodec.Codec.Prefix(),
@@ -24,7 +42,14 @@ func TestUniqueKeyCodec(t *testing.T) {
keyCodec.Codec.GetFieldNames(),
pkCodec.Codec.GetFieldNames(),
)
assert.NilError(t, err)
if isTrivialUniqueKey {
assert.ErrorContains(t, err, "no new uniqueness constraint")
return
} else {
assert.NilError(t, err)
}
for i := 0; i < 100; i++ {
a := testutil.GenA.Draw(t, fmt.Sprintf("a%d", i)).(*testpb.ExampleTable)
key := keyCodec.Codec.GetKeyValues(a.ProtoReflect())
@@ -60,3 +85,9 @@ func TestUniqueKeyCodec(t *testing.T) {
}
})
}
func TestTrivialUnique(t *testing.T) {
_, err := ormkv.NewUniqueKeyCodec(nil, (&testpb.ExampleTable{}).ProtoReflect().Type(),
[]protoreflect.Name{"u32", "str"}, []protoreflect.Name{"str", "u32"})
assert.ErrorIs(t, err, ormerrors.InvalidTableDefinition)
}