Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions drivers/mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ Add MongoDB credentials in following format in `config.json` file. To check more
"username": "test",
"password": "test",
"authdb": "admin",
"replica-set": "rs0",
"read-preference": "secondaryPreferred",
"replica_set": "rs0",
"read_preference": "secondaryPreferred",
"srv": false,
"server-ram": 16,
"server_ram": 16,
"database": "database",
"max_threads": 50,
"backoff_retry_count": 2,
"chunking_strategy":""
}
```

`read_preference` applies to both SRV and replica set connections. For SRV-based connections, set `srv` to `true` and leave `replica_set` empty.

## Commands
### Discover Command
The *Discover* command generates json content for `streams.json` file, which defines the schema of the collections to be synced.
Expand Down
12 changes: 8 additions & 4 deletions drivers/mongodb/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ func (c *Config) URI() string {

if c.ReplicaSet != "" {
query.Set("replicaSet", c.ReplicaSet)
if c.ReadPreference == "" {
c.ReadPreference = constants.DefaultReadPreference
}
query.Set("readPreference", c.ReadPreference)
}

readPreference := c.ReadPreference
if readPreference == "" && (c.ReplicaSet != "" || c.Srv) {
readPreference = constants.DefaultReadPreference
}
if readPreference != "" {
query.Set("readPreference", readPreference)
}

host := strings.Join(c.Hosts, ",")
Expand Down
102 changes: 102 additions & 0 deletions drivers/mongodb/internal/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package driver

import (
"strings"
"testing"
)

func TestConfig_URI(t *testing.T) {
tests := []struct {
name string
config *Config
expectedContains []string
notExpected []string
}{
{
// Leaves read preference unset for standalone non-SRV connections.
name: "without replica set or srv",
config: &Config{
Hosts: []string{"mongo.internal:27017"},
Username: "mongodb",
Password: "secret",
AuthDB: "admin",
},
expectedContains: []string{
"mongodb://mongodb:secret@mongo.internal:27017/",
"authSource=admin",
},
notExpected: []string{
"readPreference=",
"replicaSet=",
},
},
{
// Preserves the default read preference for replica set connections.
name: "with replica set and default read preference",
config: &Config{
Hosts: []string{"mongo-1.internal:27017", "mongo-2.internal:27017"},
Username: "mongodb",
Password: "secret",
AuthDB: "admin",
ReplicaSet: "rs0",
},
expectedContains: []string{
"replicaSet=rs0",
"readPreference=secondaryPreferred",
},
},
{
// Applies the default read preference for SRV-based connections.
name: "with srv and default read preference",
config: &Config{
Hosts: []string{"cluster0.example.mongodb.net"},
Username: "mongodb",
Password: "secret",
AuthDB: "admin",
Srv: true,
},
expectedContains: []string{
"mongodb+srv://mongodb:secret@cluster0.example.mongodb.net/",
"readPreference=secondaryPreferred",
},
notExpected: []string{
"replicaSet=",
},
},
{
// Honors an explicit read preference without requiring a replica set name.
name: "with explicit read preference and no replica set",
config: &Config{
Hosts: []string{"mongo.internal:27017"},
Username: "mongodb",
Password: "secret",
AuthDB: "admin",
ReadPreference: "nearest",
},
expectedContains: []string{
"readPreference=nearest",
},
notExpected: []string{
"replicaSet=",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uri := tt.config.URI()

for _, expected := range tt.expectedContains {
if !strings.Contains(uri, expected) {
t.Errorf("expected URI to contain %q, got %s", expected, uri)
}
}

for _, notExpected := range tt.notExpected {
if strings.Contains(uri, notExpected) {
t.Errorf("expected URI to not contain %q, got %s", notExpected, uri)
}
}
})
}
}
6 changes: 3 additions & 3 deletions drivers/mongodb/internal/testdata/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"username": "mongodb",
"password": "secure_password123",
"authdb": "admin",
"replica-set": "rs0",
"read-preference": "secondaryPreferred",
"replica_set": "rs0",
"read_preference": "secondaryPreferred",
"srv": false,
"server-ram": 16,
"server_ram": 16,
"database": "olake_mongodb_test",
"max_threads": 5,
"backoff_retry_count": 4
Expand Down
2 changes: 1 addition & 1 deletion drivers/mongodb/resources/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"read_preference": {
"type": "string",
"title": "Read Preference",
"description": "Read preference for MongoDB (e.g., secondaryPreferred)"
"description": "Read preference for MongoDB (e.g., secondaryPreferred). Applies to both SRV and replica set connections."
},
"replica_set": {
"type": "string",
Expand Down
Loading