Skip to content

Commit 3a2df66

Browse files
author
Roman Tarasov
committed
up dependencies
1 parent e8504f4 commit 3a2df66

File tree

4 files changed

+127
-99
lines changed

4 files changed

+127
-99
lines changed

commands/migrate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/coldze/mongol/primitives/mongo"
88
"github.com/coldze/primitives/custom_error"
99
"github.com/coldze/primitives/logs"
10-
mgo "github.com/mongodb/mongo-go-driver/mongo"
1110
)
1211

1312
func Migrate(path string, limit int64, log logs.Logger) custom_error.CustomError {
@@ -16,9 +15,10 @@ func Migrate(path string, limit int64, log logs.Logger) custom_error.CustomError
1615
return custom_error.NewErrorf(errValue, "Failed to load changelog.")
1716
}
1817
ctx := context.Background()
19-
mongoClient, err := mgo.Connect(ctx, changeLog.GetConnectionString(), nil)
18+
19+
mongoClient, err := newMgoClient(ctx, changeLog.GetConnectionString())
2020
if err != nil {
21-
return custom_error.MakeErrorf("Failed to connect to mongo. Error: %v", err)
21+
return custom_error.NewErrorf(err, "Failed to connect to mongo.")
2222
}
2323
defer mongoClient.Disconnect(ctx)
2424
db := mongoClient.Database(changeLog.GetDBName())

commands/rollback.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/coldze/mongol/primitives/mongo"
88
"github.com/coldze/primitives/custom_error"
99
"github.com/coldze/primitives/logs"
10-
mgo "github.com/mongodb/mongo-go-driver/mongo"
1110
)
1211

1312
func Rollback(path string, limit int64, log logs.Logger) custom_error.CustomError {
@@ -16,9 +15,9 @@ func Rollback(path string, limit int64, log logs.Logger) custom_error.CustomErro
1615
return custom_error.NewErrorf(errValue, "Failed to load changelog.")
1716
}
1817
ctx := context.Background()
19-
mongoClient, err := mgo.Connect(ctx, changeLog.GetConnectionString(), nil)
18+
mongoClient, err := newMgoClient(ctx, changeLog.GetConnectionString())
2019
if err != nil {
21-
return custom_error.MakeErrorf("Failed to connect to mongo. Error: %v", err)
20+
return custom_error.NewErrorf(err, "Failed to connect to mongo.")
2221
}
2322
defer mongoClient.Disconnect(ctx)
2423
db := mongoClient.Database(changeLog.GetDBName())

commands/util.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package commands
2+
3+
import (
4+
"context"
5+
6+
"github.com/coldze/primitives/custom_error"
7+
mgo "github.com/mongodb/mongo-go-driver/mongo"
8+
"github.com/mongodb/mongo-go-driver/mongo/options"
9+
)
10+
11+
func newMgoClient(ctx context.Context, uri string) (*mgo.Client, custom_error.CustomError) {
12+
clientOpts := options.Client()
13+
if clientOpts == nil {
14+
return nil, custom_error.MakeErrorf("Something went completely wrong. ClientOpts are nil")
15+
}
16+
clientOpts = clientOpts.ApplyURI(uri)
17+
if clientOpts == nil {
18+
return nil, custom_error.MakeErrorf("Something went completely wrong. ClientOpts are nil")
19+
}
20+
err := clientOpts.Validate()
21+
if err != nil {
22+
return nil, custom_error.MakeErrorf("Failed to validate ClientOpts. Error: %v", err)
23+
}
24+
mongoClient, err := mgo.Connect(ctx, clientOpts)
25+
if err != nil {
26+
return nil, custom_error.MakeErrorf("Failed to connect to mongo. Error: %v", err)
27+
}
28+
return mongoClient, nil
29+
}

0 commit comments

Comments
 (0)