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

Source Code for Module buildbot.db.schema.manager

 1  from twisted.python import reflect 
 2   
 3  # note that schema modules are not loaded unless an upgrade is taking place 
 4   
 5  CURRENT_VERSION = 6 
 6   
7 -class DBSchemaManager(object):
8 """ 9 This class is responsible for managing the database schema and upgrading it 10 as necessary. This includes both the *actual* database and the old pickle 11 database, as migrations move data between the two. 12 13 Note that this class is *entirely synchronous*! Performing any other operations 14 while changing the schema is just asking for trouble. 15 """
16 - def __init__(self, spec, basedir):
17 self.spec = spec 18 self.basedir = basedir 19 self.dbapi = self.spec.get_dbapi()
20
21 - def get_db_version(self, conn=None):
22 """ 23 Get the current schema version for this database 24 """ 25 close_conn = False 26 if not conn: 27 conn = self.spec.get_sync_connection() 28 close_conn = True 29 c = conn.cursor() 30 try: 31 try: 32 c.execute("SELECT version FROM version") 33 rows = c.fetchall() 34 assert len(rows) == 1, "%i rows in version table! (should only be 1)" % len(rows) 35 return rows[0][0] 36 except (self.dbapi.OperationalError, self.dbapi.ProgrammingError): 37 # no version table = version 0 38 return 0 39 finally: 40 if close_conn: 41 conn.close()
42
43 - def get_current_version(self):
44 """ 45 Get the current db version for this release of buildbot 46 """ 47 return CURRENT_VERSION
48
49 - def is_current(self):
50 """ 51 Is this database current? 52 """ 53 return self.get_db_version() == self.get_current_version()
54
55 - def upgrade(self, quiet=False):
56 """ 57 Upgrade this database to the current version 58 """ 59 conn = self.spec.get_sync_connection() 60 try: 61 while self.get_db_version() < self.get_current_version(): 62 next_version = self.get_db_version() + 1 63 next_version_module = reflect.namedModule("buildbot.db.schema.v%d" % next_version) 64 upg = next_version_module.Upgrader(self.dbapi, conn, self.basedir, quiet) 65 upg.upgrade() 66 conn.commit() 67 assert self.get_db_version() == next_version 68 finally: 69 conn.close()
70