Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: mysql parse version (#9810)

p0ny5a3ce919cef3
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.go
index 8df9f0cd94..90fe0eae06 100644
--- a/backend/plugin/db/mysql/mysql.go
+++ b/backend/plugin/db/mysql/mysql.go
@@ -7,6 +7,7 @@ import (
"fmt"
"log/slog"
"net"
+ "regexp"
"strings"
"time"
@@ -153,11 +154,15 @@ func (driver *Driver) getVersion(ctx context.Context) (string, string, error) {
}
return "", "", util.FormatErrorWithQuery(err, query)
}
- pos := strings.Index(version, "-")
- if pos == -1 {
- return version, "", nil
+
+ return parseVersion(version)
+}
+
+func parseVersion(version string) (string, string, error) {
+ if loc := regexp.MustCompile(`^\d+.\d+.\d+`).FindStringIndex(version); loc != nil {
+ return version[loc[0]:loc[1]], version[loc[1]:], nil
}
- return version[:pos], version[pos:], nil
+ return "", "", errors.Errorf("failed to parse version %q", version)
}
// Execute executes a SQL statement.
diff --git a/backend/plugin/db/mysql/mysql_test.go b/backend/plugin/db/mysql/mysql_test.go
new file mode 100644
index 0000000000..baf819f426
--- /dev/null
+++ b/backend/plugin/db/mysql/mysql_test.go
@@ -0,0 +1,44 @@
+package mysql
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestParseVersion(t *testing.T) {
+ tests := []struct {
+ version string
+ want string
+ wantRest string
+ }{
+ {
+ version: "8.0.27",
+ want: "8.0.27",
+ wantRest: "",
+ },
+ {
+ version: "5.7.22-log",
+ want: "5.7.22",
+ wantRest: "-log",
+ },
+ {
+ version: "5.6.29_ddm_3.0.1.7",
+ want: "5.6.29",
+ wantRest: "_ddm_3.0.1.7",
+ },
+ {
+ version: "10.4.7-MariaDB",
+ want: "10.4.7",
+ wantRest: "-MariaDB",
+ },
+ }
+
+ a := require.New(t)
+ for _, tc := range tests {
+ version, rest, err := parseVersion(tc.version)
+ a.NoError(err)
+ a.Equal(tc.want, version)
+ a.Equal(tc.wantRest, rest)
+ }
+}