github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/manifest/db.go (about)

     1  package manifest
     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  	IdKey               = bsonutil.MustHaveTag(Manifest{}, "Id")
    13  	ManifestRevisionKey = bsonutil.MustHaveTag(Manifest{}, "Revision")
    14  	ProjectNameKey      = bsonutil.MustHaveTag(Manifest{}, "ProjectName")
    15  	ModulesKey          = bsonutil.MustHaveTag(Manifest{}, "Modules")
    16  	ManifestBranchKey   = bsonutil.MustHaveTag(Manifest{}, "Branch")
    17  	ModuleBranchKey     = bsonutil.MustHaveTag(Module{}, "Branch")
    18  	ModuleRevisionKey   = bsonutil.MustHaveTag(Module{}, "Revision")
    19  	OwnerKey            = bsonutil.MustHaveTag(Module{}, "Owner")
    20  	UrlKey              = bsonutil.MustHaveTag(Module{}, "URL")
    21  )
    22  
    23  // FindOne gets one Manifest for the given query.
    24  func FindOne(query db.Q) (*Manifest, error) {
    25  	m := &Manifest{}
    26  	err := db.FindOneQ(Collection, query, m)
    27  	if err == mgo.ErrNotFound {
    28  		return nil, nil
    29  	}
    30  	return m, err
    31  }
    32  
    33  // TryInsert writes the manifest to the database if possible.
    34  // If the document already exists, it returns true and the error
    35  // If it does not it will return false and the error
    36  func (m *Manifest) TryInsert() (bool, error) {
    37  	err := db.Insert(Collection, m)
    38  	if mgo.IsDup(err) {
    39  		return true, nil
    40  	}
    41  	return false, err
    42  }
    43  
    44  // ById returns a query that contains an Id selector on the string, id.
    45  func ById(id string) db.Q {
    46  	return db.Query(bson.D{{IdKey, id}})
    47  }