fix(orm): fix edge case exporting json (#11134)

* fix(orm): fix edge case exporting json

* add import json test
This commit is contained in:
Aaron Craelius 2022-02-07 17:36:12 -05:00 committed by GitHub
parent 1944a0883e
commit ebddeee360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 9 deletions

View File

@ -213,7 +213,9 @@ func (t autoIncrementTable) ExportJSON(ctx context.Context, writer io.Writer) er
return err return err
} }
start := true
if seq != 0 { if seq != 0 {
start = false
bz, err := json.Marshal(seq) bz, err := json.Marshal(seq)
if err != nil { if err != nil {
return err return err
@ -222,14 +224,9 @@ func (t autoIncrementTable) ExportJSON(ctx context.Context, writer io.Writer) er
if err != nil { if err != nil {
return err return err
} }
_, err = writer.Write([]byte(",\n"))
if err != nil {
return err
}
} }
return t.doExportJSON(ctx, writer) return t.doExportJSON(ctx, writer, start)
} }
func (t *autoIncrementTable) GetTable(message proto.Message) Table { func (t *autoIncrementTable) GetTable(message proto.Message) Table {

View File

@ -65,6 +65,19 @@ func runAutoIncrementScenario(t *testing.T, table ormtable.AutoIncrementTable, c
store2 := ormtable.WrapContextDefault(testkv.NewSplitMemBackend()) store2 := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
assert.NilError(t, table.ImportJSON(store2, bytes.NewReader(buf.Bytes()))) assert.NilError(t, table.ImportJSON(store2, bytes.NewReader(buf.Bytes())))
assertTablesEqual(t, table, ctx, store2) assertTablesEqual(t, table, ctx, store2)
// test edge case where we have deleted all entities but we're still exporting the sequence number
assert.NilError(t, table.Delete(ctx, ex1))
assert.NilError(t, table.Delete(ctx, ex2))
buf = &bytes.Buffer{}
assert.NilError(t, table.ExportJSON(ctx, buf))
assert.NilError(t, table.ValidateJSON(bytes.NewReader(buf.Bytes())))
golden.Assert(t, buf.String(), "trivial_auto_inc_export.golden")
store3 := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
assert.NilError(t, table.ImportJSON(store3, bytes.NewReader(buf.Bytes())))
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
} }
func TestBadJSON(t *testing.T) { func TestBadJSON(t *testing.T) {

View File

@ -293,10 +293,10 @@ func (t tableImpl) ExportJSON(context context.Context, writer io.Writer) error {
return err return err
} }
return t.doExportJSON(context, writer) return t.doExportJSON(context, writer, true)
} }
func (t tableImpl) doExportJSON(ctx context.Context, writer io.Writer) error { func (t tableImpl) doExportJSON(ctx context.Context, writer io.Writer, start bool) error {
marshalOptions := protojson.MarshalOptions{ marshalOptions := protojson.MarshalOptions{
UseProtoNames: true, UseProtoNames: true,
Resolver: t.typeResolver, Resolver: t.typeResolver,
@ -304,7 +304,6 @@ func (t tableImpl) doExportJSON(ctx context.Context, writer io.Writer) error {
var err error var err error
it, _ := t.List(ctx, nil) it, _ := t.List(ctx, nil)
start := true
for { for {
found := it.Next() found := it.Next()

View File

@ -52,3 +52,21 @@ ITERATOR 0300 -> 0301
PK testpb.ExampleAutoIncrementTable 2 -> {"id":2,"x":"bar","y":10} PK testpb.ExampleAutoIncrementTable 2 -> {"id":2,"x":"bar","y":10}
NEXT NEXT
VALID false VALID false
GET 03000001 1203666f6f1805
PK testpb.ExampleAutoIncrementTable 1 -> {"id":1,"x":"foo","y":5}
ORM DELETE testpb.ExampleAutoIncrementTable {"id":1,"x":"foo","y":5}
DEL 03000001
DEL PK testpb.ExampleAutoIncrementTable 1 -> {"id":1}
DEL 0301666f6f
DEL ERR:EOF
GET 03000002 1203626172180a
PK testpb.ExampleAutoIncrementTable 2 -> {"id":2,"x":"bar","y":10}
ORM DELETE testpb.ExampleAutoIncrementTable {"id":2,"x":"bar","y":10}
DEL 03000002
DEL PK testpb.ExampleAutoIncrementTable 2 -> {"id":2}
DEL 0301626172
DEL ERR:EOF
GET 03808002 02
SEQ testpb.ExampleAutoIncrementTable 2
ITERATOR 0300 -> 0301
VALID false

View File

@ -0,0 +1 @@
[2]