From 733fc1cc0e42a3596208a38a87a8f4f95ab0d9c0 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Thu, 9 Jan 2025 20:24:41 +0100 Subject: [PATCH 1/2] test: make it easier to run without a main keyspace Signed-off-by: Andres Taylor --- go/test/vschemawrapper/vschema_wrapper.go | 31 +++++++++++------------ 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/go/test/vschemawrapper/vschema_wrapper.go b/go/test/vschemawrapper/vschema_wrapper.go index 3f9f072afc6..8c072d01e0e 100644 --- a/go/test/vschemawrapper/vschema_wrapper.go +++ b/go/test/vschemawrapper/vschema_wrapper.go @@ -18,6 +18,7 @@ package vschemawrapper import ( "context" + "errors" "fmt" "strings" @@ -275,17 +276,6 @@ func (vw *VSchemaWrapper) FindTableOrVindex(tab sqlparser.TableName) (*vindexes. return vw.Vcursor.FindTableOrVindex(tab) } -func (vw *VSchemaWrapper) getfirstKeyspace() (ks *vindexes.Keyspace) { - var f string - for name, schema := range vw.V.Keyspaces { - if f == "" || f > name { - f = name - ks = schema.Keyspace - } - } - return -} - func (vw *VSchemaWrapper) getActualKeyspace() string { if vw.Keyspace == nil { return "" @@ -301,15 +291,24 @@ func (vw *VSchemaWrapper) getActualKeyspace() string { } func (vw *VSchemaWrapper) SelectedKeyspace() (*vindexes.Keyspace, error) { - return vw.V.Keyspaces["main"].Keyspace, nil + return vw.AnyKeyspace() } func (vw *VSchemaWrapper) AnyKeyspace() (*vindexes.Keyspace, error) { - return vw.SelectedKeyspace() + ks, found := vw.V.Keyspaces["main"] + if found { + return ks.Keyspace, nil + } + + for _, ks := range vw.V.Keyspaces { + return ks.Keyspace, nil + } + + return nil, errors.New("no keyspace found in vschema") } func (vw *VSchemaWrapper) FirstSortedKeyspace() (*vindexes.Keyspace, error) { - return vw.V.Keyspaces["main"].Keyspace, nil + return vw.AnyKeyspace() } func (vw *VSchemaWrapper) TargetString() string { @@ -344,12 +343,12 @@ func (vw *VSchemaWrapper) IsViewsEnabled() bool { // FindMirrorRule finds the mirror rule for the requested keyspace, table // name, and the tablet type in the VSchema. -func (vs *VSchemaWrapper) FindMirrorRule(tab sqlparser.TableName) (*vindexes.MirrorRule, error) { +func (vw *VSchemaWrapper) FindMirrorRule(tab sqlparser.TableName) (*vindexes.MirrorRule, error) { destKeyspace, destTabletType, _, err := topoproto.ParseDestination(tab.Qualifier.String(), topodatapb.TabletType_PRIMARY) if err != nil { return nil, err } - mirrorRule, err := vs.V.FindMirrorRule(destKeyspace, tab.Name.String(), destTabletType) + mirrorRule, err := vw.V.FindMirrorRule(destKeyspace, tab.Name.String(), destTabletType) if err != nil { return nil, err } From 66ba4bcf959df66841f67254f3399991c2ed5216 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Fri, 10 Jan 2025 07:27:22 +0100 Subject: [PATCH 2/2] sorting for deterministic Signed-off-by: Andres Taylor --- go/test/vschemawrapper/vschema_wrapper.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/go/test/vschemawrapper/vschema_wrapper.go b/go/test/vschemawrapper/vschema_wrapper.go index 8c072d01e0e..27219c6a255 100644 --- a/go/test/vschemawrapper/vschema_wrapper.go +++ b/go/test/vschemawrapper/vschema_wrapper.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "sort" "strings" "vitess.io/vitess/go/mysql/collations" @@ -300,11 +301,19 @@ func (vw *VSchemaWrapper) AnyKeyspace() (*vindexes.Keyspace, error) { return ks.Keyspace, nil } - for _, ks := range vw.V.Keyspaces { - return ks.Keyspace, nil + size := len(vw.V.Keyspaces) + if size == 0 { + return nil, errors.New("no keyspace found in vschema") + } + + // Find the first keyspace in the map alphabetically to get deterministic results + keys := make([]string, size) + for key := range vw.V.Keyspaces { + keys = append(keys, key) } + sort.Strings(keys) - return nil, errors.New("no keyspace found in vschema") + return vw.V.Keyspaces[keys[0]].Keyspace, nil } func (vw *VSchemaWrapper) FirstSortedKeyspace() (*vindexes.Keyspace, error) {