Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.

Commit 0e76be3

Browse files
authored
Merge pull request #466 from Nydauron/mongo-driver-upgrade
Mongo driver upgrade
2 parents 3f21a20 + dbae428 commit 0e76be3

File tree

40 files changed

+895
-453
lines changed

40 files changed

+895
-453
lines changed

common/database/database.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
package database
22

3+
import (
4+
"context"
5+
6+
"go.mongodb.org/mongo-driver/bson"
7+
"go.mongodb.org/mongo-driver/mongo"
8+
)
9+
310
/*
411
Database interface exposing the methods necessary to querying, inserting, updating, upserting, and removing records
512
*/
613
type Database interface {
714
Connect(host string) error
815
Close()
9-
FindOne(collection_name string, query interface{}, result interface{}) error
10-
FindAll(collection_name string, query interface{}, result interface{}) error
11-
FindAllSorted(collection_name string, query interface{}, sort_fields []SortField, result interface{}) error
12-
RemoveOne(collection_name string, query interface{}) error
13-
RemoveAll(collection_name string, query interface{}) (*ChangeResults, error)
14-
Insert(collection_name string, item interface{}) error
15-
Upsert(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)
16-
Update(collection_name string, selector interface{}, update interface{}) error
17-
UpdateAll(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)
18-
DropDatabase() error
19-
GetStats(collection_name string, fields []string) (map[string]interface{}, error)
16+
GetRaw() *mongo.Client
17+
StartSession() (*mongo.Session, error)
18+
GetNewContext() (context.Context, context.CancelFunc)
19+
FindOne(collection_name string, query interface{}, result interface{}, session *mongo.SessionContext) error
20+
FindOneAndDelete(collection_name string, query interface{}, result interface{}, session *mongo.SessionContext) error
21+
FindOneAndUpdate(collection_name string, query interface{}, update interface{}, result interface{}, return_new_doc bool, upsert bool, session *mongo.SessionContext) error
22+
FindOneAndReplace(collection_name string, query interface{}, update interface{}, result interface{}, return_new_doc bool, upsert bool, session *mongo.SessionContext) error
23+
FindAll(collection_name string, query interface{}, result interface{}, session *mongo.SessionContext) error
24+
FindAllSorted(collection_name string, query interface{}, sort_fields bson.D, result interface{}, session *mongo.SessionContext) error
25+
RemoveOne(collection_name string, query interface{}, session *mongo.SessionContext) error
26+
RemoveAll(collection_name string, query interface{}, session *mongo.SessionContext) (*ChangeResults, error)
27+
Insert(collection_name string, item interface{}, session *mongo.SessionContext) error
28+
Upsert(collection_name string, selector interface{}, update interface{}, session *mongo.SessionContext) (*ChangeResults, error)
29+
Update(collection_name string, selector interface{}, update interface{}, session *mongo.SessionContext) error
30+
UpdateAll(collection_name string, selector interface{}, update interface{}, session *mongo.SessionContext) (*ChangeResults, error)
31+
Replace(collection_name string, selector interface{}, update interface{}, upsert bool, session *mongo.SessionContext) error
32+
DropDatabase(session *mongo.SessionContext) error
33+
GetStats(collection_name string, fields []string, session *mongo.SessionContext) (map[string]interface{}, error)
2034
}
2135

2236
/*

common/database/db_errors.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,48 @@ package database
22

33
import (
44
"errors"
5-
"gopkg.in/mgo.v2"
5+
"fmt"
6+
7+
"go.mongodb.org/mongo-driver/mongo"
68
)
79

810
var (
9-
ErrNotFound = errors.New("Error: NOT_FOUND")
10-
ErrConnection = errors.New("Error: CONNECTION_FAILED")
11-
ErrUnknown = errors.New("Error: UNKNOWN")
11+
ErrNotFound = errors.New("Error: NOT_FOUND")
12+
ErrDuplicateKey = errors.New("Error: DUPLICATE_KEY")
13+
ErrConnection = errors.New("Error: CONNECTION_FAILED")
14+
ErrDisconnected = errors.New("Error: CLIENT_DISCONNECTED")
15+
ErrUnknown = errors.New("Error: UNKNOWN")
16+
ErrNilPassedToCRUD = errors.New("Error: NIL_PASSED_TO_CRUD") // this is an error the user should never get and is merely here for debugging
1217
)
1318

1419
/*
1520
Converts internal mgo errors to external presented errors
1621
*/
1722
func convertMgoError(err error) error {
18-
if err == nil {
23+
// TODO: Need to try and encompass all possible errors
24+
switch err {
25+
case nil:
1926
return nil
20-
} else if err == mgo.ErrNotFound {
27+
case mongo.ErrClientDisconnected:
28+
return ErrDisconnected
29+
case mongo.ErrNilDocument:
30+
return ErrNilPassedToCRUD
31+
case mongo.ErrNoDocuments:
2132
return ErrNotFound
33+
default:
34+
{
35+
var e mongo.WriteException
36+
if errors.As(err, &e) {
37+
for _, we := range e.WriteErrors {
38+
if we.Code == 11000 { // Error code for duplicate key error
39+
return ErrDuplicateKey
40+
}
41+
}
42+
}
43+
}
44+
fmt.Println("Unhandled error: ", err)
45+
// TODO: How can we embed error information into here?
46+
// It'll help a lot if an unexpected error comes up
47+
return ErrUnknown
2248
}
23-
24-
return ErrUnknown
2549
}

0 commit comments

Comments
 (0)