Skip to content

Commit 676224e

Browse files
committed
mongo connection
1 parent 98dc7e8 commit 676224e

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Configurations are stored in json files. Example:
7171
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7272
| mysql | [spec](https://github.com/go-sql-driver/mysql#dsn-data-source-name), example `db_user:db_pass@(db_host:3306)/db_name?parseTime=true` (parseTime=true - required option) |
7373
| local | omit this property, because every instance have its own in-memory storage |
74-
| mongo | NOTE! Mongo db temporary supports only persistent node strategy, example `mongodb://localhost:27017`
74+
| mongo | NOTE! Mongo db supports only persistent node strateg and version from 4.0, example `mongodb://localhost:27017`
7575

7676
### Statsd - Configuration of metrics(optional).
7777
| Option | Possible values | Description |

storage/mongo/factory.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package mongo
33
import (
44
"context"
55
"errors"
6-
76
"go.mongodb.org/mongo-driver/bson"
87
"go.mongodb.org/mongo-driver/mongo"
98
"go.mongodb.org/mongo-driver/mongo/options"
9+
"go.mongodb.org/mongo-driver/x/bsonx"
10+
"strconv"
11+
"strings"
1012

1113
"github.com/qa-dev/jsonwire-grid/config"
1214
"github.com/qa-dev/jsonwire-grid/pool"
@@ -29,6 +31,12 @@ func (f *Factory) Create(cfg config.Config) (pool.StorageInterface, error) {
2931
}
3032

3133
db := client.Database(cfg.DB.DbName)
34+
err = checkServerVersion(ctx, client)
35+
if err != nil {
36+
err = errors.New("version check error: " + err.Error())
37+
return nil, err
38+
}
39+
3240
s := NewMongoStorage(db)
3341
mod := mongo.IndexModel{
3442
Keys: bson.M{
@@ -43,5 +51,27 @@ func (f *Factory) Create(cfg config.Config) (pool.StorageInterface, error) {
4351
err = errors.New("Create index error: " + err.Error())
4452
return nil, err
4553
}
54+
4655
return s, nil
4756
}
57+
58+
func checkServerVersion(ctx context.Context, client *mongo.Client) error {
59+
serverStatus, err := client.Database("admin").RunCommand(
60+
ctx,
61+
bsonx.Doc{{"serverStatus", bsonx.Int32(1)}},
62+
).DecodeBytes()
63+
if err != nil {
64+
return err
65+
}
66+
67+
version, err := serverStatus.LookupErr("version")
68+
if err != nil {
69+
return err
70+
}
71+
72+
majorVersion, err := strconv.Atoi(strings.Split(version.StringValue(), ".")[0])
73+
if majorVersion < 4 {
74+
return errors.New("mongodb version not supported: " + version.StringValue())
75+
}
76+
return nil
77+
}

storage/mongo/factory_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mongo
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/qa-dev/jsonwire-grid/config"
10+
)
11+
12+
func TestFactory_Create_Positive(t *testing.T) {
13+
f := Factory{}
14+
storage, err := f.Create(config.Config{
15+
DB: config.DB{
16+
Connection: os.Getenv(dbConnectionStringEnvVariable),
17+
DbName: "grid",
18+
},
19+
})
20+
assert.NoError(t, err)
21+
assert.NotNil(t, storage)
22+
}

0 commit comments

Comments
 (0)