aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/db/schedulers.py
blob: 0e845da3078b946b806ac27dc73609c3d0c9bee9 (plain)
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
# 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

import sqlalchemy as sa
import sqlalchemy.exc
from buildbot.db import base

class SchedulersConnectorComponent(base.DBConnectorComponent):
    # Documentation is in developer/database.rst

    def classifyChanges(self, objectid, classifications):
        def thd(conn):
            transaction = conn.begin()
            tbl = self.db.model.scheduler_changes
            ins_q = tbl.insert()
            upd_q = tbl.update(
                    ((tbl.c.objectid == objectid)
                    & (tbl.c.changeid == sa.bindparam('wc_changeid'))))
            for changeid, important in classifications.items():
                # convert the 'important' value into an integer, since that
                # is the column type
                imp_int = important and 1 or 0
                try:
                    conn.execute(ins_q,
                            objectid=objectid,
                            changeid=changeid,
                            important=imp_int)
                except (sqlalchemy.exc.ProgrammingError,
                        sqlalchemy.exc.IntegrityError):
                    # insert failed, so try an update
                    conn.execute(upd_q,
                            wc_changeid=changeid,
                            important=imp_int)

            transaction.commit()
        return self.db.pool.do(thd)

    def flushChangeClassifications(self, objectid, less_than=None):
        def thd(conn):
            sch_ch_tbl = self.db.model.scheduler_changes
            wc = (sch_ch_tbl.c.objectid == objectid)
            if less_than is not None:
                wc = wc & (sch_ch_tbl.c.changeid < less_than)
            q = sch_ch_tbl.delete(whereclause=wc)
            conn.execute(q)
        return self.db.pool.do(thd)

    class Thunk: pass
    def getChangeClassifications(self, objectid, branch=Thunk,
                                 repository=Thunk, project=Thunk,
                                 codebase=Thunk):
        def thd(conn):
            sch_ch_tbl = self.db.model.scheduler_changes
            ch_tbl = self.db.model.changes

            wc = (sch_ch_tbl.c.objectid == objectid)

            # may need to filter further based on branch, etc
            extra_wheres = []
            if branch is not self.Thunk:
                extra_wheres.append(ch_tbl.c.branch == branch)
            if repository is not self.Thunk:
                extra_wheres.append(ch_tbl.c.repository == repository)
            if project is not self.Thunk:
                extra_wheres.append(ch_tbl.c.project == project)
            if codebase is not self.Thunk:
                extra_wheres.append(ch_tbl.c.codebase == codebase)

            # if we need to filter further append those, as well as a join
            # on changeid (but just once for that one)
            if extra_wheres:
                wc &= (sch_ch_tbl.c.changeid == ch_tbl.c.changeid)
                for w in extra_wheres:
                    wc &= w

            q = sa.select(
                [ sch_ch_tbl.c.changeid, sch_ch_tbl.c.important ],
                whereclause=wc)
            return dict([ (r.changeid, [False,True][r.important])
                          for r in conn.execute(q) ])
        return self.db.pool.do(thd)