github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/model/artifact/db.go (about) 1 package artifact 2 3 import ( 4 "github.com/evergreen-ci/evergreen/db" 5 "github.com/evergreen-ci/evergreen/db/bsonutil" 6 "gopkg.in/mgo.v2" 7 "gopkg.in/mgo.v2/bson" 8 ) 9 10 var ( 11 // BSON fields for artifact file structs 12 TaskIdKey = bsonutil.MustHaveTag(Entry{}, "TaskId") 13 TaskNameKey = bsonutil.MustHaveTag(Entry{}, "TaskDisplayName") 14 BuildIdKey = bsonutil.MustHaveTag(Entry{}, "BuildId") 15 FilesKey = bsonutil.MustHaveTag(Entry{}, "Files") 16 NameKey = bsonutil.MustHaveTag(File{}, "Name") 17 LinkKey = bsonutil.MustHaveTag(File{}, "Link") 18 ) 19 20 // === Queries === 21 22 // ByTaskId returns a query for entries with the given Task Id 23 func ByTaskId(id string) db.Q { 24 return db.Query(bson.D{{TaskIdKey, id}}) 25 } 26 27 // ByBuildId returns all entries with the given Build Id, sorted by Task name 28 func ByBuildId(id string) db.Q { 29 return db.Query(bson.D{{BuildIdKey, id}}).Sort([]string{TaskNameKey}) 30 } 31 32 // === DB Logic === 33 34 // Upsert updates the files entry in the db if an entry already exists, 35 // overwriting the existing file data. If no entry exists, one is created 36 func (e Entry) Upsert() error { 37 for _, file := range e.Files { 38 _, err := db.Upsert( 39 Collection, 40 bson.M{ 41 TaskIdKey: e.TaskId, 42 TaskNameKey: e.TaskDisplayName, 43 BuildIdKey: e.BuildId, 44 }, 45 bson.M{ 46 "$addToSet": bson.M{ 47 FilesKey: file, 48 }, 49 }, 50 ) 51 if err != nil { 52 return err 53 } 54 } 55 return nil 56 } 57 58 // FindOne ets one Entry for the given query 59 func FindOne(query db.Q) (*Entry, error) { 60 entry := &Entry{} 61 err := db.FindOneQ(Collection, query, entry) 62 if err == mgo.ErrNotFound { 63 return nil, nil 64 } 65 return entry, err 66 } 67 68 // FindAll gets every Entry for the given query 69 func FindAll(query db.Q) ([]Entry, error) { 70 entries := []Entry{} 71 err := db.FindAllQ(Collection, query, &entries) 72 return entries, err 73 }