refactor(ORM)!: InsertReturningID -> InsertReturning<PrimaryKeyName> (#11659)

## Description

- changes the generated function signature for InsertReturningID to InsertReturning[AutoIncrement Field Name] 

Closes: #11655 



---

### 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:
Tyler
2022-05-13 21:47:55 +00:00
committed by GitHub
parent 16c8e2739f
commit f6150bd4af
8 changed files with 743 additions and 70 deletions
+20 -18
View File
@@ -22,7 +22,7 @@ type autoIncrementTable struct {
seqCodec *ormkv.SeqCodec
}
func (t autoIncrementTable) InsertReturningID(ctx context.Context, message proto.Message) (newId uint64, err error) {
func (t autoIncrementTable) InsertReturningPKey(ctx context.Context, message proto.Message) (newPK uint64, err error) {
backend, err := t.getWriteBackend(ctx)
if err != nil {
return 0, err
@@ -61,7 +61,7 @@ func (t autoIncrementTable) Update(ctx context.Context, message proto.Message) e
return err
}
func (t *autoIncrementTable) save(ctx context.Context, backend Backend, message proto.Message, mode saveMode) (newId uint64, err error) {
func (t *autoIncrementTable) save(ctx context.Context, backend Backend, message proto.Message, mode saveMode) (newPK uint64, err error) {
messageRef := message.ProtoReflect()
val := messageRef.Get(t.autoIncField).Uint()
writer := newBatchIndexCommitmentWriter(backend)
@@ -73,12 +73,12 @@ func (t *autoIncrementTable) save(ctx context.Context, backend Backend, message
}
mode = saveModeInsert
newId, err = t.nextSeqValue(writer.IndexStore())
newPK, err = t.nextSeqValue(writer.IndexStore())
if err != nil {
return 0, err
}
messageRef.Set(t.autoIncField, protoreflect.ValueOfUint64(newId))
messageRef.Set(t.autoIncField, protoreflect.ValueOfUint64(newPK))
} else {
if mode == saveModeInsert {
return 0, ormerrors.AutoIncrementKeyAlreadySet
@@ -87,7 +87,7 @@ func (t *autoIncrementTable) save(ctx context.Context, backend Backend, message
mode = saveModeUpdate
}
return newId, t.tableImpl.doSave(ctx, writer, message, mode)
return newPK, t.tableImpl.doSave(ctx, writer, message, mode)
}
func (t *autoIncrementTable) curSeqValue(kv kv.ReadonlyStore) (uint64, error) {
@@ -121,11 +121,12 @@ func (t autoIncrementTable) EncodeEntry(entry ormkv.Entry) (k, v []byte, err err
}
func (t autoIncrementTable) ValidateJSON(reader io.Reader) error {
return t.decodeAutoIncJson(nil, reader, func(message proto.Message, maxID uint64) error {
return t.decodeAutoIncJson(nil, reader, func(message proto.Message, maxSeq uint64) error {
messageRef := message.ProtoReflect()
id := messageRef.Get(t.autoIncField).Uint()
if id > maxID {
return fmt.Errorf("invalid ID %d, expected a value <= %d, the highest sequence number", id, maxID)
pkey := messageRef.Get(t.autoIncField).Uint()
if pkey > maxSeq {
return fmt.Errorf("invalid auto increment primary key %d, expected a value <= %d, the highest "+
"sequence number", pkey, maxSeq)
}
if t.customJSONValidator != nil {
@@ -142,22 +143,23 @@ func (t autoIncrementTable) ImportJSON(ctx context.Context, reader io.Reader) er
return err
}
return t.decodeAutoIncJson(backend, reader, func(message proto.Message, maxID uint64) error {
return t.decodeAutoIncJson(backend, reader, func(message proto.Message, maxSeq uint64) error {
messageRef := message.ProtoReflect()
id := messageRef.Get(t.autoIncField).Uint()
if id == 0 {
// we don't have an ID in the JSON, so we call Save to insert and
pkey := messageRef.Get(t.autoIncField).Uint()
if pkey == 0 {
// we don't have a primary key in the JSON, so we call Save to insert and
// generate one
_, err = t.save(ctx, backend, message, saveModeInsert)
return err
} else {
if id > maxID {
return fmt.Errorf("invalid ID %d, expected a value <= %d, the highest sequence number", id, maxID)
if pkey > maxSeq {
return fmt.Errorf("invalid auto increment primary key %d, expected a value <= %d, the highest "+
"sequence number", pkey, maxSeq)
}
// we do have an ID and calling Save will fail because it expects
// either no ID or SAVE_MODE_UPDATE. So instead we drop one level
// we do have a primary key and calling Save will fail because it expects
// either no primary key or SAVE_MODE_UPDATE. So instead we drop one level
// down and insert using tableImpl which doesn't know about
// auto-incrementing IDs
// auto-incrementing primary keys.
return t.tableImpl.save(ctx, backend, message, saveModeInsert)
}
})
+3 -3
View File
@@ -54,7 +54,7 @@ func runAutoIncrementScenario(t *testing.T, table ormtable.AutoIncrementTable, c
assert.Equal(t, uint64(1), ex1.Id)
ex2 := &testpb.ExampleAutoIncrementTable{X: "bar", Y: 10}
newId, err := table.InsertReturningID(ctx, ex2)
newId, err := table.InsertReturningPKey(ctx, ex2)
assert.NilError(t, err)
assert.Equal(t, uint64(2), ex2.Id)
assert.Equal(t, newId, ex2.Id)
@@ -89,9 +89,9 @@ func TestBadJSON(t *testing.T) {
store := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
f, err := os.Open("testdata/bad_auto_inc.json")
assert.NilError(t, err)
assert.ErrorContains(t, table.ImportJSON(store, f), "invalid ID")
assert.ErrorContains(t, table.ImportJSON(store, f), "invalid auto increment primary key")
f, err = os.Open("testdata/bad_auto_inc2.json")
assert.NilError(t, err)
assert.ErrorContains(t, table.ImportJSON(store, f), "invalid ID")
assert.ErrorContains(t, table.ImportJSON(store, f), "invalid auto increment primary key")
}
+3 -3
View File
@@ -153,7 +153,7 @@ type Schema interface {
type AutoIncrementTable interface {
Table
// InsertReturningID inserts the provided entry in the store and returns the newly
// generated ID for the message or an error.
InsertReturningID(ctx context.Context, message proto.Message) (newId uint64, err error)
// InsertReturningPKey inserts the provided entry in the store and returns the newly
// generated primary key for the message or an error.
InsertReturningPKey(ctx context.Context, message proto.Message) (newPK uint64, err error)
}
+16
View File
@@ -24,6 +24,7 @@ import (
sdkerrors "cosmossdk.io/errors"
queryv1beta1 "github.com/cosmos/cosmos-sdk/api/cosmos/base/query/v1beta1"
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
"github.com/cosmos/cosmos-sdk/orm/internal/testkv"
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
@@ -790,3 +791,18 @@ func TestReadonly(t *testing.T) {
ctx := ormtable.WrapContextDefault(readBackend)
assert.ErrorIs(t, ormerrors.ReadOnly, table.Insert(ctx, &testpb.ExampleTable{}))
}
func TestInsertReturningFieldName(t *testing.T) {
table, err := ormtable.Build(ormtable.Options{
MessageType: (&testpb.ExampleAutoIncFieldName{}).ProtoReflect().Type(),
})
backend := testkv.NewSplitMemBackend()
ctx := ormtable.WrapContextDefault(backend)
store, err := testpb.NewExampleAutoIncFieldNameTable(table)
assert.NilError(t, err)
foo, err := store.InsertReturningFoo(ctx, &testpb.ExampleAutoIncFieldName{
Bar: 45,
})
assert.NilError(t, err)
assert.Equal(t, uint64(1), foo)
}