Skip to content

Commit

Permalink
fix for vitessio#2804
Browse files Browse the repository at this point in the history
The change preserves the insert id coming from mysql autoinc
for unsharded tables.
  • Loading branch information
sougou authored and michael-berlin committed May 22, 2017
1 parent 6c06e70 commit 69387e3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
6 changes: 4 additions & 2 deletions go/vt/vtgate/engine/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -478,7 +481,6 @@ func (route *Route) execInsertSharded(vcursor VCursor, queryConstruct *queryinfo
if insertid != 0 {
result.InsertID = uint64(insertid)
}

return result, nil
}

Expand Down
60 changes: 60 additions & 0 deletions go/vt/vtgate/router_dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion go/vt/vtgate/router_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ var unshardedVSchema = `
"column": "id",
"sequence": "user_seq"
}
}
},
"simple": {}
}
}
`
Expand Down

0 comments on commit 69387e3

Please sign in to comment.