From 69387e311b5094bc0d6d54c0b70244c7b488a94d Mon Sep 17 00:00:00 2001 From: Sugu Sougoumarane Date: Fri, 28 Apr 2017 11:55:25 -0700 Subject: [PATCH] fix for #2804 The change preserves the insert id coming from mysql autoinc for unsharded tables. --- go/vt/vtgate/engine/route.go | 6 ++- go/vt/vtgate/router_dml_test.go | 60 +++++++++++++++++++++++++++ go/vt/vtgate/router_framework_test.go | 3 +- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/go/vt/vtgate/engine/route.go b/go/vt/vtgate/engine/route.go index 91ff189fbac..d4d5ce669de 100644 --- a/go/vt/vtgate/engine/route.go +++ b/go/vt/vtgate/engine/route.go @@ -455,7 +455,10 @@ func (route *Route) execInsertUnsharded(vcursor VCursor, queryConstruct *queryin if err != nil { return nil, fmt.Errorf("execInsertUnsharded: %v", err) } - result.InsertID = uint64(insertid) + + if insertid != 0 { + result.InsertID = uint64(insertid) + } return result, nil } @@ -478,7 +481,6 @@ func (route *Route) execInsertSharded(vcursor VCursor, queryConstruct *queryinfo if insertid != 0 { result.InsertID = uint64(insertid) } - return result, nil } diff --git a/go/vt/vtgate/router_dml_test.go b/go/vt/vtgate/router_dml_test.go index 52e1bbc101b..695b652a3f1 100644 --- a/go/vt/vtgate/router_dml_test.go +++ b/go/vt/vtgate/router_dml_test.go @@ -452,6 +452,36 @@ func TestInsertGeneratorSharded(t *testing.T) { } } +func TestInsertAutoincSharded(t *testing.T) { + router, sbc, _, _ := createRouterEnv() + + // Fake a mysql auto-inc response. + wantResult := &sqltypes.Result{ + Rows: [][]sqltypes.Value{{ + sqltypes.MakeTrusted(sqltypes.Int64, []byte("1")), + }}, + RowsAffected: 2, + InsertID: 2, + } + sbc.SetResults([]*sqltypes.Result{wantResult}) + result, err := routerExec(router, "insert into user_extra(user_id) values (2)", nil) + if err != nil { + t.Error(err) + } + wantQueries := []querytypes.BoundQuery{{ + Sql: "insert into user_extra(user_id) values (:_user_id0) /* vtgate:: keyspace_id:06e7ea22ce92708f */", + BindVariables: map[string]interface{}{ + "_user_id0": int64(2), + }, + }} + if !reflect.DeepEqual(sbc.Queries, wantQueries) { + t.Errorf("sbc.Queries:\n%+v, want\n%+v\n", sbc.Queries, wantQueries) + } + if !reflect.DeepEqual(result, wantResult) { + t.Errorf("result: %+v, want %+v", result, wantResult) + } +} + func TestInsertGeneratorUnsharded(t *testing.T) { router, _, _, sbclookup := createRouterEnv() result, err := routerExec(router, "insert into main1(id, name) values (null, 'myname')", nil) @@ -477,6 +507,36 @@ func TestInsertGeneratorUnsharded(t *testing.T) { } } +func TestInsertAutoincUnsharded(t *testing.T) { + router, _, _, sbclookup := createRouterEnv() + + // Fake a mysql auto-inc response. + query := "insert into simple(val) values ('val')" + wantResult := &sqltypes.Result{ + Rows: [][]sqltypes.Value{{ + sqltypes.MakeTrusted(sqltypes.Int64, []byte("1")), + }}, + RowsAffected: 2, + InsertID: 2, + } + sbclookup.SetResults([]*sqltypes.Result{wantResult}) + + result, err := routerExec(router, query, nil) + if err != nil { + t.Error(err) + } + wantQueries := []querytypes.BoundQuery{{ + Sql: query, + BindVariables: map[string]interface{}{}, + }} + if !reflect.DeepEqual(sbclookup.Queries, wantQueries) { + t.Errorf("sbclookup.Queries: \n%#v, want \n%#v\n", sbclookup.Queries, wantQueries) + } + if !reflect.DeepEqual(result, wantResult) { + t.Errorf("result: %+v, want %+v", result, wantResult) + } +} + func TestInsertLookupOwned(t *testing.T) { router, sbc, _, sbclookup := createRouterEnv() diff --git a/go/vt/vtgate/router_framework_test.go b/go/vt/vtgate/router_framework_test.go index 85eeb035de1..18b74015788 100644 --- a/go/vt/vtgate/router_framework_test.go +++ b/go/vt/vtgate/router_framework_test.go @@ -156,7 +156,8 @@ var unshardedVSchema = ` "column": "id", "sequence": "user_seq" } - } + }, + "simple": {} } } `