Skip to content

Commit 8fff0ea

Browse files
authored
test: make it easier to run tests without a main keyspace (#17501)
Signed-off-by: Andres Taylor <[email protected]>
1 parent 54dfd60 commit 8fff0ea

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

go/test/vschemawrapper/vschema_wrapper.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package vschemawrapper
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
23+
"sort"
2224
"strings"
2325

2426
"vitess.io/vitess/go/mysql/collations"
@@ -275,17 +277,6 @@ func (vw *VSchemaWrapper) FindTableOrVindex(tab sqlparser.TableName) (*vindexes.
275277
return vw.Vcursor.FindTableOrVindex(tab)
276278
}
277279

278-
func (vw *VSchemaWrapper) getfirstKeyspace() (ks *vindexes.Keyspace) {
279-
var f string
280-
for name, schema := range vw.V.Keyspaces {
281-
if f == "" || f > name {
282-
f = name
283-
ks = schema.Keyspace
284-
}
285-
}
286-
return
287-
}
288-
289280
func (vw *VSchemaWrapper) getActualKeyspace() string {
290281
if vw.Keyspace == nil {
291282
return ""
@@ -301,15 +292,32 @@ func (vw *VSchemaWrapper) getActualKeyspace() string {
301292
}
302293

303294
func (vw *VSchemaWrapper) SelectedKeyspace() (*vindexes.Keyspace, error) {
304-
return vw.V.Keyspaces["main"].Keyspace, nil
295+
return vw.AnyKeyspace()
305296
}
306297

307298
func (vw *VSchemaWrapper) AnyKeyspace() (*vindexes.Keyspace, error) {
308-
return vw.SelectedKeyspace()
299+
ks, found := vw.V.Keyspaces["main"]
300+
if found {
301+
return ks.Keyspace, nil
302+
}
303+
304+
size := len(vw.V.Keyspaces)
305+
if size == 0 {
306+
return nil, errors.New("no keyspace found in vschema")
307+
}
308+
309+
// Find the first keyspace in the map alphabetically to get deterministic results
310+
keys := make([]string, size)
311+
for key := range vw.V.Keyspaces {
312+
keys = append(keys, key)
313+
}
314+
sort.Strings(keys)
315+
316+
return vw.V.Keyspaces[keys[0]].Keyspace, nil
309317
}
310318

311319
func (vw *VSchemaWrapper) FirstSortedKeyspace() (*vindexes.Keyspace, error) {
312-
return vw.V.Keyspaces["main"].Keyspace, nil
320+
return vw.AnyKeyspace()
313321
}
314322

315323
func (vw *VSchemaWrapper) TargetString() string {
@@ -344,12 +352,12 @@ func (vw *VSchemaWrapper) IsViewsEnabled() bool {
344352

345353
// FindMirrorRule finds the mirror rule for the requested keyspace, table
346354
// name, and the tablet type in the VSchema.
347-
func (vs *VSchemaWrapper) FindMirrorRule(tab sqlparser.TableName) (*vindexes.MirrorRule, error) {
355+
func (vw *VSchemaWrapper) FindMirrorRule(tab sqlparser.TableName) (*vindexes.MirrorRule, error) {
348356
destKeyspace, destTabletType, _, err := topoproto.ParseDestination(tab.Qualifier.String(), topodatapb.TabletType_PRIMARY)
349357
if err != nil {
350358
return nil, err
351359
}
352-
mirrorRule, err := vs.V.FindMirrorRule(destKeyspace, tab.Name.String(), destTabletType)
360+
mirrorRule, err := vw.V.FindMirrorRule(destKeyspace, tab.Name.String(), destTabletType)
353361
if err != nil {
354362
return nil, err
355363
}

0 commit comments

Comments
 (0)