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

     1  package patch
     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  const (
    11  	Collection   = "patches"
    12  	GridFSPrefix = "patchfiles"
    13  )
    14  
    15  // BSON fields for the patches
    16  var (
    17  	IdKey            = bsonutil.MustHaveTag(Patch{}, "Id")
    18  	DescriptionKey   = bsonutil.MustHaveTag(Patch{}, "Description")
    19  	ProjectKey       = bsonutil.MustHaveTag(Patch{}, "Project")
    20  	GithashKey       = bsonutil.MustHaveTag(Patch{}, "Githash")
    21  	AuthorKey        = bsonutil.MustHaveTag(Patch{}, "Author")
    22  	NumberKey        = bsonutil.MustHaveTag(Patch{}, "PatchNumber")
    23  	VersionKey       = bsonutil.MustHaveTag(Patch{}, "Version")
    24  	StatusKey        = bsonutil.MustHaveTag(Patch{}, "Status")
    25  	CreateTimeKey    = bsonutil.MustHaveTag(Patch{}, "CreateTime")
    26  	StartTimeKey     = bsonutil.MustHaveTag(Patch{}, "StartTime")
    27  	FinishTimeKey    = bsonutil.MustHaveTag(Patch{}, "FinishTime")
    28  	BuildVariantsKey = bsonutil.MustHaveTag(Patch{}, "BuildVariants")
    29  	TasksKey         = bsonutil.MustHaveTag(Patch{}, "Tasks")
    30  	VariantsTasksKey = bsonutil.MustHaveTag(Patch{}, "VariantsTasks")
    31  	PatchesKey       = bsonutil.MustHaveTag(Patch{}, "Patches")
    32  	ActivatedKey     = bsonutil.MustHaveTag(Patch{}, "Activated")
    33  	PatchedConfigKey = bsonutil.MustHaveTag(Patch{}, "PatchedConfig")
    34  
    35  	// BSON fields for the module patch struct
    36  	ModulePatchNameKey    = bsonutil.MustHaveTag(ModulePatch{}, "ModuleName")
    37  	ModulePatchGithashKey = bsonutil.MustHaveTag(ModulePatch{}, "Githash")
    38  	ModulePatchSetKey     = bsonutil.MustHaveTag(ModulePatch{}, "PatchSet")
    39  
    40  	// BSON fields for the patch set struct
    41  	PatchSetPatchKey   = bsonutil.MustHaveTag(PatchSet{}, "Patch")
    42  	PatchSetSummaryKey = bsonutil.MustHaveTag(PatchSet{}, "Summary")
    43  
    44  	// BSON fields for the git patch summary struct
    45  	GitSummaryNameKey      = bsonutil.MustHaveTag(Summary{}, "Name")
    46  	GitSummaryAdditionsKey = bsonutil.MustHaveTag(Summary{}, "Additions")
    47  	GitSummaryDeletionsKey = bsonutil.MustHaveTag(Summary{}, "Deletions")
    48  )
    49  
    50  // Query Validation
    51  
    52  // IsValidId returns whether the supplied Id is a valid patch doc id (BSON ObjectId).
    53  func IsValidId(id string) bool {
    54  	return bson.IsObjectIdHex(id)
    55  }
    56  
    57  // NewId constructs a valid patch Id from the given hex string.
    58  func NewId(id string) bson.ObjectId {
    59  	return bson.ObjectIdHex(id)
    60  }
    61  
    62  // Queries
    63  
    64  // ById produces a query to return the patch with the given _id.
    65  func ById(id bson.ObjectId) db.Q {
    66  	return db.Query(bson.D{{IdKey, id}})
    67  }
    68  
    69  // ByProject produces a query that returns projects with the given identifier.
    70  func ByProject(project string) db.Q {
    71  	return db.Query(bson.D{{ProjectKey, project}})
    72  }
    73  
    74  // ByUser produces a query that returns patches by the given user.
    75  func ByUser(user string) db.Q {
    76  	return db.Query(bson.D{{AuthorKey, user}})
    77  }
    78  
    79  // ByUserProjectAndGitspec produces a query that returns patches by the given
    80  // patch author, project, and gitspec.
    81  func ByUserProjectAndGitspec(user string, project string, gitspec string) db.Q {
    82  	return db.Query(bson.M{
    83  		AuthorKey:  user,
    84  		ProjectKey: project,
    85  		GithashKey: gitspec,
    86  	})
    87  }
    88  
    89  // ByVersion produces a query that returns the patch for a given version.
    90  func ByVersion(version string) db.Q {
    91  	return db.Query(bson.D{{VersionKey, version}})
    92  }
    93  
    94  // ByVersion produces a query that returns the patch for a given version.
    95  func ByVersions(versions []string) db.Q {
    96  	return db.Query(bson.M{VersionKey: bson.M{"$in": versions}})
    97  }
    98  
    99  // ExcludePatchDiff is a projection that excludes diff data, helping load times.
   100  var ExcludePatchDiff = bson.D{
   101  	{PatchesKey + "." + ModulePatchSetKey + "." + PatchSetPatchKey, 0},
   102  }
   103  
   104  // Query Functions
   105  
   106  // FindOne runs a patch query, returning one patch.
   107  func FindOne(query db.Q) (*Patch, error) {
   108  	patch := &Patch{}
   109  	err := db.FindOneQ(Collection, query, patch)
   110  	if err == mgo.ErrNotFound {
   111  		return nil, nil
   112  	}
   113  	return patch, err
   114  }
   115  
   116  // Find runs a patch query, returning all patches that satisfy the query.
   117  func Find(query db.Q) ([]Patch, error) {
   118  	patches := []Patch{}
   119  	err := db.FindAllQ(Collection, query, &patches)
   120  	if err == mgo.ErrNotFound {
   121  		return nil, nil
   122  	}
   123  	return patches, err
   124  }
   125  
   126  // Count returns the number of patches that satisfy the given query.
   127  func Count(query db.Q) (int, error) {
   128  	return db.CountQ(Collection, query)
   129  }
   130  
   131  // Remove removes all patch documents that satisfy the query.
   132  func Remove(query db.Q) error {
   133  	return db.RemoveAllQ(Collection, query)
   134  }
   135  
   136  // UpdateAll runs an update on all patch documents.
   137  func UpdateAll(query interface{}, update interface{}) (info *mgo.ChangeInfo, err error) {
   138  	return db.UpdateAll(Collection, query, update)
   139  }
   140  
   141  // UpdateOne runs an update on a single patch document.
   142  func UpdateOne(query interface{}, update interface{}) error {
   143  	return db.Update(Collection, query, update)
   144  }