Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go/vt/vtgate/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (vc *vcursorImpl) Session() engine.SessionActions {
}

func (vc *vcursorImpl) SetTarget(target string) error {
keyspace, tabletType, _, err := parseDestinationTarget(target, vc.vschema)
keyspace, tabletType, _, err := topoprotopb.ParseDestination(target, defaultTabletType)
if err != nil {
return err
}
Expand Down
67 changes: 67 additions & 0 deletions go/vt/vtgate/vcursor_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,70 @@ func TestDestinationKeyspace(t *testing.T) {
}
}
}

func TestSetTarget(t *testing.T) {
ks1 := &vindexes.Keyspace{
Name: "ks1",
Sharded: false,
}
ks1Schema := &vindexes.KeyspaceSchema{
Keyspace: ks1,
Tables: nil,
Vindexes: nil,
Error: nil,
}
ks2 := &vindexes.Keyspace{
Name: "ks2",
Sharded: false,
}
ks2Schema := &vindexes.KeyspaceSchema{
Keyspace: ks2,
Tables: nil,
Vindexes: nil,
Error: nil,
}
vschemaWith2KS := &vindexes.VSchema{
Keyspaces: map[string]*vindexes.KeyspaceSchema{
ks1.Name: ks1Schema,
ks2.Name: ks2Schema,
}}

type testCase struct {
vschema *vindexes.VSchema
targetString string
expectedError string
}

tests := []testCase{{
vschema: vschemaWith2KS,
targetString: "",
}, {
vschema: vschemaWith2KS,
targetString: "ks1",
}, {
vschema: vschemaWith2KS,
targetString: "ks2",
}, {
vschema: vschemaWith2KS,
targetString: "ks3",
expectedError: "invalid keyspace provided: ks3",
}, {
vschema: vschemaWith2KS,
targetString: "ks2@replica",
expectedError: "cannot change to a non-master type in the middle of a transaction: REPLICA",
}}

for i, tc := range tests {
t.Run(string(i)+"#"+tc.targetString, func(t *testing.T) {
vc, _ := newVCursorImpl(context.Background(), NewSafeSession(&vtgatepb.Session{InTransaction: true}), sqlparser.MarginComments{}, nil, nil, &fakeVSchemaOperator{vschema: tc.vschema}, nil)
vc.vschema = tc.vschema
err := vc.SetTarget(tc.targetString)
if tc.expectedError == "" {
require.NoError(t, err)
require.Equal(t, vc.safeSession.TargetString, tc.targetString)
} else {
require.EqualError(t, err, tc.expectedError)
}
})
}
}