Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add state handling to the db

  • Loading branch information...
commit dbf971a69a9720789bd17be40ddebe54a2209962 1 parent 842fc87
Dustin J. Mitchell authored
35 highscore/db/connector.py
View
@@ -13,6 +13,8 @@
#
# Copyright Buildbot Team Members
+import sqlalchemy as sa
+import json
from twisted.python import log
from twisted.application import service
from highscore.db import enginestrategy, pool, model
@@ -41,3 +43,36 @@ def check_current(res):
log.msg("upgrading database")
return self.model.upgrade()
return d
+
+ # state convenience methods
+ #
+ # Note that state handling is *not* parallelizable!
+
+ def getState(self, name):
+ def thd(conn):
+ tbl = self.model.state
+ res = conn.execute(
+ sa.select([ tbl.c.value ], tbl.c.name == name))
+ return res.fetchone()
+ d = self.pool.do(thd)
+ @d.addCallback
+ def un_json(row):
+ if row:
+ return json.loads(row.value)
+ return d
+
+ def setState(self, name, value):
+ value_json = json.dumps(value)
+ def thd(conn):
+ res = conn.execute(
+ self.model.state.update(
+ self.model.state.c.name == name),
+ value=value_json)
+ if res.rowcount:
+ return
+ conn.execute(
+ self.model.state.insert(),
+ name=name,
+ value=value_json)
+ return self.pool.do(thd)
+
26 highscore/db/migrate/versions/003_state.py
View
@@ -0,0 +1,26 @@
+# 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
+
+def upgrade(migrate_engine):
+ metadata = sa.MetaData()
+ metadata.bind = migrate_engine
+
+ state = sa.Table('state', metadata,
+ sa.Column('name', sa.String, primary_key=True),
+ sa.Column('value', sa.String, nullable=False),
+ )
+ state.create()
6 highscore/db/model.py
View
@@ -66,6 +66,12 @@ def __init__(self, db):
)
sa.Index('points_userid', points.c.userid)
+ # storage for arbitrary small state
+ state = sa.Table('state', metadata,
+ sa.Column('name', sa.String, primary_key=True),
+ sa.Column('value', sa.String, nullable=False),
+ )
+
#
# migration support
#
Please sign in to comment.
Something went wrong with that request. Please try again.