Package buildbot :: Package db :: Package schema :: Module manager
[frames] | no frames]

Source Code for Module buildbot.db.schema.manager

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  from twisted.python import reflect 
17   
18  # note that schema modules are not loaded unless an upgrade is taking place 
19   
20  CURRENT_VERSION = 6 
21   
22 -class DBSchemaManager(object):
23 """ 24 This class is responsible for managing the database schema and upgrading it 25 as necessary. This includes both the *actual* database and the old pickle 26 database, as migrations move data between the two. 27 28 Note that this class is *entirely synchronous*! Performing any other operations 29 while changing the schema is just asking for trouble. 30 """
31 - def __init__(self, spec, basedir):
32 self.spec = spec 33 self.basedir = basedir 34 self.dbapi = self.spec.get_dbapi()
35
36 - def get_db_version(self, conn=None):
37 """ 38 Get the current schema version for this database 39 """ 40 close_conn = False 41 if not conn: 42 conn = self.spec.get_sync_connection() 43 close_conn = True 44 c = conn.cursor() 45 try: 46 try: 47 c.execute("SELECT version FROM version") 48 rows = c.fetchall() 49 assert len(rows) == 1, "%i rows in version table! (should only be 1)" % len(rows) 50 return rows[0][0] 51 except (self.dbapi.OperationalError, self.dbapi.ProgrammingError): 52 # no version table = version 0 53 return 0 54 finally: 55 if close_conn: 56 conn.close()
57
58 - def get_current_version(self):
59 """ 60 Get the current db version for this release of buildbot 61 """ 62 return CURRENT_VERSION
63
64 - def is_current(self):
65 """ 66 Is this database current? 67 """ 68 return self.get_db_version() == self.get_current_version()
69
70 - def upgrade(self, quiet=False):
71 """ 72 Upgrade this database to the current version 73 """ 74 conn = self.spec.get_sync_connection() 75 try: 76 while self.get_db_version() < self.get_current_version(): 77 next_version = self.get_db_version() + 1 78 next_version_module = reflect.namedModule("buildbot.db.schema.v%d" % next_version) 79 upg = next_version_module.Upgrader(self.dbapi, conn, self.basedir, quiet) 80 upg.upgrade() 81 conn.commit() 82 assert self.get_db_version() == next_version 83 finally: 84 conn.close()
85