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

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/evergreen-ci/evergreen"
     7  	"github.com/evergreen-ci/evergreen/db"
     8  	"github.com/evergreen-ci/evergreen/db/bsonutil"
     9  	"gopkg.in/mgo.v2"
    10  	"gopkg.in/mgo.v2/bson"
    11  )
    12  
    13  const (
    14  	Collection = "versions"
    15  )
    16  
    17  var (
    18  	// bson fields for the version struct
    19  	IdKey                  = bsonutil.MustHaveTag(Version{}, "Id")
    20  	CreateTimeKey          = bsonutil.MustHaveTag(Version{}, "CreateTime")
    21  	StartTimeKey           = bsonutil.MustHaveTag(Version{}, "StartTime")
    22  	FinishTimeKey          = bsonutil.MustHaveTag(Version{}, "FinishTime")
    23  	RevisionKey            = bsonutil.MustHaveTag(Version{}, "Revision")
    24  	AuthorKey              = bsonutil.MustHaveTag(Version{}, "Author")
    25  	AuthorEmailKey         = bsonutil.MustHaveTag(Version{}, "AuthorEmail")
    26  	MessageKey             = bsonutil.MustHaveTag(Version{}, "Message")
    27  	StatusKey              = bsonutil.MustHaveTag(Version{}, "Status")
    28  	BuildIdsKey            = bsonutil.MustHaveTag(Version{}, "BuildIds")
    29  	BuildVariantsKey       = bsonutil.MustHaveTag(Version{}, "BuildVariants")
    30  	RevisionOrderNumberKey = bsonutil.MustHaveTag(Version{}, "RevisionOrderNumber")
    31  	RequesterKey           = bsonutil.MustHaveTag(Version{}, "Requester")
    32  	ConfigKey              = bsonutil.MustHaveTag(Version{}, "Config")
    33  	IgnoredKey             = bsonutil.MustHaveTag(Version{}, "Ignored")
    34  	OwnerNameKey           = bsonutil.MustHaveTag(Version{}, "Owner")
    35  	RepoKey                = bsonutil.MustHaveTag(Version{}, "Repo")
    36  	ProjectNameKey         = bsonutil.MustHaveTag(Version{}, "Branch")
    37  	RepoKindKey            = bsonutil.MustHaveTag(Version{}, "RepoKind")
    38  	ErrorsKey              = bsonutil.MustHaveTag(Version{}, "Errors")
    39  	WarningsKey            = bsonutil.MustHaveTag(Version{}, "Warnings")
    40  	IdentifierKey          = bsonutil.MustHaveTag(Version{}, "Identifier")
    41  	RemoteKey              = bsonutil.MustHaveTag(Version{}, "Remote")
    42  	RemoteURLKey           = bsonutil.MustHaveTag(Version{}, "RemotePath")
    43  )
    44  
    45  // ById returns a db.Q object which will filter on {_id : <the id param>}
    46  func ById(id string) db.Q {
    47  	return db.Query(bson.M{IdKey: id})
    48  }
    49  
    50  // ByIds returns a db.Q object which will find any versions whose _id appears in the given list.
    51  func ByIds(ids []string) db.Q {
    52  	return db.Query(bson.M{IdKey: bson.M{"$in": ids}})
    53  }
    54  
    55  // All is a query for all versions.
    56  var All = db.Query(bson.D{})
    57  
    58  // ByLastKnownGoodConfig filters on versions with valid (i.e., have no errors) config for the given
    59  // project. Does not apply a limit, so should generally be used with a FindOne.
    60  func ByLastKnownGoodConfig(projectId string) db.Q {
    61  	return db.Query(
    62  		bson.M{
    63  			IdentifierKey: projectId,
    64  			RequesterKey:  evergreen.RepotrackerVersionRequester,
    65  			ErrorsKey: bson.M{
    66  				"$exists": false,
    67  			},
    68  		}).Sort([]string{"-" + RevisionOrderNumberKey})
    69  }
    70  
    71  // ByProjectIdAndRevision finds non-patch versions for the given project and revision.
    72  func ByProjectIdAndRevision(projectId, revision string) db.Q {
    73  	return db.Query(
    74  		bson.M{
    75  			IdentifierKey: projectId,
    76  			RevisionKey:   revision,
    77  			RequesterKey:  evergreen.RepotrackerVersionRequester,
    78  		})
    79  }
    80  
    81  func ByProjectIdAndRevisionPrefix(projectId, revisionPrefix string) db.Q {
    82  	lengthHash := (40 - len(revisionPrefix))
    83  	return db.Query(
    84  		bson.M{
    85  			IdentifierKey: projectId,
    86  			RevisionKey:   bson.M{"$regex": fmt.Sprintf("^%s[0-9a-f]{%d}$", revisionPrefix, lengthHash)},
    87  			RequesterKey:  evergreen.RepotrackerVersionRequester,
    88  		})
    89  }
    90  
    91  // ByProjectIdAndOrder finds non-patch versions for the given project with revision
    92  // order numbers less than or equal to revisionOrderNumber.
    93  func ByProjectIdAndOrder(projectId string, revisionOrderNumber int) db.Q {
    94  	return db.Query(
    95  		bson.M{
    96  			IdentifierKey:          projectId,
    97  			RevisionOrderNumberKey: bson.M{"$lte": revisionOrderNumber},
    98  			RequesterKey:           evergreen.RepotrackerVersionRequester,
    99  		})
   100  }
   101  
   102  // ByLastVariantActivation finds the most recent non-patch, non-ignored
   103  // versions in a project that have a particular variant activated.
   104  func ByLastVariantActivation(projectId, variant string) db.Q {
   105  	return db.Query(
   106  		bson.M{
   107  			IdentifierKey: projectId,
   108  			// TODO make this `Ignored: false` after EVG-764  has time to burn in
   109  			IgnoredKey:   bson.M{"$ne": true},
   110  			RequesterKey: evergreen.RepotrackerVersionRequester,
   111  			BuildVariantsKey: bson.M{
   112  				"$elemMatch": bson.M{
   113  					BuildStatusActivatedKey: true,
   114  					BuildStatusVariantKey:   variant,
   115  				},
   116  			},
   117  		},
   118  	).Sort([]string{"-" + RevisionOrderNumberKey})
   119  }
   120  
   121  // ByProjectId finds all non-patch versions within a project.
   122  func ByProjectId(projectId string) db.Q {
   123  	return db.Query(
   124  		bson.M{
   125  			IdentifierKey: projectId,
   126  			RequesterKey:  evergreen.RepotrackerVersionRequester,
   127  		})
   128  }
   129  
   130  // ByProjectId finds all versions within a project, ordered by most recently created to oldest.
   131  // The requester controls if it should search patch or non-patch versions.
   132  func ByMostRecentForRequester(projectId, requester string) db.Q {
   133  	return db.Query(
   134  		bson.M{
   135  			RequesterKey:  requester,
   136  			IdentifierKey: projectId,
   137  		},
   138  	).Sort([]string{"-" + RevisionOrderNumberKey})
   139  }
   140  
   141  // ByMostRecentNonignored finds all non-ignored versions within a project,
   142  // ordered by most recently created to oldest.
   143  func ByMostRecentNonignored(projectId string) db.Q {
   144  	return db.Query(
   145  		bson.M{
   146  			RequesterKey:  evergreen.RepotrackerVersionRequester,
   147  			IdentifierKey: projectId,
   148  			IgnoredKey:    bson.M{"$ne": true},
   149  		},
   150  	).Sort([]string{"-" + RevisionOrderNumberKey})
   151  }
   152  
   153  // BaseVersionFromPatch finds the base version for a patch version.
   154  func BaseVersionFromPatch(projectId, revision string) db.Q {
   155  	return db.Query(
   156  		bson.M{
   157  			IdentifierKey: projectId,
   158  			RevisionKey:   revision,
   159  			RequesterKey:  evergreen.RepotrackerVersionRequester,
   160  		})
   161  }
   162  
   163  func FindOne(query db.Q) (*Version, error) {
   164  	version := &Version{}
   165  	err := db.FindOneQ(Collection, query, version)
   166  	if err == mgo.ErrNotFound {
   167  		return nil, nil
   168  	}
   169  	return version, err
   170  }
   171  
   172  func Find(query db.Q) ([]Version, error) {
   173  	versions := []Version{}
   174  	err := db.FindAllQ(Collection, query, &versions)
   175  	if err == mgo.ErrNotFound {
   176  		return nil, nil
   177  	}
   178  	return versions, err
   179  }
   180  
   181  // Count returns the number of hosts that satisfy the given query.
   182  func Count(query db.Q) (int, error) {
   183  	return db.CountQ(Collection, query)
   184  }
   185  
   186  // UpdateOne updates one version.
   187  func UpdateOne(query interface{}, update interface{}) error {
   188  	return db.Update(
   189  		Collection,
   190  		query,
   191  		update,
   192  	)
   193  }