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(mongodb): use gomongo for Execute with mongosh fallback (#19128)
h3n4l5224818bbe4b
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex 5727665466..bdae176fa2 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -92,6 +92,25 @@ func (*Driver) GetDB() *sql.DB {// Execute executes a statement, always returns 0 as the number of rows affected because we execute the statement by mongosh, it's hard to catch the row effected number.func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOptions) (int64, error) {+ statement = strings.Trim(statement, " \t\n\r\f;")++ var gomongoErr error+ if d.client != nil {+ gmClient := gomongo.NewClient(d.client)+ _, gomongoErr = gmClient.Execute(ctx, d.databaseName, statement)+ if gomongoErr == nil {+ return 0, nil+ }+ }++ err := d.executeWithMongosh(ctx, statement)+ if err == nil && gomongoErr != nil {+ slog.Debug("executed statement with mongosh fallback", slog.String("statement", statement), log.BBError(gomongoErr))+ }+ return 0, err+}++func (d *Driver) executeWithMongosh(ctx context.Context, statement string) error {connectionURI := getBasicMongoDBConnectionURI(d.connCfg)// For MongoDB, we execute the statement in mongosh, which is a shell for MongoDB.// There are some ways to execute the statement in mongosh:@@ -129,7 +148,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOpti_ = os.Remove(caFileName)}()if err := os.WriteFile(caFileName, []byte(d.connCfg.DataSource.GetSslCa()), 0400); err != nil {- return 0, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ return errors.Wrap(err, "failed to write tlsCAFile to temporary file")}mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFileName)}@@ -142,16 +161,16 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOpti}()var sb strings.Builderif _, err := sb.WriteString(d.connCfg.DataSource.GetSslKey()); err != nil {- return 0, errors.Wrapf(err, "failed to write ssl key into string builder")+ return errors.Wrapf(err, "failed to write ssl key into string builder")}if _, err := sb.WriteString("\n"); err != nil {- return 0, errors.Wrapf(err, "failed to write new line into string builder")+ return errors.Wrapf(err, "failed to write new line into string builder")}if _, err := sb.WriteString(d.connCfg.DataSource.GetSslCert()); err != nil {- return 0, errors.Wrapf(err, "failed to write ssl cert into string builder")+ return errors.Wrapf(err, "failed to write ssl cert into string builder")}if err := os.WriteFile(clientCertName, []byte(sb.String()), 0400); err != nil {- return 0, errors.Wrap(err, "failed to write tlsCAFile to temporary file")+ return errors.Wrap(err, "failed to write tlsCAFile to temporary file")}mongoshArgs = append(mongoshArgs, "--tlsCertificateKeyFile", clientCertName)}@@ -161,14 +180,14 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOptitempDir := os.TempDir()tempFile, err := os.CreateTemp(tempDir, "mongodb-statement")if err != nil {- return 0, errors.Wrap(err, "failed to create temporary file")+ return errors.Wrap(err, "failed to create temporary file")}defer os.Remove(tempFile.Name())if _, err := tempFile.WriteString(statement); err != nil {- return 0, errors.Wrap(err, "failed to write statement to temporary file")+ return errors.Wrap(err, "failed to write statement to temporary file")}if err := tempFile.Close(); err != nil {- return 0, errors.Wrap(err, "failed to close temporary file")+ return errors.Wrap(err, "failed to close temporary file")}mongoshArgs = append(mongoshArgs, "--file", tempFile.Name())@@ -178,9 +197,9 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOptimongoshCmd.Stderr = &errContentmongoshCmd.Stdout = &outContentif err := mongoshCmd.Run(); err != nil {- return 0, errors.Wrapf(err, "failed to execute statement in mongosh: \n stdout: %s\n stderr: %s", outContent.String(), errContent.String())+ return errors.Wrapf(err, "failed to execute statement in mongosh: \n stdout: %s\n stderr: %s", outContent.String(), errContent.String())}- return 0, nil+ return nil}// Dump dumps the database.diff --git a/go.mod b/go.modindex 478c15c401..36ae1d3de8 100644--- a/go.mod+++ b/go.mod@@ -32,7 +32,7 @@ require (github.com/beltran/gohive/v2 v2.0.0github.com/blang/semver/v4 v4.0.0github.com/bmatcuk/doublestar/v4 v4.9.2- github.com/bytebase/gomongo v0.0.0-20260122102630-d4856f4d6ca0+ github.com/bytebase/gomongo v0.0.0-20260123064557-5e3ae906bb2cgithub.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0github.com/caarlos0/env/v11 v11.3.1github.com/cenkalti/backoff/v5 v5.0.3@@ -267,10 +267,10 @@ require (go.yaml.in/yaml/v2 v2.4.3 // indirectgo.yaml.in/yaml/v3 v3.0.4 // indirectgo.yaml.in/yaml/v4 v4.0.0-rc.3 // indirect- golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39 // indirect- golang.org/x/mod v0.31.0 // indirect+ golang.org/x/exp v0.0.0-20260112195511-716be5621a96 // indirect+ golang.org/x/mod v0.32.0 // indirectgolang.org/x/sys v0.40.0 // indirect- golang.org/x/telemetry v0.0.0-20251203150158-8fff8a5912fc // indirect+ golang.org/x/telemetry v0.0.0-20260109210033-bd525da824e2 // indirectgolang.org/x/term v0.39.0 // indirectgonum.org/v1/gonum v0.16.0 // indirectgopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect@@ -330,7 +330,7 @@ require (github.com/jackc/pgio v1.0.0 // indirectgithub.com/jackc/pgpassfile v1.0.0 // indirectgithub.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect- github.com/klauspost/compress v1.18.1 // indirect+ github.com/klauspost/compress v1.18.3 // indirectgithub.com/labstack/echo/v5 v5.0.0github.com/lestrrat-go/blackmagic v1.0.4 // indirectgithub.com/lestrrat-go/httpcc v1.0.1 // indirect@@ -361,7 +361,7 @@ require (github.com/tikv/client-go/v2 v2.0.8-0.20251028065425-b7d4dfd8520e // indirectgithub.com/tikv/pd/client v0.0.0-20250703091733-dfd345b89500 // indirectgithub.com/xdg-go/pbkdf2 v1.0.0 // indirect- github.com/xdg-go/scram v1.1.2 // indirect+ github.com/xdg-go/scram v1.2.0 // indirectgithub.com/xdg-go/stringprep v1.0.4 // indirectgithub.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78github.com/yusufpapurcu/wmi v1.2.4 // indirect@@ -372,7 +372,7 @@ require (golang.org/x/net v0.49.0golang.org/x/sync v0.19.0golang.org/x/time v0.14.0 // indirect- golang.org/x/tools v0.40.0 // indirect+ golang.org/x/tools v0.41.0 // indirectgolang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirectgopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect)diff --git a/go.sum b/go.sumindex 5e65924468..8d7d990648 100644--- a/go.sum+++ b/go.sum@@ -857,8 +857,8 @@ github.com/bytebase/gh-ost2 v1.1.7-0.20251002210738-35e5dddaad7c h1:scJ4ycKuUAaAgithub.com/bytebase/gh-ost2 v1.1.7-0.20251002210738-35e5dddaad7c/go.mod h1:X0CGYwIZUnjfrv02/nBlftxqSBMm4mYJzFwiyn2D4d8=github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898 h1:0ggqA9XL4yvtePPvRDRYMed3jtdNk4s0o8AMW8uJPSA=github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898/go.mod h1:2KhUz4GSLcdJGXSJMXAf2BjmakcgoOkNyPpHdGAxRpY=-github.com/bytebase/gomongo v0.0.0-20260122102630-d4856f4d6ca0 h1:jUZGxDJQz1M9e12Lgh6bzFlruSHt7CCDV8Exwg+xIPY=-github.com/bytebase/gomongo v0.0.0-20260122102630-d4856f4d6ca0/go.mod h1:7aVbtrEx9OpYH+qoeyqdxAVXH5NsBJSbTScD2jt+B0o=+github.com/bytebase/gomongo v0.0.0-20260123064557-5e3ae906bb2c h1:e1yWka4+s62d7rInxzkJZXmeu5ntH66TqLilqOuqqUE=+github.com/bytebase/gomongo v0.0.0-20260123064557-5e3ae906bb2c/go.mod h1:tmnMdRvFw6R4jYLWnPCtGMsCh2McnCXBLW4ZonyShsE=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/parser v0.0.0-20260121030202-698704919f24 h1:oonTO26orUa4bYk/hQjALiYO1zII+Kzpjg75OnC3VtU=@@ -1416,8 +1416,8 @@ github.com/klauspost/asmfmt v1.3.2 h1:4Ri7ox3EwapiOjCki+hw14RyKk201CN4rzyCJRFLpKgithub.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=-github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=-github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0=+github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=+github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=@@ -1815,8 +1815,8 @@ github.com/wI2L/jsondiff v0.7.0/go.mod h1:KAEIojdQq66oJiHhDyQez2x+sRit0vIzC9KeK0github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=-github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=-github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=+github.com/xdg-go/scram v1.2.0 h1:bYKF2AEwG5rqd1BumT4gAnvwU/M9nBp2pTSxeZw7Wvs=+github.com/xdg-go/scram v1.2.0/go.mod h1:3dlrS0iBaWKYVt2ZfA4cj48umJZ+cAEbR6/SjLA88I8=github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=@@ -1975,8 +1975,8 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=golang.org/x/exp v0.0.0-20220827204233-334a2380cb91/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=-golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39 h1:DHNhtq3sNNzrvduZZIiFyXWOL9IWaDPHqTnLJp+rCBY=-golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39/go.mod h1:46edojNIoXTNOhySWIWdix628clX9ODXwPsQuG6hsK0=+golang.org/x/exp v0.0.0-20260112195511-716be5621a96 h1:Z/6YuSHTLOHfNFdb8zVZomZr7cqNgTJvA8+Qz75D8gU=+golang.org/x/exp v0.0.0-20260112195511-716be5621a96/go.mod h1:nzimsREAkjBCIEFtHiYkrJyT+2uy9YZJB7H1k68CXZU=golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=@@ -2021,8 +2021,8 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=-golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=-golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=+golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=+golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=@@ -2237,8 +2237,8 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=-golang.org/x/telemetry v0.0.0-20251203150158-8fff8a5912fc h1:bH6xUXay0AIFMElXG2rQ4uiE+7ncwtiOdPfYK1NK2XA=-golang.org/x/telemetry v0.0.0-20251203150158-8fff8a5912fc/go.mod h1:hKdjCMrbv9skySur+Nek8Hd0uJ0GuxJIoIX2payrIdQ=+golang.org/x/telemetry v0.0.0-20260109210033-bd525da824e2 h1:O1cMQHRfwNpDfDJerqRoE2oD+AFlyid87D40L/OkkJo=+golang.org/x/telemetry v0.0.0-20260109210033-bd525da824e2/go.mod h1:b7fPSJ0pKZ3ccUh8gnTONJxhn3c/PS6tyzQvyqw4iA8=golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=@@ -2352,8 +2352,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbcgolang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=-golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=-golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=+golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=+golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=