Replaced bsonx.Doc with bson.D

This commit is contained in:
DBobrov 2019-01-04 22:01:21 +03:00
parent 309edb6da6
commit 436b81b357
2 changed files with 7 additions and 7 deletions

View File

@ -12,7 +12,7 @@
| URL Query | WithInstance Config | Description |
|------------|---------------------|-------------|
| `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
| `x-transaction-mode` | `TransactionMode` | If set to `true` wrap commands in [transaction](https://docs.mongodb.com/manual/core/transactions). Available only for replica set. |
| `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|
| `dbname` | `DatabaseName` | The name of the database to connect to |
| `user` | | The user to sign in as. Can be omitted |
| `password` | | The user's password. Can be omitted |

View File

@ -6,12 +6,12 @@ import (
"io"
"io/ioutil"
"net/url"
"strconv"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/mongodb/mongo-go-driver/x/bsonx"
"github.com/mongodb/mongo-go-driver/x/network/connstring"
)
@ -80,7 +80,7 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
migrationsCollection = DefaultMigrationsCollection
}
transactionMode := purl.Query().Get("x-transaction-mode") == "true"
transactionMode, _ := strconv.ParseBool(purl.Query().Get("x-transaction-mode"))
q := migrate.FilterCustomQuery(purl)
q.Scheme = "mongodb"
@ -133,7 +133,7 @@ func (m *Mongo) Run(migration io.Reader) error {
if err != nil {
return err
}
var cmds []bsonx.Doc
var cmds []bson.D
err = bson.UnmarshalExtJSON(migr, true, &cmds)
if err != nil {
return fmt.Errorf("unmarshaling json error: %s", err)
@ -150,7 +150,7 @@ func (m *Mongo) Run(migration io.Reader) error {
return nil
}
func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bsonx.Doc) error {
func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bson.D) error {
err := m.db.Client().UseSession(ctx, func(sessionContext mongo.SessionContext) error {
if err := sessionContext.StartTransaction(); err != nil {
return &database.Error{OrigErr: err, Err: "failed to start transaction"}
@ -171,11 +171,11 @@ func (m *Mongo) executeCommandsWithTransaction(ctx context.Context, cmds []bsonx
return nil
}
func (m *Mongo) executeCommands(ctx context.Context, cmds []bsonx.Doc) error {
func (m *Mongo) executeCommands(ctx context.Context, cmds []bson.D) error {
for _, cmd := range cmds {
err := m.db.RunCommand(ctx, cmd).Err()
if err != nil {
return &database.Error{OrigErr: err, Err: fmt.Sprintf("failed to execute command:%s", cmd.String())}
return &database.Error{OrigErr: err, Err: fmt.Sprintf("failed to execute command:%v", cmd.Map())}
}
}
return nil