Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(neo4j): connect to Memgraph via the direct-connection fallback

HARRISON051cab88a383
diff --git a/agents/drivers/neo4j-go/driver.go b/agents/drivers/neo4j-go/driver.go
index 41d7f42da6..927982a59d 100644
--- a/agents/drivers/neo4j-go/driver.go
+++ b/agents/drivers/neo4j-go/driver.go
@@ -65,6 +65,9 @@ func openVerifiedRuntime(params connectParams) (*connectionRuntime, error) {
// Older Bolt versions supply numeric IDs in the element-ID fields.
runtime.legacySingleDatabase = info.ProtocolVersion().Major < 4
runtime.legacyGraphIDs = info.ProtocolVersion().Major < 5
+ // Memgraph reports itself as a Neo4j-compatible server, so the product
+ // name is the only way to tell them apart.
+ runtime.memgraph = strings.Contains(strings.ToLower(info.Agent()), "memgraph")
}
return runtime, nil
}
@@ -234,6 +237,12 @@ func directConnectionParams(params connectParams) (connectParams, bool) {
// server reports that cluster routing is unavailable.
const routingUnsupportedMessage = "does not support: routing"
+// memgraphRoutingUnsupportedMessage is how Memgraph refuses the routing-table
+// procedure. Memgraph speaks Bolt and accepts `neo4j://`, so it is reachable
+// through the Neo4j connection type, but a single instance is not a cluster and
+// answers with this instead of the Neo4j wording (#11947).
+const memgraphRoutingUnsupportedMessage = "cannot fetch routing table"
+
// isRoutingUnsupported reports whether err is the driver's "routing requires a
// cluster" failure. The driver flattens its typed feature error into a usage
// error, so the message is inspected as well as the type.
@@ -245,7 +254,8 @@ func isRoutingUnsupported(err error) bool {
if errors.As(err, &featureErr) && strings.EqualFold(featureErr.Feature, "routing") {
return true
}
- return strings.Contains(strings.ToLower(err.Error()), routingUnsupportedMessage)
+ message := strings.ToLower(err.Error())
+ return strings.Contains(message, routingUnsupportedMessage) || strings.Contains(message, memgraphRoutingUnsupportedMessage)
}
func isNeo4jScheme(value string) bool {
@@ -335,9 +345,30 @@ func (s *server) databaseName(override string) string {
if value := strings.TrimSpace(override); value != "" {
return value
}
+ // Memgraph exposes exactly one database named `memgraph`, and sending the
+ // Neo4j default `neo4j` fails with "Tried to retrieve an unknown database".
+ // Omit the name unless the user configured one, so the server picks its own
+ // default (#11947).
+ if s.runtime != nil && s.runtime.memgraph && !hasConfiguredDatabase(s.params) {
+ return ""
+ }
return configuredDatabase(s.params)
}
+// hasConfiguredDatabase reports whether a database name came from the user
+// (the form field or the connection string) rather than the Neo4j default.
+func hasConfiguredDatabase(params connectParams) bool {
+ if strings.TrimSpace(params.Database) != "" {
+ return true
+ }
+ value := strings.TrimPrefix(strings.TrimSpace(params.ConnectionString), "jdbc:")
+ parsed, err := url.Parse(value)
+ if err != nil {
+ return false
+ }
+ return strings.TrimSpace(parsed.Query().Get("database")) != "" || strings.Trim(strings.TrimSpace(parsed.Path), "/") != ""
+}
+
func configuredDatabase(params connectParams) string {
if value := strings.TrimSpace(params.Database); value != "" {
return value
diff --git a/agents/drivers/neo4j-go/main.go b/agents/drivers/neo4j-go/main.go
index 793ace7393..61c0955de7 100644
--- a/agents/drivers/neo4j-go/main.go
+++ b/agents/drivers/neo4j-go/main.go
@@ -98,9 +98,15 @@ type connectionRuntime struct {
// they have a single default database and reject session database selection.
legacySingleDatabase bool
legacyGraphIDs bool
- references int
- closed bool
- mu sync.Mutex
+ // memgraph is set for Memgraph servers, which advertise themselves as
+ // "Neo4j/v5.11.0 compatible graph database server - Memgraph". They expose a
+ // single database named `memgraph`, and answer any other name with a fatal
+ // unknown-database error instead of falling back to their own default, so the
+ // Neo4j default must not be sent (#11947).
+ memgraph bool
+ references int
+ closed bool
+ mu sync.Mutex
}
type server struct {
diff --git a/agents/drivers/neo4j-go/main_test.go b/agents/drivers/neo4j-go/main_test.go
index 2c712445a5..04d83c47d8 100644
--- a/agents/drivers/neo4j-go/main_test.go
+++ b/agents/drivers/neo4j-go/main_test.go
@@ -81,6 +81,53 @@ func TestConfiguredDatabaseUsesConnectionString(t *testing.T) {
}
}
+// #11947: Memgraph has a single database named `memgraph`, so the Neo4j default
+// `neo4j` must not be sent — Memgraph fails the session with "Tried to retrieve
+// an unknown database" rather than falling back to its own default.
+func TestMemgraphOmitsTheNeo4jDefaultDatabase(t *testing.T) {
+ memgraph := &connectionRuntime{memgraph: true}
+ if got := (&server{runtime: memgraph}).databaseName(""); got != "" {
+ t.Fatalf("Memgraph without a configured database must send no name, got %q", got)
+ }
+ // A user who did configure one keeps it: Memgraph may grow multi-database
+ // support, and silently ignoring an explicit choice would be worse.
+ if got := (&server{runtime: memgraph}).databaseName("analytics"); got != "analytics" {
+ t.Fatalf("explicit override must win, got %q", got)
+ }
+ if got := (&server{runtime: memgraph, params: connectParams{Database: "mydb"}}).databaseName(""); got != "mydb" {
+ t.Fatalf("configured database must win, got %q", got)
+ }
+ if got := (&server{runtime: memgraph, params: connectParams{ConnectionString: "neo4j://db:7687/archive"}}).databaseName(""); got != "archive" {
+ t.Fatalf("connection-string database must win, got %q", got)
+ }
+
+ // Neo4j itself keeps the default: only Memgraph is special-cased.
+ neo4j := &connectionRuntime{}
+ if got := (&server{runtime: neo4j}).databaseName(""); got != defaultDatabase {
+ t.Fatalf("Neo4j must keep the %q default, got %q", defaultDatabase, got)
+ }
+}
+
+func TestHasConfiguredDatabase(t *testing.T) {
+ for _, testCase := range []struct {
+ name string
+ params connectParams
+ want bool
+ }{
+ {name: "nothing configured", params: connectParams{}, want: false},
+ {name: "form field", params: connectParams{Database: "mydb"}, want: true},
+ {name: "connection string query", params: connectParams{ConnectionString: "neo4j://db:7687?database=movies"}, want: true},
+ {name: "connection string path", params: connectParams{ConnectionString: "neo4j://db:7687/archive"}, want: true},
+ {name: "connection string without database", params: connectParams{ConnectionString: "bolt://db:7687"}, want: false},
+ } {
+ t.Run(testCase.name, func(t *testing.T) {
+ if got := hasConfiguredDatabase(testCase.params); got != testCase.want {
+ t.Fatalf("hasConfiguredDatabase(%+v) = %t, want %t", testCase.params, got, testCase.want)
+ }
+ })
+ }
+}
+
func TestEffectiveNeo4jSchemeMatchesBuiltURI(t *testing.T) {
for _, params := range []connectParams{
{Host: "127.0.0.1", Port: 7687},
@@ -178,6 +225,19 @@ func TestIsRoutingUnsupported(t *testing.T) {
want: true,
},
{name: "typed feature error", err: &neo4jdb.FeatureNotSupportedError{Server: "db:7687", Feature: "routing", Reason: "requires cluster setup"}, want: true},
+ // #11947: Memgraph speaks Bolt and accepts `neo4j://`, but refuses the
+ // routing-table procedure with its own wording, so the direct fallback
+ // has to recognise it too.
+ {
+ name: "memgraph single-instance routing refusal",
+ err: errors.New("Neo4jError: Memgraph.TransientError.MemgraphError.MemgraphError (You cannot fetch routing table from an instance which is not part of a cluster.)"),
+ want: true,
+ },
+ {
+ name: "memgraph routing table wording without neo4j error prefix",
+ err: errors.New("You cannot fetch routing table from an instance which is not part of a cluster."),
+ want: true,
+ },
{name: "typed route-to-database error", err: &neo4jdb.FeatureNotSupportedError{Server: "db:7687", Feature: "route to database", Reason: "requires at least server v4"}, want: false},
{name: "unrelated error", err: errors.New("connection refused"), want: false},
{name: "nil error", err: nil, want: false},