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