From 9b83c1e233138132f781459b2f8567eda985d51d Mon Sep 17 00:00:00 2001 From: Renan Rangel Date: Thu, 24 Oct 2024 04:54:44 -0700 Subject: [PATCH] do not use slices with 1.20 Signed-off-by: 'Renan Rangel' --- go/vt/mysqlctl/mysqlshellbackupengine.go | 15 ++++++-- go/vt/mysqlctl/mysqlshellbackupengine_test.go | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/go/vt/mysqlctl/mysqlshellbackupengine.go b/go/vt/mysqlctl/mysqlshellbackupengine.go index ad2b2a7d84d..b07caa6da54 100644 --- a/go/vt/mysqlctl/mysqlshellbackupengine.go +++ b/go/vt/mysqlctl/mysqlshellbackupengine.go @@ -26,7 +26,6 @@ import ( "os" "os/exec" "path" - "slices" "strings" "sync" "time" @@ -513,7 +512,7 @@ func cleanupMySQL(ctx context.Context, params RestoreParams, shouldDeleteUsers b // drop all databases for _, row := range result.Rows { dbName := row[0].ToString() - if slices.Contains(internalDBs, dbName) { + if sliceContains(internalDBs, dbName) { continue // not dropping internal DBs } @@ -548,7 +547,7 @@ func cleanupMySQL(ctx context.Context, params RestoreParams, shouldDeleteUsers b if user == currentUser { continue // we don't drop the current user } - if slices.Contains(reservedUsers, user) { + if sliceContains(reservedUsers, user) { continue // we skip reserved MySQL users } @@ -562,3 +561,13 @@ func cleanupMySQL(ctx context.Context, params RestoreParams, shouldDeleteUsers b return err } + +func sliceContains[S ~[]E, E comparable](s S, v E) bool { + for _, item := range s { + if item == v { + return true + } + } + + return false +} diff --git a/go/vt/mysqlctl/mysqlshellbackupengine_test.go b/go/vt/mysqlctl/mysqlshellbackupengine_test.go index 5400491c2fc..ceacab49d60 100644 --- a/go/vt/mysqlctl/mysqlshellbackupengine_test.go +++ b/go/vt/mysqlctl/mysqlshellbackupengine_test.go @@ -396,3 +396,38 @@ func TestMySQLShellBackupEngine_ExecuteBackup_ReleaseLock(t *testing.T) { }) } + +func Test_sliceContains(t *testing.T) { + tests := []struct { + slice []any + value any + want bool + }{ + { + []any{"apple", "banana", "cherry"}, + "apple", + true, + }, + { + []any{"apple", "banana", "cherry"}, + "banana", + true, + }, + { + []any{"apple", "banana", "cherry"}, + "cherry", + true, + }, + { + []any{"apple", "banana", "cherry"}, + "dragonfruit", + false, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { + assert.Equal(t, tt.want, sliceContains(tt.slice, tt.value)) + }) + } +}