-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
changes.py
308 lines (258 loc) · 11.4 KB
/
changes.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
"""
Support for changes in the database
"""
from buildbot.util import json
import sqlalchemy as sa
from twisted.internet import defer
from buildbot.changes.changes import Change
from buildbot.db import base
class ChangesConnectorComponent(base.DBConnectorComponent):
"""
A DBConnectorComponent to handle getting changes into and out of the
database. An instance is available at C{master.db.changes}.
"""
changeHorizon = 0
"maximum number of changes to keep on hand, or 0 to keep all changes forever"
# TODO: add a threadsafe change cache
def addChange(self, who, files, comments, isdir=0, links=None,
revision=None, when=None, branch=None, category=None,
revlink='', properties={}, repository='', project=''):
"""Add the a Change with the given attributes to the database; returns
a Change instance via a deferred.
@param who: the author of this change
@type branch: unicode string
@param files: a list of filenames that were changed
@type branch: list of unicode strings
@param comments: user comments on the change
@type branch: unicode string
@param isdir: deprecated
@param links: a list of links related to this change, e.g., to web
viewers or review pages
@type links: list of unicode strings
@param revision: the revision identifier for this change
@type revision: unicode string
@param when: when this change occurs
@type when: integer (UNIX epoch time)
@param branch: the branch on which this change took place
@type branch: unicode string
@param category: category for this change (arbitrary use by Buildbot
users)
@type category: unicode string
@param revlink: link to a web view of this revision
@type revlink: unicode string
@param properties: properties to set on this change
@type properties: dictionary with string keys and simple values
(JSON-able)
@param repository: the repository in which this change took place
@type repository: unicode string
@param project: the project this change is a part of
@type project: unicode string
@returns: a L{buildbot.changes.changes.Change} instance via Deferred
"""
assert project is not None, "project must be a string, not None"
assert repository is not None, "repository must be a string, not None"
# first create the change, although with no 'number'
change = Change(who=who, files=files, comments=comments, isdir=isdir,
links=links, revision=revision, when=when, branch=branch,
category=category, revlink=revlink, properties=properties,
repository=repository, project=project)
# then add it to the database and update its '.number'
def thd(conn):
assert change.number is None
# note that in a read-uncommitted database like SQLite this
# transaction does not buy atomicitiy - other database users may
# still come across a change without its links, files, properties,
# etc. That's OK, since we don't announce the change until it's
# all in the database, but beware.
transaction = conn.begin()
ins = self.db.model.changes.insert()
r = conn.execute(ins, dict(
author=change.who,
comments=change.comments,
is_dir=change.isdir,
branch=change.branch,
revision=change.revision,
revlink=change.revlink,
when_timestamp=change.when,
category=change.category,
repository=change.repository,
project=change.project))
change.number = r.inserted_primary_key[0]
if change.links:
ins = self.db.model.change_links.insert()
conn.execute(ins, [
dict(changeid=change.number, link=l)
for l in change.links
])
if change.files:
ins = self.db.model.change_files.insert()
conn.execute(ins, [
dict(changeid=change.number, filename=f)
for f in change.files
])
if change.properties:
ins = self.db.model.change_properties.insert()
conn.execute(ins, [
dict(changeid=change.number,
property_name=k,
property_value=json.dumps(v))
for k,v,s in change.properties.asList()
])
transaction.commit()
return change
d = self.db.pool.do(thd)
return d
def getChangeInstance(self, changeid):
"""
Get a L{buildbot.changes.changes.Change} instance for the given changeid,
or None if no such change exists.
@param changeid: the id of the change instance to fetch
@returns: Change instance via Deferred
"""
assert changeid >= 0
def thd(conn):
# get the row from the 'changes' table
changes_tbl = self.db.model.changes
q = changes_tbl.select(whereclause=(changes_tbl.c.changeid == changeid))
rp = conn.execute(q)
row = rp.fetchone()
if not row:
return None
# and fetch the ancillary data (links, files, properties)
return self._chdict_from_change_row_thd(conn, row)
d = self.db.pool.do(thd)
def make_change(chdict):
if not chdict:
return None
return self._change_from_chdict(chdict)
d.addCallback(make_change)
return d
def getRecentChangeInstances(self, count):
"""
Get a list of the C{count} most recent
L{buildbot.changes.changes.Change} instances, or fewer if that many do
not exist.
@param count: maximum number of instances to return
@returns: list of Change instances via Deferred
"""
def thd(conn):
# get the row from the 'changes' table
changes_tbl = self.db.model.changes
q = changes_tbl.select(
order_by=[sa.desc(changes_tbl.c.changeid)],
limit=count)
rp = conn.execute(q)
changes = []
for row in rp:
# note that this does *three* extra queries per row!
changes.append(self._chdict_from_change_row_thd(conn, row))
rp.close()
return reversed(changes)
d = self.db.pool.do(thd)
def make_changes(chdict_list):
return [ self._change_from_chdict(chdict)
for chdict in chdict_list ]
d.addCallback(make_changes)
return d
def getLatestChangeid(self):
"""
Get the most-recently-assigned changeid, or None if there are no
changes at all.
@returns: changeid via Deferred
"""
def thd(conn):
changes_tbl = self.db.model.changes
q = sa.select([ changes_tbl.c.changeid ],
order_by=sa.desc(changes_tbl.c.changeid),
limit=1)
return conn.scalar(q)
d = self.db.pool.do(thd)
return d
def setChangeHorizon(self, changeHorizon): # TODO: remove
"this method should go away"
self.changeHorizon = changeHorizon
# cache management
def _flush_cache(self):
pass # TODO
# utility methods
def pruneChanges(self, changeHorizon):
if not changeHorizon:
return defer.succeed(None)
def thd(conn):
changes_tbl = self.db.model.changes
# First, get the list of changes to delete. This could be written
# as a subquery but then that subquery would be run for every
# table, which is very inefficient; also, MySQL's subquery support
# leaves much to be desired, and doesn't support this particular
# form.
q = sa.select([changes_tbl.c.changeid],
order_by=[sa.desc(changes_tbl.c.changeid)],
offset=changeHorizon)
res = conn.execute(q)
ids_to_delete = [ r.changeid for r in res ]
# and delete from all relevant tables, in dependency order
for table_name in ('scheduler_changes', 'sourcestamp_changes',
'change_files', 'change_links',
'change_properties', 'changes'):
table = self.db.model.metadata.tables[table_name]
conn.execute(
table.delete(table.c.changeid.in_(ids_to_delete)))
return self.db.pool.do(thd)
def _chdict_from_change_row_thd(self, conn, ch_row):
# This method must be run in a db.pool thread, and returns a chdict
# (which can be used to construct a Change object), given a row from
# the 'changes' table
change_links_tbl = self.db.model.change_links
change_files_tbl = self.db.model.change_files
change_properties_tbl = self.db.model.change_properties
chdict = dict(
number=ch_row.changeid,
who=ch_row.author,
files=[], # see below
comments=ch_row.comments,
isdir=ch_row.is_dir,
links=[], # see below
revision=ch_row.revision,
when=ch_row.when_timestamp,
branch=ch_row.branch,
category=ch_row.category,
revlink=ch_row.revlink,
properties={}, # see below
repository=ch_row.repository,
project=ch_row.project)
query = change_links_tbl.select(
whereclause=(change_links_tbl.c.changeid == ch_row.changeid))
rows = conn.execute(query)
for r in rows:
chdict['links'].append(r.link)
query = change_files_tbl.select(
whereclause=(change_files_tbl.c.changeid == ch_row.changeid))
rows = conn.execute(query)
for r in rows:
chdict['files'].append(r.filename)
query = change_properties_tbl.select(
whereclause=(change_properties_tbl.c.changeid == ch_row.changeid))
rows = conn.execute(query)
for r in rows:
chdict['properties'][r.property_name] = json.loads(r.property_value)
return chdict
def _change_from_chdict(self, chdict):
# create a Change object, given a chdict
changeid = chdict.pop('number')
c = Change(**chdict)
c.number = changeid
return c