Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(mariadb): carve out Diagnose to omni/mariadb parser (#20632)
Vincent Huang4fdb3ec79c32
diff --git a/backend/component/sheet/sheet.go b/backend/component/sheet/sheet.goindex 700cca8b15..e77de9d303 100644--- a/backend/component/sheet/sheet.go+++ b/backend/component/sheet/sheet.go@@ -13,6 +13,7 @@ import (// Import parsers to register their parse functions._ "github.com/bytebase/bytebase/backend/plugin/parser/cockroachdb"_ "github.com/bytebase/bytebase/backend/plugin/parser/doris"+ _ "github.com/bytebase/bytebase/backend/plugin/parser/mariadb"_ "github.com/bytebase/bytebase/backend/plugin/parser/mysql"_ "github.com/bytebase/bytebase/backend/plugin/parser/partiql"_ "github.com/bytebase/bytebase/backend/plugin/parser/plsql"diff --git a/backend/plugin/parser/mariadb/diagnose.go b/backend/plugin/parser/mariadb/diagnose.gonew file mode 100644index 0000000000..efc7a82cf2--- /dev/null+++ b/backend/plugin/parser/mariadb/diagnose.go@@ -0,0 +1,56 @@+package mariadb++import (+ "context"+ "errors"+ "strings"+ "unicode"++ mariadbomniparser "github.com/bytebase/omni/mariadb/parser"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++func init() {+ base.RegisterDiagnoseFunc(storepb.Engine_MARIADB, Diagnose)+}++func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {+ diagnostics := make([]base.Diagnostic, 0)+ syntaxError := parseMariaDBStatement(statement)+ if syntaxError != nil {+ diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError, statement))+ }++ return diagnostics, nil+}++func parseMariaDBStatement(statement string) *base.SyntaxError {+ trimmedStatement := strings.TrimRightFunc(statement, unicode.IsSpace)+ if len(trimmedStatement) > 0 && !strings.HasSuffix(trimmedStatement, ";") {+ // Add a semicolon to the end of the statement to allow users to omit the semicolon+ // for the last statement in the script.+ statement += ";"+ }++ _, err := ParseMariaDBOmni(statement)+ if err == nil {+ return nil+ }++ var parseErr *mariadbomniparser.ParseError+ if !errors.As(err, &parseErr) {+ return &base.SyntaxError{+ Position: &storepb.Position{Line: 1, Column: 1},+ Message: err.Error(),+ }+ }++ pos := ByteOffsetToRunePosition(statement, parseErr.Position)+ return &base.SyntaxError{+ Position: pos,+ Message: parseErr.Message,+ RawMessage: parseErr.Message,+ }+}diff --git a/backend/plugin/parser/mariadb/diagnose_e2e_test.go b/backend/plugin/parser/mariadb/diagnose_e2e_test.gonew file mode 100644index 0000000000..acd73f0ca3--- /dev/null+++ b/backend/plugin/parser/mariadb/diagnose_e2e_test.go@@ -0,0 +1,48 @@+package mariadb++import (+ "context"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+)++// mariaDBOnlySQL are statements valid in MariaDB but rejected by the omni/mysql+// parser that backed Engine_MARIADB before this carve-out: SEQUENCE objects,+// NEXT VALUE FOR, and RETURNING on INSERT/REPLACE/DELETE.+var mariaDBOnlySQL = []string{+ "CREATE SEQUENCE s",+ "INSERT INTO t (id) VALUES (1) RETURNING id",+ "REPLACE INTO t (id) VALUES (1) RETURNING id",+ "DELETE FROM t RETURNING id",+ "SELECT NEXT VALUE FOR s",+}++// TestMariaDBDiagnoseKeystone is the carve-out keystone. Before the re-point,+// Engine_MARIADB Diagnose ran through omni/mysql and reported a *false* syntax+// error on MariaDB-only SQL. After re-pointing to omni/mariadb it parses cleanly.+// The test contrasts both backends so the green state can't be accidental: the+// mysql backend must still flag these (the bug), the mariadb backend must not.+func TestMariaDBDiagnoseKeystone(t *testing.T) {+ ctx := context.Background()+ for _, stmt := range mariaDBOnlySQL {+ // RED baseline: the old omni/mysql backend reports a false syntax error.+ mysqlDiags, err := mysql.Diagnose(ctx, base.DiagnoseContext{}, stmt)+ require.NoError(t, err)+ require.NotEmpty(t, mysqlDiags, "omni/mysql should still (wrongly) flag %q — the false error being fixed", stmt)++ // GREEN: the omni/mariadb backend accepts it (no diagnostics).+ mariadbDiags, err := Diagnose(ctx, base.DiagnoseContext{}, stmt)+ require.NoError(t, err)+ require.Empty(t, mariadbDiags, "omni/mariadb should accept %q with no diagnostic", stmt)+ }++ // Diagnose must still report a genuine syntax error — proving it actually+ // validates, not just "returns empty".+ badDiags, err := Diagnose(ctx, base.DiagnoseContext{}, "SELECT FROM WHERE )(")+ require.NoError(t, err)+ require.NotEmpty(t, badDiags, "a real syntax error must still be reported")+}diff --git a/backend/plugin/parser/mariadb/omni.go b/backend/plugin/parser/mariadb/omni.gonew file mode 100644index 0000000000..40aaa956d4--- /dev/null+++ b/backend/plugin/parser/mariadb/omni.go@@ -0,0 +1,42 @@+package mariadb++import (+ "unicode/utf8"++ "github.com/bytebase/omni/mariadb/ast"+ mariadbparser "github.com/bytebase/omni/mariadb/parser"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++// ParseMariaDBOmni parses SQL using omni's MariaDB parser and returns an ast.List.+func ParseMariaDBOmni(sql string) (*ast.List, error) {+ return mariadbparser.Parse(sql)+}++// ByteOffsetToRunePosition converts a byte offset in sql to a 1-based line:column+// where column is measured in Unicode code points (runes), matching storepb.Position semantics.+func ByteOffsetToRunePosition(sql string, byteOffset int) *storepb.Position {+ if byteOffset > len(sql) {+ byteOffset = len(sql)+ }++ line := int32(1)+ runeCol := int32(0) // 0-based rune count on current line+ i := 0+ for i < byteOffset {+ r, size := utf8.DecodeRuneInString(sql[i:])+ if r == '\n' {+ line+++ runeCol = 0+ } else {+ runeCol+++ }+ i += size+ }++ return &storepb.Position{+ Line: line,+ Column: runeCol + 1, // convert to 1-based+ }+}diff --git a/backend/plugin/parser/mysql/diagnose.go b/backend/plugin/parser/mysql/diagnose.goindex a10d33f0c3..e3c791cefe 100644--- a/backend/plugin/parser/mysql/diagnose.go+++ b/backend/plugin/parser/mysql/diagnose.go@@ -14,7 +14,6 @@ import (func init() {base.RegisterDiagnoseFunc(storepb.Engine_MYSQL, Diagnose)- base.RegisterDiagnoseFunc(storepb.Engine_MARIADB, Diagnose)base.RegisterDiagnoseFunc(storepb.Engine_OCEANBASE, Diagnose)base.RegisterDiagnoseFunc(storepb.Engine_CLICKHOUSE, Diagnose)}diff --git a/backend/server/ultimate.go b/backend/server/ultimate.goindex 5c110e1449..f050a2ab42 100644--- a/backend/server/ultimate.go+++ b/backend/server/ultimate.go@@ -31,6 +31,7 @@ import (_ "github.com/bytebase/bytebase/backend/plugin/parser/cosmosdb"_ "github.com/bytebase/bytebase/backend/plugin/parser/doris"_ "github.com/bytebase/bytebase/backend/plugin/parser/elasticsearch"+ _ "github.com/bytebase/bytebase/backend/plugin/parser/mariadb"_ "github.com/bytebase/bytebase/backend/plugin/parser/mongodb"_ "github.com/bytebase/bytebase/backend/plugin/parser/mysql"_ "github.com/bytebase/bytebase/backend/plugin/parser/partiql"diff --git a/go.mod b/go.modindex e57461d419..d174183a8d 100644--- a/go.mod+++ b/go.mod@@ -34,7 +34,7 @@ require (github.com/bmatcuk/doublestar/v4 v4.10.0github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0- github.com/bytebase/omni v0.0.0-20260618025236-cf4c9b844d52+ github.com/bytebase/omni v0.0.0-20260618070256-8f57f04f5aa2github.com/caarlos0/env/v11 v11.4.1github.com/cenkalti/backoff/v5 v5.0.3github.com/cockroachdb/cockroachdb-parser v0.25.2diff --git a/go.sum b/go.sumindex 9093270922..497ce50b5d 100644--- a/go.sum+++ b/go.sum@@ -250,8 +250,8 @@ github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352 h1:3sC5pdvJ7bNAhRgithub.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352/go.mod h1:eyWrxE51HCbjGIChvty/mYaNcAroXshnIiR6SIqlxfY=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=-github.com/bytebase/omni v0.0.0-20260618025236-cf4c9b844d52 h1:4P+GLe/f5JJOU2wwDlGoWI5ve64OyQcTqnHGyqgbhKM=-github.com/bytebase/omni v0.0.0-20260618025236-cf4c9b844d52/go.mod h1:Gp5RN3FM07f9/FFOTZCeu8duTAIMeuinkjjBCrs2Dw4=+github.com/bytebase/omni v0.0.0-20260618070256-8f57f04f5aa2 h1:DHoqAOglW5+OyeKujkZamgwKidFMSiQANICOofJ0bWA=+github.com/bytebase/omni v0.0.0-20260618070256-8f57f04f5aa2/go.mod h1:Gp5RN3FM07f9/FFOTZCeu8duTAIMeuinkjjBCrs2Dw4=github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640 h1:g4Jed1/Jv/DHBiQmEgN5ILOQDDBjFlG4N4Lp+3B4aYE=github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=@@ -954,6 +954,8 @@ github.com/stripe/stripe-go/v85 v85.1.0 h1:MywWUmgw9M7cnwFFch9cyLmRZuAiYM5cZ27Jxgithub.com/stripe/stripe-go/v85 v85.1.0/go.mod h1:5P+HGFenpWgak27T5Is6JMsmDfUC1yJnjhhmquz7kXw=github.com/testcontainers/testcontainers-go v0.42.0 h1:He3IhTzTZOygSXLJPMX7n44XtK+qhjat1nI9cneBbUY=github.com/testcontainers/testcontainers-go v0.42.0/go.mod h1:vZjdY1YmUA1qEForxOIOazfsrdyORJAbhi0bp8plN30=+github.com/testcontainers/testcontainers-go/modules/mariadb v0.41.0 h1:t0J/TOgzTKCO+7Pc7wE6ITJXGCZFufisuokOWYVzNaQ=+github.com/testcontainers/testcontainers-go/modules/mariadb v0.41.0/go.mod h1:ELA34/aGm5oXG0g9OkPVWmIsw+BDdeybr7wk8i+TQCg=github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0 h1:z/1qHeliTLDKNaJ7uOHOx1FjwghbcbYfga4dTFkF0hU=github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0/go.mod h1:GaunAWwMXLtsMKG3xn2HYIBDbKddGArfcGsF2Aog81E=github.com/testcontainers/testcontainers-go/modules/mssql v0.41.0 h1:YAuSa+IMTn0nA6iNvSAFgJEeJsQWSghFWu0ubebWXJI=