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(orm): add sequence getter for auto increment tables #15320

Merged
merged 8 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions orm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Feature

* [#15320](https://github.com/cosmos/cosmos-sdk/pull/15320) Add current sequence getter (`LastInsertedSequence`) for auto increment tables.

### API Breaking Changes

- [14822](https://github.com/cosmos/cosmos-sdk/pull/14822) Migrate to cosmossdk.io/core genesis API
* [#14822](https://github.com/cosmos/cosmos-sdk/pull/14822) Migrate to cosmossdk.io/core genesis API

### State-machine Breaking Changes

- [12273](https://github.com/cosmos/cosmos-sdk/pull/12273) The timestamp key encoding was reworked to properly handle nil values. Existing users will need to manually migrate their data to the new encoding before upgrading.
* [#12273](https://github.com/cosmos/cosmos-sdk/pull/12273) The timestamp key encoding was reworked to properly handle nil values. Existing users will need to manually migrate their data to the new encoding before upgrading.
6 changes: 6 additions & 0 deletions orm/internal/codegen/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (t tableGen) getTableInterface() {
t.P("Insert(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") error")
if t.table.PrimaryKey.AutoIncrement {
t.P("InsertReturning", fieldsToCamelCase(t.table.PrimaryKey.Fields), "(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") (uint64, error)")
t.P("LastInsertedSequence(ctx ", contextPkg.Ident("Context"), ") (uint64, error)")
}
t.P("Update(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") error")
t.P("Save(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") error")
Expand Down Expand Up @@ -184,6 +185,11 @@ func (t tableGen) genTableImpl() {
t.P("return ", receiverVar, ".table.InsertReturningPKey(ctx, ", varName, ")")
t.P("}")
t.P()

t.P(receiver, "LastInsertedSequence(ctx ", contextPkg.Ident("Context"), ") (uint64, error) {")
t.P("return ", receiverVar, ".table.LastInsertedSequence(ctx)")
t.P("}")
t.P()
}

// Has
Expand Down
2 changes: 1 addition & 1 deletion orm/internal/testpb/bank.pb.go

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

6 changes: 5 additions & 1 deletion orm/internal/testpb/bank_query.pb.go

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

15 changes: 15 additions & 0 deletions orm/internal/testpb/test_schema.cosmos_orm.go

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

3 changes: 2 additions & 1 deletion orm/internal/testpb/test_schema.pb.go

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

12 changes: 11 additions & 1 deletion orm/internal/testpb/test_schema_query.pb.go

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

9 changes: 9 additions & 0 deletions orm/model/ormtable/auto_increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func (t autoIncrementTable) Update(ctx context.Context, message proto.Message) e
return err
}

func (t autoIncrementTable) LastInsertedSequence(ctx context.Context) (uint64, error) {
backend, err := t.getBackend(ctx)
if err != nil {
return 0, err
}

return t.curSeqValue(backend.IndexStoreReader())
}

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()
Expand Down
9 changes: 9 additions & 0 deletions orm/model/ormtable/auto_increment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ func runAutoIncrementScenario(t *testing.T, table ormtable.AutoIncrementTable, c
ex1 := &testpb.ExampleAutoIncrementTable{X: "foo", Y: 5}
assert.NilError(t, store.Save(ctx, ex1))
assert.Equal(t, uint64(1), ex1.Id)
curSeq, err := table.LastInsertedSequence(ctx)
assert.NilError(t, err)
assert.Equal(t, curSeq, uint64(1))

ex2 := &testpb.ExampleAutoIncrementTable{X: "bar", Y: 10}
newId, err := table.InsertReturningPKey(ctx, ex2)
assert.NilError(t, err)
assert.Equal(t, uint64(2), ex2.Id)
assert.Equal(t, newId, ex2.Id)
curSeq, err = table.LastInsertedSequence(ctx)
assert.NilError(t, err)
assert.Equal(t, curSeq, uint64(2))

buf := &bytes.Buffer{}
assert.NilError(t, table.ExportJSON(ctx, buf))
Expand All @@ -78,6 +84,9 @@ func runAutoIncrementScenario(t *testing.T, table ormtable.AutoIncrementTable, c
ex1.Id = 0
assert.NilError(t, table.Insert(store3, ex1))
assert.Equal(t, uint64(3), ex1.Id) // should equal 3 because the sequence number 2 should have been imported from JSON
curSeq, err = table.LastInsertedSequence(store3)
assert.NilError(t, err)
assert.Equal(t, curSeq, uint64(3))
}

func TestBadJSON(t *testing.T) {
Expand Down
9 changes: 3 additions & 6 deletions orm/model/ormtable/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package ormtable
import (
"fmt"

"github.com/cosmos/cosmos-sdk/orm/internal/fieldnames"

"github.com/cosmos/cosmos-sdk/orm/encoding/encodeutil"

"google.golang.org/protobuf/reflect/protoregistry"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

ormv1 "cosmossdk.io/api/cosmos/orm/v1"

"github.com/cosmos/cosmos-sdk/orm/encoding/encodeutil"
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
"github.com/cosmos/cosmos-sdk/orm/internal/fieldnames"
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
)

Expand Down
4 changes: 4 additions & 0 deletions orm/model/ormtable/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type Table interface {

// Schema is an interface for things that contain tables and can encode and
// decode kv-store pairs.

type Schema interface {
ormkv.EntryCodec

Expand All @@ -156,4 +157,7 @@ type AutoIncrementTable interface {
// 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)

// LastInsertedSequence retrieves the last inserted sequence in the table.
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
LastInsertedSequence(ctx context.Context) (uint64, error)
}
4 changes: 4 additions & 0 deletions orm/model/ormtable/testdata/test_auto_inc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ SET 03808002 01
SET 0301666f6f 0001
UNIQ testpb.ExampleAutoIncrementTable x : foo -> 1
ORM AFTER INSERT testpb.ExampleAutoIncrementTable {"id":1,"x":"foo","y":5}
GET 03808002 01
SEQ testpb.ExampleAutoIncrementTable 1
GET 03808002 01
SEQ testpb.ExampleAutoIncrementTable 1
GET 03000002
Expand All @@ -28,6 +30,8 @@ SET 03808002 02
SET 0301626172 0002
UNIQ testpb.ExampleAutoIncrementTable x : bar -> 2
ORM AFTER INSERT testpb.ExampleAutoIncrementTable {"id":2,"x":"bar","y":10}
GET 03808002 02
SEQ testpb.ExampleAutoIncrementTable 2
GET 03808002 02
SEQ testpb.ExampleAutoIncrementTable 2
ITERATOR 0300 -> 0301
Expand Down