Skip to content

Commit

Permalink
do not use slices with 1.20
Browse files Browse the repository at this point in the history
Signed-off-by: 'Renan Rangel' <[email protected]>
  • Loading branch information
rvrangel committed Oct 24, 2024
1 parent 0edb426 commit 9b83c1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
15 changes: 12 additions & 3 deletions go/vt/mysqlctl/mysqlshellbackupengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"os"
"os/exec"
"path"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
35 changes: 35 additions & 0 deletions go/vt/mysqlctl/mysqlshellbackupengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}

0 comments on commit 9b83c1e

Please sign in to comment.