Skip to content

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

feat(mongodb): bump gomongo + omni and remove mongosh fallback (#20281)

h3n4la3fe204f4323
diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.yml
index af8f074696..2d17436c43 100644
--- a/.github/workflows/backend-tests.yml
+++ b/.github/workflows/backend-tests.yml
@@ -28,56 +28,6 @@ jobs:
with:
go-version: '1.26.3'
cache: false # Caching is slow.
- # TODO: Remove this step after migrating MongoDB driver to use Go driver API instead of shelling out to mongosh
- # Currently required because MongoDB driver executes queries by calling mongosh CLI (see backend/plugin/db/mongodb/mongodb.go)
- - name: Install mongosh (Linux only)
- run: |
- if command -v mongosh &> /dev/null; then
- echo "mongosh is already installed at $(which mongosh)"
- mongosh --version
- elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
- echo "Installing mongosh v2.5.0 for Linux..."
- MONGOSH_VERSION="2.5.0"
- ARCH=$(uname -m)
-
- if [[ "$ARCH" == "x86_64" ]]; then
- ARCH="x64"
- elif [[ "$ARCH" == "aarch64" ]]; then
- ARCH="arm64"
- fi
-
- ARCHIVE="mongosh-${MONGOSH_VERSION}-linux-${ARCH}.tgz"
- DOWNLOAD_URL="https://downloads.mongodb.com/compass/${ARCHIVE}"
-
- # Use runner temp directory
- WORK_DIR="${RUNNER_TEMP:-$HOME}"
- echo "Working directory: $WORK_DIR"
- cd "$WORK_DIR"
-
- echo "Downloading from ${DOWNLOAD_URL}"
- curl -fsSL "${DOWNLOAD_URL}" -o "${ARCHIVE}"
- echo "Download complete, extracting..."
- tar -xzf "${ARCHIVE}"
-
- # Install to ~/.local/bin (no sudo required)
- mkdir -p "$HOME/.local/bin"
- EXTRACTED_DIR="mongosh-${MONGOSH_VERSION}-linux-${ARCH}"
- cp "${EXTRACTED_DIR}/bin/mongosh" "$HOME/.local/bin/mongosh"
- chmod +x "$HOME/.local/bin/mongosh"
-
- # Add to PATH for subsequent steps
- echo "$HOME/.local/bin" >> $GITHUB_PATH
-
- # Cleanup
- rm -rf "${EXTRACTED_DIR}" "${ARCHIVE}"
-
- # Verify installation
- "$HOME/.local/bin/mongosh" --version
- echo "mongosh v${MONGOSH_VERSION} installed successfully to $HOME/.local/bin"
- else
- echo "Non-Linux OS detected. Skipping mongosh installation."
- echo "Please ensure mongosh is installed manually on macOS runners."
- fi
- name: Verify go.mod is tidy
run: |
go mod tidy
diff --git a/backend/api/v1/rollout_service_task.go b/backend/api/v1/rollout_service_task.go
index 6e0e4eed6a..a8b15f12c0 100644
--- a/backend/api/v1/rollout_service_task.go
+++ b/backend/api/v1/rollout_service_task.go
@@ -418,9 +418,9 @@ func getCreateDatabaseStatement(dbType storepb.Engine, c *storepb.PlanConfig_Cre
// This is a fake CREATE DATABASE and USE statement since a single SQLite file represents a database. Engine driver will recognize it and establish a connection to create the sqlite file representing the database.
return fmt.Sprintf("CREATE DATABASE '%s';", databaseName), nil
case storepb.Engine_MONGODB:
- // We just run createCollection in mongosh instead of execute `use <database>` first, because we execute the
- // mongodb statement in mongosh with --file flag, and it doesn't support `use <database>` statement in the file.
- // And we pass the database name to Bytebase engine driver, which will be used to build the connection string.
+ // MongoDB has no top-level CREATE DATABASE — a database is implicitly created
+ // when its first collection is. We pass the database name to the engine driver
+ // (which uses it to build the connection URI) and emit createCollection.
return fmt.Sprintf(`db.createCollection("%s");`, c.Table), nil
case storepb.Engine_SPANNER:
return fmt.Sprintf("CREATE DATABASE %s;", databaseName), nil
diff --git a/backend/component/telemetry/reporter.go b/backend/component/telemetry/reporter.go
deleted file mode 100644
index 991dce4f5a..0000000000
--- a/backend/component/telemetry/reporter.go
+++ /dev/null
@@ -1,173 +0,0 @@
-// Package telemetry provides telemetry reporting to hub.bytebase.com.
-package telemetry
-
-import (
- "bytes"
- "context"
- "crypto/sha256"
- "encoding/hex"
- "encoding/json"
- "net/http"
- "sync"
- "time"
-
- lru "github.com/hashicorp/golang-lru/v2"
-
- "github.com/bytebase/bytebase/backend/common"
- "github.com/bytebase/bytebase/backend/store"
-)
-
-const (
- hubEventURL = "https://hub.bytebase.com/v1/events"
- maxStatementLength = 1000
- cacheCapacity = 100
-)
-
-// Reporter sends telemetry events to hub.bytebase.com with deduplication.
-type Reporter struct {
- mu sync.RWMutex
- version string
- gitCommit string
- enabled bool
-
- // LRU cache for deduplication: tracks reported statement hashes
- cache *lru.Cache[string, struct{}]
-
- httpClient *http.Client
-}
-
-var (
- globalReporter *Reporter
- globalReporterOnce sync.Once
-)
-
-// InitGlobalReporter initializes the global telemetry reporter.
-// The workspace ID is not needed at init time — it's resolved from request context
-// when events are reported, so this works in both single-workspace and SaaS modes.
-func InitGlobalReporter(version, gitCommit string, enabled bool) {
- globalReporterOnce.Do(func() {
- cache, _ := lru.New[string, struct{}](cacheCapacity)
- globalReporter = &Reporter{
- version: version,
- gitCommit: gitCommit,
- enabled: enabled,
- cache: cache,
- httpClient: &http.Client{
- Timeout: 10 * time.Second,
- },
- }
- })
-}
-
-// gomongoFallbackPayload is the JSON payload for gomongo fallback events.
-type gomongoFallbackPayload struct {
- WorkspaceID string `json:"workspaceId"`
- Email string `json:"email"`
- Version string `json:"version"`
- Commit string `json:"commit"`
- GomongoFallback struct {
- Statement string `json:"statement"`
- ErrorMessage string `json:"errorMessage"`
- } `json:"gomongoFallback"`
-}
-
-// ReportGomongoFallback reports a gomongo fallback event.
-// It deduplicates based on statement hash using an LRU cache.
-// Only reports in release versions (non-development builds).
-// The workspace ID is resolved from the request context if available,
-// otherwise from the explicit workspaceID parameter (for runner contexts).
-func ReportGomongoFallback(ctx context.Context, workspaceID, statement, errorMessage string) {
- if globalReporter == nil {
- return
- }
-
- globalReporter.mu.RLock()
- if !globalReporter.enabled {
- globalReporter.mu.RUnlock()
- return
- }
- version := globalReporter.version
- gitCommit := globalReporter.gitCommit
- globalReporter.mu.RUnlock()
-
- // Skip telemetry in development builds
- if version == "development" {
- return
- }
-
- // Prefer workspace from context (API requests), fall back to parameter (runners).
- if wsFromCtx := common.GetWorkspaceIDFromContext(ctx); wsFromCtx != "" {
- workspaceID = wsFromCtx
- }
- if workspaceID == "" {
- return
- }
-
- // Truncate statement
- truncatedStatement, truncated := common.TruncateString(statement, maxStatementLength)
- if truncated {
- truncatedStatement += "..."
- }
-
- // Check deduplication
- hash := hashStatement(truncatedStatement)
- if !globalReporter.shouldReport(hash) {
- return
- }
-
- // Extract email from context user.
- var email string
- if user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage); ok {
- email = user.Email
- }
-
- // Build payload
- payload := gomongoFallbackPayload{
- WorkspaceID: workspaceID,
- Email: email,
- Version: version,
- Commit: gitCommit,
- }
- payload.GomongoFallback.Statement = truncatedStatement
- payload.GomongoFallback.ErrorMessage = errorMessage
-
- // Send async with a detached context so the request context cancellation
- // does not abort the telemetry HTTP call.
- go globalReporter.send(context.WithoutCancel(ctx), payload)
-}
-
-func (r *Reporter) shouldReport(hash string) bool {
- r.mu.Lock()
- defer r.mu.Unlock()
-
- if _, ok := r.cache.Get(hash); ok {
- return false
- }
-
- r.cache.Add(hash, struct{}{})
- return true
-}
-
-func (r *Reporter) send(ctx context.Context, payload any) {
- body, err := json.Marshal(payload)
- if err != nil {
- return
- }
-
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, hubEventURL, bytes.NewReader(body))
- if err != nil {
- return
- }
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := r.httpClient.Do(req)
- if err != nil {
- return
- }
- resp.Body.Close()
-}
-
-func hashStatement(statement string) string {
- h := sha256.Sum256([]byte(statement))
- return hex.EncodeToString(h[:])
-}
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.go
index d9fe7d941f..d1f434f12b 100644
--- a/backend/plugin/db/mongodb/mongodb.go
+++ b/backend/plugin/db/mongodb/mongodb.go
@@ -2,15 +2,12 @@
package mongodb
import (
- "bytes"
"context"
"database/sql"
"fmt"
"io"
"log/slog"
"net/url"
- "os"
- "os/exec"
"strings"
"time"
@@ -21,9 +18,7 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo/options"
"google.golang.org/protobuf/types/known/durationpb"
- "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
- "github.com/bytebase/bytebase/backend/component/telemetry"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/plugin/db"
@@ -92,13 +87,11 @@ func (*Driver) GetDB() *sql.DB {
return nil
}
-// Execute executes MongoDB statements one by one, trying gomongo first and
-// falling back to mongosh for unsupported operations.
+// Execute executes MongoDB statements one by one via gomongo.
func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {
stmts, err := mongodbparser.SplitSQL(statement)
if err != nil {
- // If parsing fails, fall back to executing the entire statement via mongosh.
- return d.executeWithMongosh(ctx, statement)
+ return 0, errors.Wrap(err, "failed to split MongoDB statement")
}
stmts = base.FilterEmptyStatements(stmts)
@@ -111,20 +104,9 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
for _, stmt := range stmts {
opts.LogCommandExecute(stmt.Range, stmt.Text)
- _, gomongoErr := gmClient.Execute(ctx, d.databaseName, stmt.Text)
- if gomongoErr != nil && isFallbackError(gomongoErr) {
- // gomongo doesn't support this operation; fall back to mongosh.
- if mongoshErr := d.executeWithMongoshSingle(ctx, stmt.Text); mongoshErr == nil {
- slog.Debug("executed statement with mongosh fallback", slog.String("statement", stmt.Text), log.BBError(gomongoErr))
- telemetry.ReportGomongoFallback(ctx, "", stmt.Text, gomongoErr.Error())
- gomongoErr = nil
- } else {
- gomongoErr = mongoshErr
- }
- }
- if gomongoErr != nil {
- opts.LogCommandResponse(0, nil, gomongoErr.Error())
- return 0, gomongoErr
+ if _, err := gmClient.Execute(ctx, d.databaseName, stmt.Text); err != nil {
+ opts.LogCommandResponse(0, nil, err.Error())
+ return 0, err
}
opts.LogCommandResponse(0, nil, "")
}
@@ -132,125 +114,6 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
return 0, nil
}
-// isFallbackError returns true if the error from gomongo indicates the
-// operation should be retried with mongosh.
-func isFallbackError(err error) bool {
- var parseErr *gomongo.ParseError
- var unsupported *gomongo.UnsupportedOperationError
- var planned *gomongo.PlannedOperationError
- var unsupportedOpt *gomongo.UnsupportedOptionError
- return errors.As(err, &parseErr) ||
- errors.As(err, &unsupported) ||
- errors.As(err, &planned) ||
- errors.As(err, &unsupportedOpt)
-}
-
-// buildMongoshBaseArgs returns the base mongosh arguments (connection URI, TLS
-// flags) and a cleanup function that removes any temporary certificate files.
-func (d *Driver) buildMongoshBaseArgs() (args []string, cleanup func(), err error) {
- connectionURI := getBasicMongoDBConnectionURI(d.connCfg)
- args = []string{
- connectionURI,
- "--retryWrites", "false",
- "--quiet",
- }
-
- var cleanups []string
- cleanup = func() {
- for _, f := range cleanups {
- _ = os.Remove(f)
- }
- }
-
- if d.connCfg.DataSource.GetUseSsl() {
- args = append(args, "--tls")
- if !d.connCfg.DataSource.GetVerifyTlsCertificate() {
- args = append(args, "--tlsAllowInvalidHostnames", "--tlsAllowInvalidCertificates")
- }
-
- if d.connCfg.DataSource.GetSslCa() == "" {
- args = append(args, "--tlsUseSystemCA")
- } else {
- caFile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("mongodb-tls-ca-%s-*", d.connCfg.ConnectionContext.DatabaseName))
- if err != nil {
- return nil, nil, errors.Wrap(err, "failed to create tlsCAFile temporary file")
- }
- if _, err := caFile.WriteString(d.connCfg.DataSource.GetSslCa()); err != nil {
- _ = caFile.Close()
- _ = os.Remove(caFile.Name())
- return nil, nil, errors.Wrap(err, "failed to write tlsCAFile to temporary file")
- }
- if err := caFile.Close(); err != nil {
- _ = os.Remove(caFile.Name())
- return nil, nil, errors.Wrap(err, "failed to close tlsCAFile temporary file")
- }
- cleanups = append(cleanups, caFile.Name())
- args = append(args, "--tlsCAFile", caFile.Name())
- }
-
- if d.connCfg.DataSource.GetSslKey() != "" && d.connCfg.DataSource.GetSslCert() != "" {
- clientCertFile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("mongodb-tls-client-cert-%s-*", d.connCfg.ConnectionContext.DatabaseName))
- if err != nil {
- cleanup()
- return nil, nil, errors.Wrap(err, "failed to create client certificate temporary file")
- }
- certContent := d.connCfg.DataSource.GetSslKey() + "\n" + d.connCfg.DataSource.GetSslCert()
- if _, err := clientCertFile.WriteString(certContent); err != nil {
- _ = clientCertFile.Close()
- _ = os.Remove(clientCertFile.Name())
- cleanup()
- return nil, nil, errors.Wrap(err, "failed to write client certificate to temporary file")
- }
- if err := clientCertFile.Close(); err != nil {
- _ = os.Remove(clientCertFile.Name())
- cleanup()
- return nil, nil, errors.Wrap(err, "failed to close client certificate temporary file")
- }
- cleanups = append(cleanups, clientCertFile.Name())
- args = append(args, "--tlsCertificateKeyFile", clientCertFile.Name())
- }
- }
-
- return args, cleanup, nil
-}
-
-// executeWithMongoshSingle executes a single statement via mongosh --file.
-func (d *Driver) executeWithMongoshSingle(ctx context.Context, statement string) error {
- _, err := d.executeWithMongosh(ctx, statement)
- return err
-}
-
-// executeWithMongosh executes a statement via mongosh --file.
-func (d *Driver) executeWithMongosh(ctx context.Context, statement string) (int64, error) {
- mongoshArgs, cleanup, err := d.buildMongoshBaseArgs()
- if err != nil {
- return 0, err
- }
- defer cleanup()
-
- tempFile, err := os.CreateTemp(os.TempDir(), "mongodb-statement")
- if err != nil {
- return 0, 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")
- }
- if err := tempFile.Close(); err != nil {
- return 0, errors.Wrap(err, "failed to close temporary file")
- }
- mongoshArgs = append(mongoshArgs, "--file", tempFile.Name())
-
- mongoshCmd := exec.CommandContext(ctx, "mongosh", mongoshArgs...)
- var errContent, outContent bytes.Buffer
- mongoshCmd.Stderr = &errContent
- mongoshCmd.Stdout = &outContent
- if err := mongoshCmd.Run(); err != nil {
- return 0, errors.Wrapf(err, "failed to execute statement in mongosh:\nstdout: %s\nstderr: %s", outContent.String(), errContent.String())
- }
- return 0, nil
-}
-
// Dump dumps the database.
func (*Driver) Dump(_ context.Context, _ io.Writer, _ *storepb.DatabaseSchemaMetadata) error {
return nil
@@ -261,10 +124,10 @@ func (*Driver) Dump(_ context.Context, _ io.Writer, _ *storepb.DatabaseSchemaMet
// https://www.mongodb.com/docs/manual/reference/connection-string/
func getBasicMongoDBConnectionURI(connConfig db.ConnectionConfig) string {
u := &url.URL{
- Scheme: "mongodb",
// In RFC, there can be no tailing slash('/') in the path if the path is empty and the query is not empty.
- // For mongosh, it can handle this case correctly, but for driver, it will throw the error likes "error parsing uri: must have a / before the query ?".
- Path: "/",
+ // The Go driver throws "error parsing uri: must have a / before the query ?" without it.
+ Scheme: "mongodb",
+ Path: "/",
}
if connConfig.DataSource.GetSrv() {
u.Scheme = "mongodb+srv"
@@ -321,26 +184,16 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q
statement = strings.Trim(statement, " \t\n\r\f;")
startTime := time.Now()
- var gomongoErr error
- if d.client != nil {
- gmClient := gomongo.NewClient(d.client)
- var gmResult *gomongo.Result
- var opts []gomongo.ExecuteOption
- if queryContext.Limit > 0 {
- opts = append(opts, gomongo.WithMaxRows(int64(queryContext.Limit)))
- }
- gmResult, gomongoErr = gmClient.Execute(ctx, d.databaseName, statement, opts...)
- if gomongoErr == nil {
- return d.convertGomongoResult(gmResult, statement, startTime), nil
- }
+ gmClient := gomongo.NewClient(d.client)
+ var gmOpts []gomongo.ExecuteOption
+ if queryContext.Limit > 0 {
+ gmOpts = append(gmOpts, gomongo.WithMaxRows(int64(queryContext.Limit)))
}
-
- results, err := d.queryConnWithMongosh(ctx, statement, queryContext, startTime)
- if err == nil && gomongoErr != nil {
- slog.Debug("executed query with mongosh fallback", slog.String("statement", statement), log.BBError(gomongoErr))
- telemetry.ReportGomongoFallback(ctx, "", statement, gomongoErr.Error())
+ result, err := gmClient.Execute(ctx, d.databaseName, statement, gmOpts...)
+ if err != nil {
+ return nil, err
}
- return results, err
+ return d.convertGomongoResult(result, statement, startTime), nil
}
func (*Driver) convertGomongoResult(res *gomongo.Result, statement string, startTime time.Time) []*v1pb.QueryResult {
@@ -386,240 +239,3 @@ func marshalValueToExtJSON(v any) (string, error) {
}
return string(jsonBytes), nil
}
-
-func (d *Driver) queryConnWithMongosh(ctx context.Context, statement string, queryContext db.QueryContext, startTime time.Time) ([]*v1pb.QueryResult, error) {
- simpleStatement := isMongoStatement(statement)
- connectionURI := getBasicMongoDBConnectionURI(d.connCfg)
- // For MongoDB query, we execute the statement in mongosh with flag --eval for the following reasons:
- // 1. Query always short, so it's safe to execute in the command line.
- // 2. We cannot catch the output if we use the --file option.
-
- evalArg := statement
- if simpleStatement {
- limit := ""
- if queryContext.Limit > 0 {
- limit = fmt.Sprintf(".slice(0, %d)", queryContext.Limit)
- }
- evalArg = fmt.Sprintf(`a = %s; if (typeof a.toArray === 'function') {a.toArray()%s;} else {a;}`, statement, limit)
- }
- // We will use single quotes for the evalArg, so we need to escape the single quotes in the statement.
- evalArg = strings.ReplaceAll(evalArg, `'`, `'"'`)
- evalArg = fmt.Sprintf(`'%s'`, evalArg)
-
- mongoshArgs := []string{
- "mongosh",
- // quote the connectionURI because we execute the mongosh via sh, and the multi-queries part contains '&', which will be translated to the background process.
- fmt.Sprintf(`"%s"`, connectionURI),
- "--quiet",
- "--json",
- "canonical",
- "--eval",
- evalArg,
- // DocumentDB do not support retryWrites, so we set it to false.
- "--retryWrites",
- "false",
- }
- var tlsTempFiles []string
- defer func() {
- for _, fileName := range tlsTempFiles {
- _ = os.Remove(fileName)
- }
- }()
-
- if d.connCfg.DataSource.GetUseSsl() {
- mongoshArgs = append(mongoshArgs, "--tls")
-
- // Only allow invalid hostnames/certificates if certificate verification is disabled
- if !d.connCfg.DataSource.GetVerifyTlsCertificate() {
- mongoshArgs = append(mongoshArgs, "--tlsAllowInvalidHostnames")
- mongoshArgs = append(mongoshArgs, "--tlsAllowInvalidCertificates")
- }
-
- if d.connCfg.DataSource.GetSslCa() == "" {
- mongoshArgs = append(mongoshArgs, "--tlsUseSystemCA")
- } else {
- caFile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("mongodb-tls-ca-%s-*", d.connCfg.ConnectionContext.DatabaseName))
- if err != nil {
- return nil, errors.Wrap(err, "failed to create tlsCAFile temporary file")
- }
- if _, err := caFile.WriteString(d.connCfg.DataSource.GetSslCa()); err != nil {
- _ = caFile.Close()
- _ = os.Remove(caFile.Name())
- return nil, errors.Wrap(err, "failed to write tlsCAFile to temporary file")
- }
- if err := caFile.Close(); err != nil {
- _ = os.Remove(caFile.Name())
- return nil, errors.Wrap(err, "failed to close tlsCAFile temporary file")
- }
- tlsTempFiles = append(tlsTempFiles, caFile.Name())
- mongoshArgs = append(mongoshArgs, "--tlsCAFile", caFile.Name())
- }
-
- if d.connCfg.DataSource.GetSslKey() != "" && d.connCfg.DataSource.GetSslCert() != "" {
- clientCertFile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("mongodb-tls-client-cert-%s-*", d.connCfg.ConnectionContext.DatabaseName))
- if err != nil {
- return nil, errors.Wrap(err, "failed to create client certificate temporary file")
- }
- var sb strings.Builder
- if _, err := sb.WriteString(d.connCfg.DataSource.GetSslKey()); err != nil {
- _ = clientCertFile.Close()
- _ = os.Remove(clientCertFile.Name())
- return nil, errors.Wrapf(err, "failed to write ssl key into string builder")
- }
- if _, err := sb.WriteString("\n"); err != nil {
- _ = clientCertFile.Close()
- _ = os.Remove(clientCertFile.Name())
- return nil, errors.Wrapf(err, "failed to write new line into string builder")
- }
- if _, err := sb.WriteString(d.connCfg.DataSource.GetSslCert()); err != nil {
- _ = clientCertFile.Close()
- _ = os.Remove(clientCertFile.Name())
- return nil, errors.Wrapf(err, "failed to write ssl cert into string builder")
- }
- if _, err := clientCertFile.WriteString(sb.String()); err != nil {
- _ = clientCertFile.Close()
- _ = os.Remove(clientCertFile.Name())
- return nil, errors.Wrap(err, "failed to write tlsCAFile to temporary file")
- }
- if err := clientCertFile.Close(); err != nil {
- _ = os.Remove(clientCertFile.Name())
- return nil, errors.Wrap(err, "failed to close client certificate temporary file")
- }
- tlsTempFiles = append(tlsTempFiles, clientCertFile.Name())
- mongoshArgs = append(mongoshArgs, "--tlsCertificateKeyFile", clientCertFile.Name())
- }
- }
-
- queryResultFile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("mongodb-query-%s-*", d.connCfg.ConnectionContext.DatabaseName))
- if err != nil {
- return nil, errors.Wrap(err, "failed to create query result temporary file")
- }
- if err := queryResultFile.Close(); err != nil {
- _ = os.Remove(queryResultFile.Name())
- return nil, errors.Wrap(err, "failed to close query result temporary file")
- }
- defer func() {
- // While error occurred in mongosh, the temporary file may not created, so we ignore the error here.
- _ = os.Remove(queryResultFile.Name())
- }()
- mongoshArgs = append(mongoshArgs, ">", queryResultFile.Name())
-
- shellArgs := []string{
- "-c",
- strings.Join(mongoshArgs, " "),
- }
- shCmd := exec.CommandContext(ctx, "sh", shellArgs...)
- var errContent bytes.Buffer
- var outContent bytes.Buffer
- shCmd.Stderr = &errContent
- shCmd.Stdout = &outContent
- if err := shCmd.Run(); err != nil {
- f, ferr := os.OpenFile(queryResultFile.Name(), os.O_RDONLY, 0644)
- if ferr == nil {
- defer f.Close()
- if content, ferr := io.ReadAll(f); ferr == nil {
- return []*v1pb.QueryResult{{
- Latency: durationpb.New(time.Since(startTime)),
- Statement: statement,
- Error: string(content),
- }}, nil
- }
- }
- return nil, errors.Wrapf(err, "failed to execute statement in mongosh: \n stdout: %s\n stderr: %s", outContent.String(), errContent.String())
- }
-
- f, err := os.OpenFile(queryResultFile.Name(), os.O_RDONLY, 0644)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to open file: %s", queryResultFile.Name())
- }
- defer f.Close()
-
- fileInfo, err := f.Stat()
- if err != nil {
- return nil, err
- }
- if int64(fileInfo.Size()) > queryContext.MaximumSQLResultSize {
- return []*v1pb.QueryResult{{
- Latency: durationpb.New(time.Since(startTime)),
- Statement: statement,
- Error: common.FormatMaximumSQLResultSizeMessage(queryContext.MaximumSQLResultSize),
- }}, nil
- }
-
- content, err := io.ReadAll(f)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to read file: %s", queryResultFile.Name())
- }
-
- if simpleStatement {
- // We make best-effort attempt to parse the content and fallback to single bulk result on failure.
- result, err := getSimpleStatementResult(content)
- if err != nil {
- slog.Error("failed to get simple statement result", slog.String("content", string(content)), log.BBError(err))
- } else {
- result.Latency = durationpb.New(time.Since(startTime))
- result.Statement = statement
- return []*v1pb.QueryResult{result}, nil
- }
- }
-
- return []*v1pb.QueryResult{{
- ColumnNames: []string{"result"},
- ColumnTypeNames: []string{"TEXT"},
- Rows: []*v1pb.QueryRow{{
- Values: []*v1pb.RowValue{{
- Kind: &v1pb.RowValue_StringValue{StringValue: string(content)},
- }},
- }},
- Latency: durationpb.New(time.Since(startTime)),
- Statement: statement,
- RowsCount: 0, /* unknown */
- }}, nil
-}
-
-func getSimpleStatementResult(data []byte) (*v1pb.QueryResult, error) {
- rows, err := convertRows(data)
- if err != nil {
- return nil, err
- }
-
- result := &v1pb.QueryResult{
- ColumnNames: []string{"result"},
- ColumnTypeNames: []string{"TEXT"},
- }
-
- for _, v := range rows {
- r, err := bson.MarshalExtJSONIndent(v, false, false, "", " ")
- if err != nil {
- return nil, err
- }
- result.Rows = append(result.Rows, &v1pb.QueryRow{
- Values: []*v1pb.RowValue{
- {Kind: &v1pb.RowValue_StringValue{StringValue: string(r)}},
- },
- })
- }
- return result, nil
-}
-
-func convertRows(data []byte) ([]any, error) {
- var a any
- // Set canonical to false in order to accept both canonical and relaxed format.
- // https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/
- if err := bson.UnmarshalExtJSON(data, false, &a); err != nil {
- return nil, err
- }
-
- if aa, ok := a.(bson.A); ok {
- return []any(aa), nil
- }
- return []any{a}, nil
-}
-
-func isMongoStatement(statement string) bool {
- statement = strings.ToLower(statement)
- if strings.HasPrefix(statement, "db.") {
- return true
- }
- return strings.HasPrefix(statement, "db[")
-}
diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.go
index ffadf019f3..8973c4040e 100644
--- a/backend/plugin/db/mongodb/mongodb_test.go
+++ b/backend/plugin/db/mongodb/mongodb_test.go
@@ -1,18 +1,11 @@
package mongodb
import (
- "fmt"
- "os"
- "path/filepath"
"testing"
- "github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
- "google.golang.org/protobuf/testing/protocmp"
- "google.golang.org/protobuf/types/known/durationpb"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -172,144 +165,3 @@ func TestIsSystemCollection(t *testing.T) {
})
}
}
-
-func TestIsMongoStatement(t *testing.T) {
- tests := []struct {
- statement string
- want bool
- }{
- {
- statement: `show collections`,
- want: false,
- },
- {
- statement: `db.cpl_station_info.find().limit(100)`,
- want: true,
- },
- {
- statement: `db["collection"].find().limit(50)`,
- want: true,
- },
- {
- statement: `db['collection'].find().limit(50)`,
- want: true,
- },
- {
- statement: `db[variableName].find()`,
- want: true,
- },
- {
- statement: `DB["COLLECTION"].FIND()`,
- want: true,
- },
- }
-
- a := require.New(t)
- for _, tt := range tests {
- got := isMongoStatement(tt.statement)
- a.Equal(tt.want, got, tt.statement)
- }
-}
-
-func TestBuildMongoshBaseArgsUsesTempDirForTLSFiles(t *testing.T) {
- driver := &Driver{
- connCfg: db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Host: "localhost",
- Port: "27017",
- UseSsl: true,
- VerifyTlsCertificate: false,
- SslCa: "ca-pem",
- SslCert: "cert-pem",
- SslKey: "key-pem",
- },
- },
- }
-
- args, cleanup, err := driver.buildMongoshBaseArgs()
- require.NoError(t, err)
- defer cleanup()
-
- var caFile, clientCertFile string
- for i := 0; i < len(args)-1; i++ {
- switch args[i] {
- case "--tlsCAFile":
- caFile = args[i+1]
- case "--tlsCertificateKeyFile":
- clientCertFile = args[i+1]
- default:
- }
- }
-
- require.NotEmpty(t, caFile)
- require.NotEmpty(t, clientCertFile)
- require.Equal(t, filepath.Clean(os.TempDir()), filepath.Clean(filepath.Dir(caFile)))
- require.Equal(t, filepath.Clean(os.TempDir()), filepath.Clean(filepath.Dir(clientCertFile)))
- require.FileExists(t, caFile)
- require.FileExists(t, clientCertFile)
-
- cleanup()
- require.NoFileExists(t, caFile)
- require.NoFileExists(t, clientCertFile)
-}
-
-func TestGetSimpleStatementResult(t *testing.T) {
- testData1 := `{
- "_id": {
- "$oid": "66f62cad7195ccc0dbdfafbb"
- },
- "a": {
- "$numberLong": "1546786128982089728"
- }
-}`
- relaxedTestData1 := `{
- "_id": {
- "$oid": "66f62cad7195ccc0dbdfafbb"
- },
- "a": 1546786128982089728
-}`
-
- testData2 := `{
- "_id": {
- "$oid": "66f6758c30daae815ac8784f"
- },
- "name": "dannyyy",
- "groups": [
- "123",
- "222"
- ]
-}`
-
- tests := []struct {
- data string
- want *v1pb.QueryResult
- }{
- {
- data: fmt.Sprintf(`[%s, %s]`, testData1, testData2),
- want: &v1pb.QueryResult{
- ColumnNames: []string{"result"},
- ColumnTypeNames: []string{"TEXT"},
- Rows: []*v1pb.QueryRow{
- {
- Values: []*v1pb.RowValue{
- {Kind: &v1pb.RowValue_StringValue{StringValue: relaxedTestData1}},
- },
- },
- {
- Values: []*v1pb.RowValue{
- {Kind: &v1pb.RowValue_StringValue{StringValue: testData2}},
- },
- },
- },
- },
- },
- }
-
- a := require.New(t)
- for _, tt := range tests {
- result, err := getSimpleStatementResult([]byte(tt.data))
- a.NoError(err)
- diff := cmp.Diff(tt.want, result, protocmp.Transform(), protocmp.IgnoreMessages(&durationpb.Duration{}))
- a.Empty(diff)
- }
-}
diff --git a/backend/plugin/db/mongodb/mongodb_testcontainer_test.go b/backend/plugin/db/mongodb/mongodb_testcontainer_test.go
index e348c4e396..7b7f0fec7f 100644
--- a/backend/plugin/db/mongodb/mongodb_testcontainer_test.go
+++ b/backend/plugin/db/mongodb/mongodb_testcontainer_test.go
@@ -2,7 +2,6 @@ package mongodb
import (
"context"
- "os/exec"
"strings"
"testing"
@@ -16,30 +15,6 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db"
)
-// requireMongosh checks if mongosh is installed and fails the test if not.
-// These tests require mongosh to be installed because the MongoDB driver
-// executes queries by shelling out to mongosh (see mongodb.go:174 and mongodb.go:344).
-//
-// TODO: These tests can be removed after migrating MongoDB driver to use Go driver API
-// instead of shelling out to mongosh CLI.
-//
-// To install mongosh v2.5.0 (recommended version):
-// - macOS: brew install mongosh
-// - Linux: Download from https://github.com/mongodb-js/mongosh/releases/tag/v2.5.0
-// - CI: Automatically installed in .github/workflows/backend-tests.yml
-func requireMongosh(t *testing.T) {
- t.Helper()
- path, err := exec.LookPath("mongosh")
- if err != nil {
- t.Fatalf("mongosh is required but not found in PATH. Please install mongosh v2.5.0 to run this test.\n"+
- "Install instructions:\n"+
- " macOS: brew install mongosh\n"+
- " Linux: https://github.com/mongodb-js/mongosh/releases/tag/v2.5.0\n"+
- "Error: %v", err)
- }
- t.Logf("Using mongosh at: %s", path)
-}
-
// TestQueryWithBracketNotation tests the critical user journey (CUJ) of querying
// MongoDB collections using bracket notation with different quote styles.
// This ensures the fix for PR #17282 (which changed to single-quote bracket notation
@@ -49,8 +24,6 @@ func TestQueryWithBracketNotation(t *testing.T) {
t.Skip("Skipping MongoDB testcontainer test in short mode")
}
- requireMongosh(t)
-
ctx := context.Background()
// Get MongoDB container from testcontainer utility
@@ -161,8 +134,6 @@ func TestQueryWithBracketNotationStructure(t *testing.T) {
t.Skip("Skipping MongoDB testcontainer test in short mode")
}
- requireMongosh(t)
-
ctx := context.Background()
// Get MongoDB container from testcontainer utility
diff --git a/backend/server/server.go b/backend/server/server.go
index b1cd6cd6b3..cf527c712c 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -27,7 +27,6 @@ import (
"github.com/bytebase/bytebase/backend/component/iam"
"github.com/bytebase/bytebase/backend/component/sampleinstance"
"github.com/bytebase/bytebase/backend/component/sheet"
- "github.com/bytebase/bytebase/backend/component/telemetry"
"github.com/bytebase/bytebase/backend/component/webhook"
"github.com/bytebase/bytebase/backend/demo"
"github.com/bytebase/bytebase/backend/enterprise"
@@ -187,11 +186,6 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
if logSetup.EnableDebug {
log.LogLevel.Set(slog.LevelDebug)
}
- telemetry.InitGlobalReporter(
- profile.Version,
- profile.GitCommit,
- logSetup.GetEnableMetricCollection(),
- )
s.bus, err = bus.New()
if err != nil {
diff --git a/go.mod b/go.mod
index 2a7c326f07..9c8f8e0dbf 100644
--- a/go.mod
+++ b/go.mod
@@ -32,9 +32,9 @@ require (
github.com/beltran/gohive/v2 v2.0.0
github.com/blang/semver/v4 v4.0.0
github.com/bmatcuk/doublestar/v4 v4.10.0
- github.com/bytebase/gomongo v0.0.0-20260403033636-f6c50d4ab888
+ github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
- github.com/bytebase/omni v0.0.0-20260509021101-01140a7b9722
+ github.com/bytebase/omni v0.0.0-20260509101155-5091fce0322a
github.com/caarlos0/env/v11 v11.4.1
github.com/cenkalti/backoff/v5 v5.0.3
github.com/cockroachdb/cockroachdb-parser v0.25.2
diff --git a/go.sum b/go.sum
index 86ca991629..586fe908c3 100644
--- a/go.sum
+++ b/go.sum
@@ -246,12 +246,12 @@ github.com/bytebase/gh-ost2 v1.1.7-0.20260509080506-4ca6df829327 h1:zfT0iH+eaP1k
github.com/bytebase/gh-ost2 v1.1.7-0.20260509080506-4ca6df829327/go.mod h1:XVUEFLOcM31VxA/pvm1YMolnDWlzVyvsqZy9OZKVJ8k=
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-20260403033636-f6c50d4ab888 h1:E8tz8maxpih/vtCnyrqtIOQlFbO00gT+Ahk6+W6kyQs=
-github.com/bytebase/gomongo v0.0.0-20260403033636-f6c50d4ab888/go.mod h1:IgLUOjyiHehdE0G3C92IjGYu9lJQgkPwf2mdNSvPWq8=
+github.com/bytebase/gomongo v0.0.0-20260510192859-8526a440b352 h1:3sC5pdvJ7bNAhRDQ2Qd1EkW2+CJNAQiL6yB94gzCH/c=
+github.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-20260509021101-01140a7b9722 h1:zeMIExVxJVNoU3DZfrKrMMrdZTmUJ264bihZ3fDNcqo=
-github.com/bytebase/omni v0.0.0-20260509021101-01140a7b9722/go.mod h1:EVG8nQbNPUnUBGKn6J0niSGOs3jgfKo3TciY22R4bfo=
+github.com/bytebase/omni v0.0.0-20260509101155-5091fce0322a h1:DFSkgKegf9yqinPUKo13O6x7STk3SbRY4eeurJt3WVI=
+github.com/bytebase/omni v0.0.0-20260509101155-5091fce0322a/go.mod h1:EVG8nQbNPUnUBGKn6J0niSGOs3jgfKo3TciY22R4bfo=
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=
diff --git a/scripts/Dockerfile b/scripts/Dockerfile
index 3757fa3a0f..f77e2f35f5 100644
--- a/scripts/Dockerfile
+++ b/scripts/Dockerfile
@@ -55,10 +55,6 @@ RUN apk add postgresql16
# Fix the posgres server issue (invalid value for parameter "lc_messages": "en_US.UTF-8").
#RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen
-# Install mongosh.
-RUN apk add --no-cache npm
-RUN npm install -g [email protected]
-
# The file indicates running in docker environment.
RUN touch /etc/bb.env
COPY --from=backend /backend-build/bytebase /usr/local/bin/