Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 436b81b

Browse files
author
DBobrov
committed
Replaced bsonx.Doc with bson.D
1 parent 309edb6 commit 436b81b

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

database/mongodb/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| URL Query | WithInstance Config | Description |
1313
|------------|---------------------|-------------|
1414
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
15-
| `x-transaction-mode` | `TransactionMode` | If set to `true` wrap commands in [transaction](https://docs.mongodb.com/manual/core/transactions). Available only for replica set. |
15+
| `x-transaction-mode` | `TransactionMode` | If set to `true` wrap commands in [transaction](https://docs.mongodb.com/manual/core/transactions). Available only for replica set. Driver is using [strconv.ParseBool](https://golang.org/pkg/strconv/#ParseBool) for parsing|
1616
| `dbname` | `DatabaseName` | The name of the database to connect to |
1717
| `user` | | The user to sign in as. Can be omitted |
1818
| `password` | | The user's password. Can be omitted |

database/mongodb/mongodb.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"io"
77
"io/ioutil"
88
"net/url"
9+
"strconv"
910

1011
"github.com/golang-migrate/migrate/v4"
1112
"github.com/golang-migrate/migrate/v4/database"
1213
"github.com/mongodb/mongo-go-driver/bson"
1314
"github.com/mongodb/mongo-go-driver/mongo"
14-
"github.com/mongodb/mongo-go-driver/x/bsonx"
1515
"github.com/mongodb/mongo-go-driver/x/network/connstring"
1616
)
1717

@@ -80,7 +80,7 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
8080
migrationsCollection = DefaultMigrationsCollection
8181
}
8282

83-
transactionMode := purl.Query().Get("x-transaction-mode") == "true"
83+
transactionMode, _ := strconv.ParseBool(purl.Query().Get("x-transaction-mode"))
8484

8585
q := migrate.FilterCustomQuery(purl)
8686
q.Scheme = "mongodb"
@@ -133,7 +133,7 @@ func (m *Mongo) Run(migration io.Reader) error {
133133
if err != nil {
134134
return err
135135
}
136-
var cmds []bsonx.Doc
136+
var cmds []bson.D
137137
err = bson.UnmarshalExtJSON(migr, true, &cmds)
138138
if err != nil {
139139
return fmt.Errorf("unmarshaling json error: %s", err)
@@ -150,7 +150,7 @@ func (m *Mongo) Run(migration io.Reader) error {
150150
return nil
151151
}
152152

153-
func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bsonx.Doc) error {
153+
func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bson.D) error {
154154
err := m.db.Client().UseSession(ctx, func(sessionContext mongo.SessionContext) error {
155155
if err := sessionContext.StartTransaction(); err != nil {
156156
return &database.Error{OrigErr: err, Err: "failed to start transaction"}
@@ -171,11 +171,11 @@ func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bsonx
171171
return nil
172172
}
173173

174-
func (m *Mongo) executeCommands(ctx context.Context, cmds []bsonx.Doc) error {
174+
func (m *Mongo) executeCommands(ctx context.Context, cmds []bson.D) error {
175175
for _, cmd := range cmds {
176176
err := m.db.RunCommand(ctx, cmd).Err()
177177
if err != nil {
178-
return &database.Error{OrigErr: err, Err: fmt.Sprintf("failed to execute command:%s", cmd.String())}
178+
return &database.Error{OrigErr: err, Err: fmt.Sprintf("failed to execute command:%v", cmd.Map())}
179179
}
180180
}
181181
return nil

0 commit comments

Comments
 (0)